authorgravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2021-10-20 15:49:48-06:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-17 14:48:22+02:00
logbaead472d7641bdd96130354bafadc1fb1ed223b
treece2e7619d3f7d91957c973e06980826444d33841
parente2ad95c0883bfaacba7377fdf3e037d9fecd4940

reduce build error noise

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" {
34383438 const dupe = exe.packages.items[0];
34393439 try std.testing.expectEqualStrings(pkg_top.name, dupe.name);
34403440}
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.
3446pub 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.
3451pub 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
36163616 .Exited => |code| {
36173617 if (code == 0) return cleanExit();
36183618
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) {
36203622 fatal("the build command failed with exit code {d}", .{code});
36213623 } else {
36223624 const cmd = try std.mem.join(arena, " ", child_argv);
test/standalone.zig+1
......@@ -49,6 +49,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
4949 cases.addBuildFile("test/standalone/issue_7030/build.zig", .{});
5050 cases.addBuildFile("test/standalone/install_raw_hex/build.zig", .{});
5151 cases.addBuildFile("test/standalone/issue_9812/build.zig", .{});
52 cases.addBuildFile("test/standalone/fail_full_report/build.zig", .{});
5253 if (builtin.os.tag != .wasi) {
5354 cases.addBuildFile("test/standalone/load_dynamic_library/build.zig", .{});
5455 }
test/standalone/fail_full_report/build.zig created+32
......@@ -0,0 +1,32 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3
4pub 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 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3
4pub 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}