| author | |
| committer | |
| log | baead472d7641bdd96130354bafadc1fb1ed223b |
| tree | ce2e7619d3f7d91957c973e06980826444d33841 |
| parent | e2ad95c0883bfaacba7377fdf3e037d9fecd4940 |
Address https://github.com/ziglang/zig/issues/3477
This provides a mechanism for builds to fully report an error to the user and prevent zig from piling on extra noise.5 files changed, 58 insertions(+), 1 deletions(-)
lib/std/build.zig+14| ... | @@ -3438,3 +3438,17 @@ test "LibExeObjStep.addPackage" { | ... | @@ -3438,3 +3438,17 @@ test "LibExeObjStep.addPackage" { |
| 3438 | const dupe = exe.packages.items[0]; | 3438 | const dupe = exe.packages.items[0]; |
| 3439 | try std.testing.expectEqualStrings(pkg_top.name, dupe.name); | 3439 | try std.testing.expectEqualStrings(pkg_top.name, dupe.name); |
| 3440 | } | 3440 | } |
| 3441 | |||
| 3442 | /// This exit code from either "zig build" or the build_runner indicates | ||
| 3443 | /// the "full reason" for a build failure has already been reported to stderr. | ||
| 3444 | /// This will prevent 'zig` from piling on errors to the ones reported by the | ||
| 3445 | /// build_runner. | ||
| 3446 | pub const fail_fully_reported_exit_code = 0x3f; | ||
| 3447 | |||
| 3448 | /// Print an error message to stderr and exit with a special exit | ||
| 3449 | /// code that notifies the invoking process that the build error | ||
| 3450 | /// has already been fully reported to stderr. | ||
| 3451 | pub fn fatalFullReport(comptime error_fmt: []const u8, args: anytype) noreturn { | ||
| 3452 | std.log.err(error_fmt, args); | ||
| 3453 | std.os.exit(fail_fully_reported_exit_code); | ||
| 3454 | } |
src/main.zig+3-1| ... | @@ -3616,7 +3616,9 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi | ... | @@ -3616,7 +3616,9 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi |
| 3616 | .Exited => |code| { | 3616 | .Exited => |code| { |
| 3617 | if (code == 0) return cleanExit(); | 3617 | if (code == 0) return cleanExit(); |
| 3618 | 3618 | ||
| 3619 | if (prominent_compile_errors) { | 3619 | if (code == std.build.fail_fully_reported_exit_code) { |
| 3620 | process.exit(std.build.fail_fully_reported_exit_code); | ||
| 3621 | } else if (prominent_compile_errors) { | ||
| 3620 | fatal("the build command failed with exit code {d}", .{code}); | 3622 | fatal("the build command failed with exit code {d}", .{code}); |
| 3621 | } else { | 3623 | } else { |
| 3622 | const cmd = try std.mem.join(arena, " ", child_argv); | 3624 | const cmd = try std.mem.join(arena, " ", child_argv); |
test/standalone.zig+1| ... | @@ -49,6 +49,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void { | ... | @@ -49,6 +49,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void { |
| 49 | cases.addBuildFile("test/standalone/issue_7030/build.zig", .{}); | 49 | cases.addBuildFile("test/standalone/issue_7030/build.zig", .{}); |
| 50 | cases.addBuildFile("test/standalone/install_raw_hex/build.zig", .{}); | 50 | cases.addBuildFile("test/standalone/install_raw_hex/build.zig", .{}); |
| 51 | cases.addBuildFile("test/standalone/issue_9812/build.zig", .{}); | 51 | cases.addBuildFile("test/standalone/issue_9812/build.zig", .{}); |
| 52 | cases.addBuildFile("test/standalone/fail_full_report/build.zig", .{}); | ||
| 52 | if (builtin.os.tag != .wasi) { | 53 | if (builtin.os.tag != .wasi) { |
| 53 | cases.addBuildFile("test/standalone/load_dynamic_library/build.zig", .{}); | 54 | cases.addBuildFile("test/standalone/load_dynamic_library/build.zig", .{}); |
| 54 | } | 55 | } |
test/standalone/fail_full_report/build.zig created+32| ... | @@ -0,0 +1,32 @@ | ||
| 1 | const std = @import("std"); | ||
| 2 | const Builder = std.build.Builder; | ||
| 3 | |||
| 4 | pub fn build(b: *Builder) !void { | ||
| 5 | const test_step = b.step("test", "The test"); | ||
| 6 | |||
| 7 | { | ||
| 8 | const run_step = b.addSystemCommand(&[_][]const u8{ | ||
| 9 | b.zig_exe, | ||
| 10 | "build", | ||
| 11 | "--build-file", | ||
| 12 | "build2.zig", | ||
| 13 | }); | ||
| 14 | run_step.stdout_action = .{ .expect_exact = "" }; | ||
| 15 | test_step.dependOn(&run_step.step); | ||
| 16 | } | ||
| 17 | |||
| 18 | { | ||
| 19 | const run_step = b.addSystemCommand(&[_][]const u8{ | ||
| 20 | b.zig_exe, | ||
| 21 | "build", | ||
| 22 | "--build-file", | ||
| 23 | "build2.zig", | ||
| 24 | "-Dbadoption", | ||
| 25 | }); | ||
| 26 | run_step.stderr_action = .{ .expect_exact = "error: got a bad build option!\n" }; | ||
| 27 | run_step.expected_exit_code = std.build.fail_fully_reported_exit_code; | ||
| 28 | test_step.dependOn(&run_step.step); | ||
| 29 | } | ||
| 30 | |||
| 31 | b.default_step.dependOn(test_step); | ||
| 32 | } | ||
test/standalone/fail_full_report/build2.zig created+8| ... | @@ -0,0 +1,8 @@ | ||
| 1 | const std = @import("std"); | ||
| 2 | const Builder = std.build.Builder; | ||
| 3 | |||
| 4 | pub fn build(b: *Builder) !void { | ||
| 5 | const bad_option = if (b.option(bool, "badoption", "Use this to emulator a bad build option")) |o| o else false; | ||
| 6 | if (bad_option) | ||
| 7 | std.build.fatalFullReport("got a bad build option!", .{}); | ||
| 8 | } | ||