From eb74a9d63d484a10f0e62c64de589260060856a5 Mon Sep 17 00:00:00 2001 From: Parzival-3141 <29632054+Parzival-3141@users.noreply.github.com> Date: Tue, 27 Aug 2024 00:44:55 -0400 Subject: [PATCH] group error and trace for reporting in build runner Unlike the previous commit, this ensures that the error message and trace are printed together. --- lib/compiler/build_runner.zig | 12 ++++++++---- lib/std/Build/Step.zig | 19 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/compiler/build_runner.zig b/lib/compiler/build_runner.zig index c840354acd..a4e5a4971b 100644 --- a/lib/compiler/build_runner.zig +++ b/lib/compiler/build_runner.zig @@ -893,7 +893,7 @@ fn printStepFailure( try ttyconf.setColor(stderr, .reset); } try stderr.writeAll("\n"); - } else if (s.result_error_msgs.items.len > 0) { + } else if (s.result_error_msgs.items.len > 0 or s.make_error_trace != null) { try ttyconf.setColor(stderr, .red); try stderr.writeAll(" failure\n"); try ttyconf.setColor(stderr, .reset); @@ -1102,7 +1102,7 @@ fn workerMakeOneStep( // No matter the result, we want to display error/warning messages. const show_compile_errors = !run.prominent_compile_errors and s.result_error_bundle.errorMessageCount() > 0; - const show_error_msgs = s.result_error_msgs.items.len > 0; + const show_error_msgs = s.result_error_msgs.items.len > 0 or s.make_error_trace != null; const show_stderr = s.result_stderr.len > 0; if (show_error_msgs or show_compile_errors or show_stderr) { @@ -1217,8 +1217,12 @@ pub fn printErrorMessages( try stderr.writeAll("\n"); } - if (failing_step.result_error_trace) |trace| { - try trace.format("", .{}, stderr.writer()); + if (failing_step.make_error_trace) |make_err| { + try ttyconf.setColor(stderr, .red); + try stderr.writeAll("error: "); + try ttyconf.setColor(stderr, .reset); + try stderr.writer().print("this step's make function failed with error {s}", .{@errorName(make_err.err)}); + if (make_err.trace) |trace| try trace.format("", .{}, stderr.writer()); } } diff --git a/lib/std/Build/Step.zig b/lib/std/Build/Step.zig index cbb548dd37..714e97245f 100644 --- a/lib/std/Build/Step.zig +++ b/lib/std/Build/Step.zig @@ -41,7 +41,8 @@ max_rss: usize, result_error_msgs: std.ArrayListUnmanaged([]const u8), result_error_bundle: std.zig.ErrorBundle, -result_error_trace: ?*std.builtin.StackTrace, +make_error_trace: ?MakeErrorTrace, + result_stderr: []const u8, result_cached: bool, result_duration_ns: ?u64, @@ -53,6 +54,12 @@ test_results: TestResults, /// to print along with debugging messages. debug_stack_trace: []usize, +/// Storage for unhandled errors returned by the make function. +pub const MakeErrorTrace = struct { + err: anyerror, + trace: ?*std.builtin.StackTrace, +}; + pub const TestResults = struct { fail_count: u32 = 0, skip_count: u32 = 0, @@ -215,7 +222,7 @@ pub fn init(options: StepOptions) Step { }, .result_error_msgs = .{}, .result_error_bundle = std.zig.ErrorBundle.empty, - .result_error_trace = null, + .make_error_trace = null, .result_stderr = "", .result_cached = false, .result_duration_ns = null, @@ -225,8 +232,8 @@ pub fn init(options: StepOptions) Step { } /// If the Step's `make` function reports `error.MakeFailed`, it indicates they -/// have already reported the error. Otherwise, we add a simple error report -/// here. +/// have already reported the error. Otherwise, we store the error for reporting +/// later. pub fn make(s: *Step, options: MakeOptions) error{ MakeFailed, MakeSkipped }!void { const arena = s.owner.allocator; @@ -235,8 +242,7 @@ pub fn make(s: *Step, options: MakeOptions) error{ MakeFailed, MakeSkipped }!voi error.MakeFailed => return error.MakeFailed, error.MakeSkipped => return error.MakeSkipped, else => { - s.result_error_trace = @errorReturnTrace(); - s.addError("this step's make function failed with error {s}:", .{@errorName(err)}) catch @panic("OOM"); + s.make_error_trace = .{ .err = err, .trace = @errorReturnTrace() }; return error.MakeFailed; }, } @@ -900,6 +906,7 @@ fn reset(step: *Step, gpa: Allocator) void { step.result_error_bundle.deinit(gpa); step.result_error_bundle = std.zig.ErrorBundle.empty; + step.make_error_trace = null; } /// Implementation detail of file watching. Prepares the step for being re-evaluated.