From 11d8468539f489d760098453810ed9b274813169 Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Mon, 1 May 2023 19:27:50 +0200 Subject: [PATCH] build: use Child.exec in ZiglingStep.run Update the run method to use Child.exec, instead of Child.spawn followed by Child.wait. This simplifies the code. --- build.zig | 47 +++++++++++++++++------------------------------ 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/build.zig b/build.zig index 03fad4c..234a58f 100644 --- a/build.zig +++ b/build.zig @@ -272,44 +272,31 @@ const ZiglingStep = struct { resetLine(); print("Checking {s}...\n", .{self.exercise.main_file}); - const cwd = self.builder.build_root.path.?; - - const argv = [_][]const u8{exe_path}; - - var child = std.ChildProcess.init(&argv, self.builder.allocator); - - child.cwd = cwd; - child.env_map = self.builder.env_map; - - child.stdin_behavior = .Inherit; - if (self.exercise.check_stdout) { - child.stdout_behavior = .Pipe; - child.stderr_behavior = .Inherit; - } else { - child.stdout_behavior = .Inherit; - child.stderr_behavior = .Pipe; - } + const b = self.step.owner; - child.spawn() catch |err| { - print("{s}Unable to spawn {s}: {s}{s}\n", .{ red_text, argv[0], @errorName(err), reset_text }); + // Allow up to 1 MB of stdout capture. + const max_output_bytes = 1 * 1024 * 1024; + + var result = Child.exec(.{ + .allocator = b.allocator, + .argv = &.{exe_path}, + .cwd = b.build_root.path.?, + .cwd_dir = b.build_root.handle, + .max_output_bytes = max_output_bytes, + }) catch |err| { + print("{s}Unable to spawn {s}: {s}{s}\n", .{ + red_text, exe_path, @errorName(err), reset_text, + }); return err; }; - // Allow up to 1 MB of stdout capture. - const max_output_len = 1 * 1024 * 1024; const output = if (self.exercise.check_stdout) - try child.stdout.?.reader().readAllAlloc(self.builder.allocator, max_output_len) + result.stdout else - try child.stderr.?.reader().readAllAlloc(self.builder.allocator, max_output_len); - - // At this point stdout is closed, wait for the process to terminate. - const term = child.wait() catch |err| { - print("{s}Unable to spawn {s}: {s}{s}\n", .{ red_text, argv[0], @errorName(err), reset_text }); - return err; - }; + result.stderr; // Make sure it exited cleanly. - switch (term) { + switch (result.term) { .Exited => |code| { if (code != 0) { print("{s}{s} exited with error code {d} (expected {d}){s}\n", .{ red_text, self.exercise.main_file, code, 0, reset_text });