authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-14 00:56:00-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-30 12:10:01-08:00
logd67c8f5d23ae02bf2dcc63049f39c71b35e80a50
treeb345ffdb3f1e289f997d95350276e6fbf7e62bb7
parentd579375a7af91320bd932878d2a5b124e2572b11

compiler: update for std.Io.File.MultiReader API


6 files changed, 59 insertions(+), 22 deletions(-)

lib/std/Build/Step.zig+10-1
......@@ -539,6 +539,7 @@ fn zigProcessUpdate(s: *Step, zp: *ZigProcess, watch: bool, web_server: ?*Build.
539539 if (!watch) try sendMessage(io, zp.child.stdin.?, .exit);
540540
541541 var result: ?Path = null;
542 var eos_err: error{EndOfStream}!void = {};
542543
543544 const stdout = zp.multi_reader.fileReader(0);
544545
......@@ -549,7 +550,13 @@ fn zigProcessUpdate(s: *Step, zp: *ZigProcess, watch: bool, web_server: ?*Build.
549550 error.ReadFailed => return stdout.err.?,
550551 };
551552 const body = stdout.interface.take(header.bytes_len) catch |err| switch (err) {
552 error.EndOfStream => |e| return e,
553 error.EndOfStream => |e| {
554 // Better to report the crash with stderr below, but we set
555 // this in case the child exits successfully while violating
556 // this protocol.
557 eos_err = e;
558 break;
559 },
553560 error.ReadFailed => return stdout.err.?,
554561 };
555562 switch (header.tag) {
......@@ -647,6 +654,8 @@ fn zigProcessUpdate(s: *Step, zp: *ZigProcess, watch: bool, web_server: ?*Build.
647654 try s.result_error_msgs.append(arena, try arena.dupe(u8, stderr_contents));
648655 }
649656
657 try eos_err;
658
650659 return result;
651660}
652661
lib/std/Io/File/MultiReader.zig+8
......@@ -246,6 +246,14 @@ pub fn fill(mr: *MultiReader, unused_capacity: usize, timeout: Io.Timeout) FillE
246246 if (!any_completed) return error.EndOfStream;
247247}
248248
249/// Wait until all streams fail or reach the end.
250pub fn fillRemaining(mr: *MultiReader, timeout: Io.Timeout) Io.Batch.WaitError!void {
251 while (fill(mr, 1, timeout)) |_| {} else |err| switch (err) {
252 error.EndOfStream => return,
253 else => |e| return e,
254 }
255}
256
249257fn rebaseGrowing(mr: *MultiReader, context: *Context, capacity: usize) Allocator.Error!void {
250258 const gpa = mr.gpa;
251259 const r = &context.fr.interface;
lib/std/process.zig+2
......@@ -488,6 +488,7 @@ pub const RunOptions = struct {
488488 create_no_window: bool = true,
489489 /// Darwin-only. Disable ASLR for the child process.
490490 disable_aslr: bool = false,
491 timeout: Io.Timeout = .none,
491492};
492493
493494pub const RunResult = struct {
......@@ -529,6 +530,7 @@ pub fn run(gpa: Allocator, io: Io, options: RunOptions) RunError!RunResult {
529530 .stderr = &stderr,
530531 .stdout_limit = options.stdout_limit,
531532 .stderr_limit = options.stderr_limit,
533 .timeout = options.timeout,
532534 });
533535
534536 const term = try child.wait(io);
lib/std/process/Child.zig+2-1
......@@ -137,6 +137,7 @@ pub const CollectOutputOptions = struct {
137137 allocator: ?Allocator = null,
138138 stdout_limit: Io.Limit = .unlimited,
139139 stderr_limit: Io.Limit = .unlimited,
140 timeout: Io.Timeout = .none,
140141};
141142
142143/// Collect the output from the process's stdout and stderr. Will return once
......@@ -173,7 +174,7 @@ pub fn collectOutput(child: *const Child, io: Io, options: CollectOutputOptions)
173174 remaining += 1;
174175 }
175176 while (remaining > 0) {
176 try batch.wait(io, .none);
177 try batch.wait(io, options.timeout);
177178 while (batch.next()) |op| {
178179 const n = try reads[op].file_read_streaming.status.result;
179180 if (n == 0) {
lib/std/zig/LibCInstallation.zig+4-2
......@@ -268,7 +268,8 @@ fn findNativeIncludeDirPosix(self: *LibCInstallation, gpa: Allocator, io: Io, ar
268268 });
269269
270270 const run_res = std.process.run(gpa, io, .{
271 .max_output_bytes = 1024 * 1024,
271 .stdout_limit = .limited(1024 * 1024),
272 .stderr_limit = .limited(1024 * 1024),
272273 .argv = argv.items,
273274 .environ_map = &environ_map,
274275 // Some C compilers, such as Clang, are known to rely on argv[0] to find the path
......@@ -584,7 +585,8 @@ fn ccPrintFileName(gpa: Allocator, io: Io, args: CCPrintFileNameOptions) ![]u8 {
584585 try argv.append(arg1);
585586
586587 const run_res = std.process.run(gpa, io, .{
587 .max_output_bytes = 1024 * 1024,
588 .stdout_limit = .limited(1024 * 1024),
589 .stderr_limit = .limited(1024 * 1024),
588590 .argv = argv.items,
589591 .environ_map = &environ_map,
590592 // Some C compilers, such as Clang, are known to rely on argv[0] to find the path
src/Compilation.zig+33-18
......@@ -6873,6 +6873,7 @@ fn spawnZigRc(
68736873 child_progress_node: std.Progress.Node,
68746874) !void {
68756875 const io = comp.io;
6876 const gpa = comp.gpa;
68766877 var node_name: std.ArrayList(u8) = .empty;
68776878 defer node_name.deinit(arena);
68786879
......@@ -6887,55 +6888,69 @@ fn spawnZigRc(
68876888 });
68886889 defer child.kill(io);
68896890
6890 var poller = std.Io.poll(comp.gpa, enum { stdout, stderr }, .{
6891 .stdout = child.stdout.?,
6892 .stderr = child.stderr.?,
6893 });
6894 defer poller.deinit();
6891 var multi_reader_buffer: Io.File.MultiReader.Buffer(2) = undefined;
6892 var multi_reader: Io.File.MultiReader = undefined;
6893 multi_reader.init(gpa, io, multi_reader_buffer.toStreams(), &.{ child.stdout.?, child.stderr.? });
6894 defer multi_reader.deinit();
68956895
6896 const stdout = poller.reader(.stdout);
6896 const stdout = multi_reader.fileReader(0);
6897 const MessageHeader = std.zig.Server.Message.Header;
68976898
6898 poll: while (true) {
6899 const MessageHeader = std.zig.Server.Message.Header;
6900 while (stdout.buffered().len < @sizeOf(MessageHeader)) if (!try poller.poll()) break :poll;
6901 const header = stdout.takeStruct(MessageHeader, .little) catch unreachable;
6902 while (stdout.buffered().len < header.bytes_len) if (!try poller.poll()) break :poll;
6903 const body = stdout.take(header.bytes_len) catch unreachable;
6899 var eos_err: error{EndOfStream}!void = {};
69046900
6901 while (true) {
6902 const header = stdout.interface.takeStruct(MessageHeader, .little) catch |err| switch (err) {
6903 error.EndOfStream => break,
6904 error.ReadFailed => return stdout.err.?,
6905 };
6906 const body = stdout.interface.take(header.bytes_len) catch |err| switch (err) {
6907 error.EndOfStream => |e| {
6908 // Better to report the crash with stderr below, but we set
6909 // this in case the child exits successfully while violating
6910 // this protocol.
6911 eos_err = e;
6912 break;
6913 },
6914 error.ReadFailed => return stdout.err.?,
6915 };
69056916 switch (header.tag) {
69066917 // We expect exactly one ErrorBundle, and if any error_bundle header is
69076918 // sent then it's a fatal error.
69086919 .error_bundle => {
6909 const error_bundle = try std.zig.Server.allocErrorBundle(comp.gpa, body);
6920 const error_bundle = try std.zig.Server.allocErrorBundle(gpa, body);
69106921 return comp.failWin32ResourceWithOwnedBundle(win32_resource, error_bundle);
69116922 },
69126923 else => {}, // ignore other messages
69136924 }
69146925 }
69156926
6916 // Just in case there's a failure that didn't send an ErrorBundle (e.g. an error return trace)
6917 const stderr = poller.reader(.stderr);
6927 try multi_reader.fillRemaining(.none);
69186928
6929 // Just in case there's a failure that didn't send an ErrorBundle (e.g. an error return trace)
69196930 const term = child.wait(io) catch |err| {
69206931 return comp.failWin32Resource(win32_resource, "unable to wait for {s} rc: {t}", .{ argv[0], err });
69216932 };
69226933
6934 const stderr = multi_reader.reader(1).buffered();
6935
69236936 switch (term) {
69246937 .exited => |code| {
69256938 if (code != 0) {
6926 log.err("zig rc failed with stderr:\n{s}", .{stderr.buffered()});
6939 log.err("zig rc failed with stderr:\n{s}", .{stderr});
69276940 return comp.failWin32Resource(win32_resource, "zig rc exited with code {d}", .{code});
69286941 }
69296942 },
69306943 .signal => |sig| {
6931 log.err("zig rc signaled {t} with stderr:\n{s}", .{ sig, stderr.buffered() });
6944 log.err("zig rc signaled {t} with stderr:\n{s}", .{ sig, stderr });
69326945 return comp.failWin32Resource(win32_resource, "zig rc terminated unexpectedly", .{});
69336946 },
69346947 else => {
6935 log.err("zig rc terminated with stderr:\n{s}", .{stderr.buffered()});
6948 log.err("zig rc terminated with stderr:\n{s}", .{stderr});
69366949 return comp.failWin32Resource(win32_resource, "zig rc terminated unexpectedly", .{});
69376950 },
69386951 }
6952
6953 try eos_err;
69396954}
69406955
69416956pub fn tmpFilePath(comp: Compilation, ally: Allocator, suffix: []const u8) error{OutOfMemory}![]const u8 {