Skip to content

Commit 1d35332

Browse files
committed
fix(github-matrix): improve multiline error display in GitHub Actions
Extract core error messages and format them better for GitHub Actions annotations.
1 parent 5acfc8a commit 1d35332

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

nix/packages/github-matrix/github_matrix.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,23 @@ def parse_nix_eval_line(
128128
data: NixEvalJobsOutput = json.loads(line)
129129
if "error" in data:
130130
error_msg = data["error"]
131-
# Strip the redundant first line if it contains "does not have valid outputs"
131+
132+
# Extract the core error message (last "error:" line and following context)
132133
error_lines = error_msg.split("\n")
133-
if len(error_lines) > 1 and "does not have valid outputs" in error_lines[0]:
134-
error_msg = "\n".join(error_lines[1:]).strip()
134+
core_error_idx = -1
135+
for i in range(len(error_lines) - 1, -1, -1):
136+
if error_lines[i].strip().startswith("error:"):
137+
core_error_idx = i
138+
break
139+
140+
if core_error_idx >= 0:
141+
# Take the last error line and up to 3 lines of context after it
142+
error_msg = "\n".join(
143+
error_lines[
144+
core_error_idx : min(core_error_idx + 4, len(error_lines))
145+
]
146+
).strip()
147+
135148
return Err({"attr": data["attr"], "error": error_msg})
136149
if data["drvPath"] in drv_paths:
137150
return Ok(None)
@@ -152,8 +165,12 @@ def run_nix_eval_jobs(
152165
"""
153166
debug(f"Running command: {' '.join(cmd)}")
154167

168+
# Disable colors in nix output
169+
env = os.environ.copy()
170+
env["NO_COLOR"] = "1"
171+
155172
process = subprocess.Popen(
156-
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
173+
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, env=env
157174
)
158175
stdout_data, stderr_data = process.communicate()
159176

@@ -333,23 +350,21 @@ def clean_package_for_output(pkg: NixEvalJobsOutput) -> GitHubActionPackage:
333350
errors_by_message[err["error"]].append(err["attr"])
334351

335352
for error_msg, attrs in errors_by_message.items():
353+
# Format message with attributes on first line, then error details
336354
if len(attrs) > 1:
337-
error(
338-
f"{error_msg}\nAffected attributes: {', '.join(attrs)}",
339-
title="Nix Evaluation Error",
340-
)
355+
formatted_msg = f"Affected attributes ({len(attrs)}): {', '.join(attrs)}\n\n{error_msg}"
341356
else:
342-
error(
343-
f"{error_msg}\nAttribute: {attrs[0]}",
344-
title="Nix Evaluation Error",
345-
)
357+
formatted_msg = f"Attribute: {attrs[0]}\n\n{error_msg}"
358+
formatted_msg = formatted_msg.replace("\n", "%0A")
359+
error(formatted_msg, title="Nix Evaluation Error")
346360

347-
# Exit with error code if any evaluation errors occurred
348361
if errors_list:
349362
sys.exit(1)
350363
else:
351-
# Output matrix to GitHub Actions
352-
notice(f"Generated GitHub Actions matrix: {json.dumps(gh_output, indent=2)}")
364+
formatted_msg = f"Generated GitHub Actions matrix: {json.dumps(gh_output, indent=2)}".replace(
365+
"\n", "%0A"
366+
)
367+
notice(formatted_msg, title="GitHub Actions Matrix")
353368
set_output("matrix", json.dumps(gh_output))
354369

355370

0 commit comments

Comments
 (0)