From df544cace97b57b318dba439d4e67a3953388477 Mon Sep 17 00:00:00 2001 From: iddev5 Date: Fri, 18 Mar 2022 13:10:41 +0530 Subject: [PATCH 1/3] std: explicitly handle error.UnexpectedExitCode in build_runner RunStep on unexpected exit code used to return error.UncleanExit, which was confusing and unclear. When it was changed, the error handling code in build_runner was not modified, which produced an error trace. This commit explicitly handles error.UnexpectedExitCode in build_runner so that the behavior now matches that of zig 0.8.1 after which it was regressed. --- lib/std/special/build_runner.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index eb83ef8fcd..bdc2d0457e 100644 --- a/lib/std/special/build_runner.zig +++ b/lib/std/special/build_runner.zig @@ -205,7 +205,7 @@ pub fn main() !void { error.InvalidStepName => { return usageAndErr(builder, true, stderr_stream); }, - error.UncleanExit => process.exit(1), + error.UnexpectedExitCode, error.UncleanExit => process.exit(1), else => return err, } }; From 8d3e7aa5e0a7b3956a5ba8cf2c85d756ab581465 Mon Sep 17 00:00:00 2001 From: iddev5 Date: Mon, 4 Apr 2022 18:33:47 +0530 Subject: [PATCH 2/3] std.testing: add function zigBuild for running zig build runner commands --- lib/std/testing.zig | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/std/testing.zig b/lib/std/testing.zig index 4146e033b4..c979a555b8 100644 --- a/lib/std/testing.zig +++ b/lib/std/testing.zig @@ -450,6 +450,36 @@ pub fn buildExe(zigexec: []const u8, zigfile: []const u8, binfile: []const u8) ! try expectEqual(ret_val, .{ .Exited = 0 }); } +/// Spawns a zig build runner process 'zigexec build subcmd' and +/// expects success +/// If specified, runs zig build in the cwd path +/// If specified, uses the specified lib_dir for zig standard library +/// instead of compiler's default library directory +pub fn runZigBuild(zigexec: []const u8, options: struct { + subcmd: ?[]const u8 = null, + cwd: ?[]const u8 = null, + lib_dir: ?[]const u8 = null, +}) !std.ChildProcess.ExecResult { + var args = std.ArrayList([]const u8).init(allocator); + defer args.deinit(); + + try args.appendSlice(&.{ zigexec, "build" }); + if (options.subcmd) |subcmd| try args.append(subcmd); + if (options.lib_dir) |lib_dir| try args.append(lib_dir); + + var result = try std.ChildProcess.exec(.{ + .allocator = allocator, + .argv = args.items, + .cwd = if (options.cwd) |c| c else null, + }); + errdefer { + allocator.free(result.stdout); + allocator.free(result.stderr); + } + + return result; +} + test "expectEqual nested array" { const a = [2][2]f32{ [_]f32{ 1.0, 0.0 }, From d9f9948b6536339747322b8a04431116b698892e Mon Sep 17 00:00:00 2001 From: iddev5 Date: Mon, 4 Apr 2022 18:34:19 +0530 Subject: [PATCH 3/3] std.build: add test for issue 10381 --- lib/std/build.zig | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/lib/std/build.zig b/lib/std/build.zig index f1287c7be5..902849a379 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -3492,3 +3492,58 @@ test "LibExeObjStep.addPackage" { const dupe = exe.packages.items[0]; try std.testing.expectEqualStrings(pkg_top.name, dupe.name); } + +test "build_runner issue 10381" { + if (builtin.os.tag == .wasi) return error.SkipZigTest; + + const progstr = + \\ pub fn main() u8 { + \\ return 1; + \\ } + ; + + const buildstr = + \\ const std = @import("std"); + \\ pub fn build(b: *std.build.Builder) void { + \\ const exe = b.addExecutable("source", "source.zig"); + \\ exe.install(); + \\ const run_cmd = exe.run(); + \\ run_cmd.step.dependOn(b.getInstallStep()); + \\ const run_step = b.step("run", "Run"); + \\ run_step.dependOn(&run_cmd.step); + \\ } + ; + + const testing = std.testing; + const allocator = testing.allocator; + + var it = try std.process.argsWithAllocator(allocator); + defer it.deinit(); + const testargs = try testing.getTestArgs(&it); + + var tmpdir = testing.tmpDir(.{ .no_follow = true }); + defer tmpdir.cleanup(); + const tmpdir_path = try tmpdir.getFullPath(allocator); + defer allocator.free(tmpdir_path); + + try tmpdir.dir.writeFile("source.zig", progstr); + try tmpdir.dir.writeFile("build.zig", buildstr); + + const cwd_path = try std.process.getCwdAlloc(allocator); + defer allocator.free(cwd_path); + const lib_dir = try std.fs.path.join(allocator, &.{ cwd_path, "lib" }); + defer allocator.free(lib_dir); + + const result = try testing.runZigBuild(testargs.zigexec, .{ + .subcmd = "run", + .cwd = tmpdir_path, + .lib_dir = lib_dir, + }); + defer { + allocator.free(result.stdout); + allocator.free(result.stderr); + } + + try testing.expectEqual(result.term, .{ .Exited = 1 }); + try testing.expect(std.mem.indexOf(u8, result.stderr, "error: UnexpectedExitCode") == null); +}