authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-12 17:20:18-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-23 22:15:09-08:00
logec56696503e702e063af8740a77376b2e7694c29
tree0781bc186578adb488bbce306970e4ba971ea06c
parente68ae8d7a1e78a25392e271d7ca894e0f09aa218

std.process.cleanExit: take an Io parameter

In case exit(0) will be called, this provides the opportunity for the application's Io instance to be the one to clear the terminal in case std.Progress or similar was used.

5 files changed, 33 insertions(+), 33 deletions(-)

lib/compiler/libc.zig+2-2
...@@ -50,7 +50,7 @@ pub fn main() !void {...@@ -50,7 +50,7 @@ pub fn main() !void {
50 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {50 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
51 try stdout.writeAll(usage_libc);51 try stdout.writeAll(usage_libc);
52 try stdout.flush();52 try stdout.flush();
53 return std.process.cleanExit();53 return std.process.cleanExit(io);
54 } else if (mem.eql(u8, arg, "-target")) {54 } else if (mem.eql(u8, arg, "-target")) {
55 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});55 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
56 i += 1;56 i += 1;
...@@ -110,7 +110,7 @@ pub fn main() !void {...@@ -110,7 +110,7 @@ pub fn main() !void {
110 try stdout.writeByte('\n');110 try stdout.writeByte('\n');
111 }111 }
112 try stdout.flush();112 try stdout.flush();
113 return std.process.cleanExit();113 return std.process.cleanExit(io);
114 }114 }
115115
116 if (input_file) |libc_file| {116 if (input_file) |libc_file| {
lib/compiler/objcopy.zig+2-2
...@@ -235,7 +235,7 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void...@@ -235,7 +235,7 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
235 const hdr = try server.receiveMessage();235 const hdr = try server.receiveMessage();
236 switch (hdr.tag) {236 switch (hdr.tag) {
237 .exit => {237 .exit => {
238 return std.process.cleanExit();238 return std.process.cleanExit(io);
239 },239 },
240 .update => {240 .update => {
241 if (seen_update) fatal("zig objcopy only supports 1 update for now", .{});241 if (seen_update) fatal("zig objcopy only supports 1 update for now", .{});
...@@ -250,7 +250,7 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void...@@ -250,7 +250,7 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
250 }250 }
251 }251 }
252 }252 }
253 return std.process.cleanExit();253 return std.process.cleanExit(io);
254}254}
255255
256const usage =256const usage =
lib/compiler/reduce.zig+3-3
...@@ -75,7 +75,7 @@ pub fn main() !void {...@@ -75,7 +75,7 @@ pub fn main() !void {
75 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {75 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
76 const stdout = Io.File.stdout();76 const stdout = Io.File.stdout();
77 try stdout.writeAll(usage);77 try stdout.writeAll(usage);
78 return std.process.cleanExit();78 return std.process.cleanExit(io);
79 } else if (mem.eql(u8, arg, "--")) {79 } else if (mem.eql(u8, arg, "--")) {
80 argv = args[i + 1 ..];80 argv = args[i + 1 ..];
81 break;81 break;
...@@ -278,10 +278,10 @@ pub fn main() !void {...@@ -278,10 +278,10 @@ pub fn main() !void {
278 try tree.render(gpa, &rendered.writer, fixups);278 try tree.render(gpa, &rendered.writer, fixups);
279 try Io.Dir.cwd().writeFile(io, .{ .sub_path = root_source_file_path, .data = rendered.written() });279 try Io.Dir.cwd().writeFile(io, .{ .sub_path = root_source_file_path, .data = rendered.written() });
280280
281 return std.process.cleanExit();281 return std.process.cleanExit(io);
282 }282 }
283 std.debug.print("no more transformations found\n", .{});283 std.debug.print("no more transformations found\n", .{});
284 return std.process.cleanExit();284 return std.process.cleanExit(io);
285}285}
286286
287fn sortTransformations(transformations: []Walk.Transformation, rng: std.Random) void {287fn sortTransformations(transformations: []Walk.Transformation, rng: std.Random) void {
src/fmt.zig+1-1
...@@ -60,7 +60,7 @@ pub fn run(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !...@@ -60,7 +60,7 @@ pub fn run(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !
60 if (mem.startsWith(u8, arg, "-")) {60 if (mem.startsWith(u8, arg, "-")) {
61 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {61 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
62 try Io.File.stdout().writeStreamingAll(io, usage_fmt);62 try Io.File.stdout().writeStreamingAll(io, usage_fmt);
63 return process.cleanExit();63 return process.cleanExit(io);
64 } else if (mem.eql(u8, arg, "--color")) {64 } else if (mem.eql(u8, arg, "--color")) {
65 if (i + 1 >= args.len) {65 if (i + 1 >= args.len) {
66 fatal("expected [auto|on|off] after --color", .{});66 fatal("expected [auto|on|off] after --color", .{});
src/main.zig+25-25
...@@ -1038,7 +1038,7 @@ fn buildOutputType(...@@ -1038,7 +1038,7 @@ fn buildOutputType(
1038 } else if (mem.startsWith(u8, arg, "-")) {1038 } else if (mem.startsWith(u8, arg, "-")) {
1039 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {1039 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
1040 try Io.File.stdout().writeStreamingAll(io, usage_build_generic);1040 try Io.File.stdout().writeStreamingAll(io, usage_build_generic);
1041 return cleanExit();1041 return cleanExit(io);
1042 } else if (mem.eql(u8, arg, "--")) {1042 } else if (mem.eql(u8, arg, "--")) {
1043 if (arg_mode == .run) {1043 if (arg_mode == .run) {
1044 // args_iter.i is 1, referring the next arg after "--" in ["--", ...]1044 // args_iter.i is 1, referring the next arg after "--" in ["--", ...]
...@@ -3646,7 +3646,7 @@ fn buildOutputType(...@@ -3646,7 +3646,7 @@ fn buildOutputType(
3646 all_args,3646 all_args,
3647 runtime_args_start,3647 runtime_args_start,
3648 );3648 );
3649 return cleanExit();3649 return cleanExit(io);
3650 },3650 },
3651 .ip4 => |ip4_addr| {3651 .ip4 => |ip4_addr| {
3652 const addr: Io.net.IpAddress = .{ .ip4 = ip4_addr };3652 const addr: Io.net.IpAddress = .{ .ip4 = ip4_addr };
...@@ -3672,7 +3672,7 @@ fn buildOutputType(...@@ -3672,7 +3672,7 @@ fn buildOutputType(
3672 all_args,3672 all_args,
3673 runtime_args_start,3673 runtime_args_start,
3674 );3674 );
3675 return cleanExit();3675 return cleanExit(io);
3676 },3676 },
3677 }3677 }
36783678
...@@ -3755,7 +3755,7 @@ fn buildOutputType(...@@ -3755,7 +3755,7 @@ fn buildOutputType(
3755 }3755 }
37563756
3757 // Skip resource deallocation in release builds; let the OS do it.3757 // Skip resource deallocation in release builds; let the OS do it.
3758 return cleanExit();3758 return cleanExit(io);
3759}3759}
37603760
3761const CreateModule = struct {3761const CreateModule = struct {
...@@ -4176,7 +4176,7 @@ fn serve(...@@ -4176,7 +4176,7 @@ fn serve(
4176 defer if (comp.debugIncremental()) ids.mutex.unlock(io);4176 defer if (comp.debugIncremental()) ids.mutex.unlock(io);
41774177
4178 switch (hdr.tag) {4178 switch (hdr.tag) {
4179 .exit => return cleanExit(),4179 .exit => return cleanExit(io),
4180 .update => {4180 .update => {
4181 tracy.frameMark();4181 tracy.frameMark();
4182 file_system_inputs.clearRetainingCapacity();4182 file_system_inputs.clearRetainingCapacity();
...@@ -4462,7 +4462,7 @@ fn runOrTest(...@@ -4462,7 +4462,7 @@ fn runOrTest(
4462 switch (term) {4462 switch (term) {
4463 .Exited => |code| {4463 .Exited => |code| {
4464 if (code == 0) {4464 if (code == 0) {
4465 return cleanExit();4465 return cleanExit(io);
4466 } else {4466 } else {
4467 process.exit(code);4467 process.exit(code);
4468 }4468 }
...@@ -4476,7 +4476,7 @@ fn runOrTest(...@@ -4476,7 +4476,7 @@ fn runOrTest(
4476 switch (term) {4476 switch (term) {
4477 .Exited => |code| {4477 .Exited => |code| {
4478 if (code == 0) {4478 if (code == 0) {
4479 return cleanExit();4479 return cleanExit(io);
4480 } else {4480 } else {
4481 const cmd = try std.mem.join(arena, " ", argv.items);4481 const cmd = try std.mem.join(arena, " ", argv.items);
4482 fatal("the following test command failed with exit code {d}:\n{s}", .{ code, cmd });4482 fatal("the following test command failed with exit code {d}:\n{s}", .{ code, cmd });
...@@ -4688,7 +4688,7 @@ fn cmdTranslateC(...@@ -4688,7 +4688,7 @@ fn cmdTranslateC(
4688 var file_reader = zig_file.reader(io, &.{});4688 var file_reader = zig_file.reader(io, &.{});
4689 _ = try stdout_writer.interface.sendFileAll(&file_reader, .unlimited);4689 _ = try stdout_writer.interface.sendFileAll(&file_reader, .unlimited);
4690 try stdout_writer.interface.flush();4690 try stdout_writer.interface.flush();
4691 return cleanExit();4691 return cleanExit(io);
4692 }4692 }
4693}4693}
46944694
...@@ -4735,7 +4735,7 @@ fn cmdInit(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !...@@ -4735,7 +4735,7 @@ fn cmdInit(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !
4735 template = .minimal;4735 template = .minimal;
4736 } else if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {4736 } else if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
4737 try Io.File.stdout().writeStreamingAll(io, usage_init);4737 try Io.File.stdout().writeStreamingAll(io, usage_init);
4738 return cleanExit();4738 return cleanExit(io);
4739 } else {4739 } else {
4740 fatal("unrecognized parameter: '{s}'", .{arg});4740 fatal("unrecognized parameter: '{s}'", .{arg});
4741 }4741 }
...@@ -4780,7 +4780,7 @@ fn cmdInit(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !...@@ -4780,7 +4780,7 @@ fn cmdInit(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !
4780 if (ok_count == template_paths.len) {4780 if (ok_count == template_paths.len) {
4781 std.log.info("see `zig build --help` for a menu of options", .{});4781 std.log.info("see `zig build --help` for a menu of options", .{});
4782 }4782 }
4783 return cleanExit();4783 return cleanExit(io);
4784 },4784 },
4785 .minimal => {4785 .minimal => {
4786 writeSimpleTemplateFile(io, Package.Manifest.basename,4786 writeSimpleTemplateFile(io, Package.Manifest.basename,
...@@ -4813,11 +4813,11 @@ fn cmdInit(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !...@@ -4813,11 +4813,11 @@ fn cmdInit(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !
4813 // their `build.zig.zon` *after* writing their `build.zig`. So this one isn't fatal.4813 // their `build.zig.zon` *after* writing their `build.zig`. So this one isn't fatal.
4814 error.PathAlreadyExists => {4814 error.PathAlreadyExists => {
4815 std.log.info("successfully populated '{s}', preserving existing '{s}'", .{ Package.Manifest.basename, Package.build_zig_basename });4815 std.log.info("successfully populated '{s}', preserving existing '{s}'", .{ Package.Manifest.basename, Package.build_zig_basename });
4816 return cleanExit();4816 return cleanExit(io);
4817 },4817 },
4818 };4818 };
4819 std.log.info("successfully populated '{s}' and '{s}'", .{ Package.Manifest.basename, Package.build_zig_basename });4819 std.log.info("successfully populated '{s}' and '{s}'", .{ Package.Manifest.basename, Package.build_zig_basename });
4820 return cleanExit();4820 return cleanExit(io);
4821 },4821 },
4822 }4822 }
4823}4823}
...@@ -5284,7 +5284,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)...@@ -5284,7 +5284,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)
5284 process.exit(1);5284 process.exit(1);
5285 }5285 }
52865286
5287 if (fetch_only) return cleanExit();5287 if (fetch_only) return cleanExit(io);
52885288
5289 var source_buf = std.array_list.Managed(u8).init(gpa);5289 var source_buf = std.array_list.Managed(u8).init(gpa);
5290 defer source_buf.deinit();5290 defer source_buf.deinit();
...@@ -5420,7 +5420,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)...@@ -5420,7 +5420,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)
54205420
5421 switch (term) {5421 switch (term) {
5422 .Exited => |code| {5422 .Exited => |code| {
5423 if (code == 0) return cleanExit();5423 if (code == 0) return cleanExit(io);
5424 // Indicates that the build runner has reported compile errors5424 // Indicates that the build runner has reported compile errors
5425 // and this parent process does not need to report any further5425 // and this parent process does not need to report any further
5426 // diagnostics.5426 // diagnostics.
...@@ -5691,7 +5691,7 @@ fn jitCmd(...@@ -5691,7 +5691,7 @@ fn jitCmd(
5691 .Exited => |code| {5691 .Exited => |code| {
5692 if (code == 0) {5692 if (code == 0) {
5693 if (options.capture != null) return;5693 if (options.capture != null) return;
5694 return cleanExit();5694 return cleanExit(io);
5695 }5695 }
5696 const cmd = try std.mem.join(arena, " ", child_argv.items);5696 const cmd = try std.mem.join(arena, " ", child_argv.items);
5697 fatal("the following build command failed with exit code {d}:\n{s}", .{ code, cmd });5697 fatal("the following build command failed with exit code {d}:\n{s}", .{ code, cmd });
...@@ -6151,7 +6151,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {...@@ -6151,7 +6151,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {
6151 if (mem.startsWith(u8, arg, "-")) {6151 if (mem.startsWith(u8, arg, "-")) {
6152 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {6152 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
6153 try Io.File.stdout().writeStreamingAll(io, usage_ast_check);6153 try Io.File.stdout().writeStreamingAll(io, usage_ast_check);
6154 return cleanExit();6154 return cleanExit(io);
6155 } else if (mem.eql(u8, arg, "-t")) {6155 } else if (mem.eql(u8, arg, "-t")) {
6156 want_output_text = true;6156 want_output_text = true;
6157 } else if (mem.eql(u8, arg, "--zon")) {6157 } else if (mem.eql(u8, arg, "--zon")) {
...@@ -6222,7 +6222,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {...@@ -6222,7 +6222,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {
6222 if (zir.hasCompileErrors()) {6222 if (zir.hasCompileErrors()) {
6223 process.exit(1);6223 process.exit(1);
6224 } else {6224 } else {
6225 return cleanExit();6225 return cleanExit(io);
6226 }6226 }
6227 }6227 }
6228 if (!build_options.enable_debug_extensions) {6228 if (!build_options.enable_debug_extensions) {
...@@ -6273,7 +6273,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {...@@ -6273,7 +6273,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {
6273 if (zir.hasCompileErrors()) {6273 if (zir.hasCompileErrors()) {
6274 process.exit(1);6274 process.exit(1);
6275 } else {6275 } else {
6276 return cleanExit();6276 return cleanExit(io);
6277 }6277 }
6278 },6278 },
6279 .zon => {6279 .zon => {
...@@ -6288,7 +6288,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {...@@ -6288,7 +6288,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {
6288 }6288 }
62896289
6290 if (!want_output_text) {6290 if (!want_output_text) {
6291 return cleanExit();6291 return cleanExit(io);
6292 }6292 }
62936293
6294 if (!build_options.enable_debug_extensions) {6294 if (!build_options.enable_debug_extensions) {
...@@ -6297,7 +6297,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {...@@ -6297,7 +6297,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {
62976297
6298 try @import("print_zoir.zig").renderToWriter(zoir, arena, stdout_bw);6298 try @import("print_zoir.zig").renderToWriter(zoir, arena, stdout_bw);
6299 try stdout_bw.flush();6299 try stdout_bw.flush();
6300 return cleanExit();6300 return cleanExit(io);
6301 },6301 },
6302 }6302 }
6303}6303}
...@@ -6325,7 +6325,7 @@ fn cmdDetectCpu(io: Io, args: []const []const u8) !void {...@@ -6325,7 +6325,7 @@ fn cmdDetectCpu(io: Io, args: []const []const u8) !void {
6325 if (mem.startsWith(u8, arg, "-")) {6325 if (mem.startsWith(u8, arg, "-")) {
6326 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {6326 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
6327 try Io.File.stdout().writeStreamingAll(io, detect_cpu_usage);6327 try Io.File.stdout().writeStreamingAll(io, detect_cpu_usage);
6328 return cleanExit();6328 return cleanExit(io);
6329 } else if (mem.eql(u8, arg, "--llvm")) {6329 } else if (mem.eql(u8, arg, "--llvm")) {
6330 use_llvm = true;6330 use_llvm = true;
6331 } else {6331 } else {
...@@ -6475,7 +6475,7 @@ fn cmdDumpLlvmInts(...@@ -6475,7 +6475,7 @@ fn cmdDumpLlvmInts(
6475 }6475 }
6476 try stdout_bw.flush();6476 try stdout_bw.flush();
64776477
6478 return cleanExit();6478 return cleanExit(io);
6479}6479}
64806480
6481/// This is only enabled for debug builds.6481/// This is only enabled for debug builds.
...@@ -6909,7 +6909,7 @@ fn cmdFetch(...@@ -6909,7 +6909,7 @@ fn cmdFetch(
6909 if (mem.startsWith(u8, arg, "-")) {6909 if (mem.startsWith(u8, arg, "-")) {
6910 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {6910 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
6911 try Io.File.stdout().writeStreamingAll(io, usage_fetch);6911 try Io.File.stdout().writeStreamingAll(io, usage_fetch);
6912 return cleanExit();6912 return cleanExit(io);
6913 } else if (mem.eql(u8, arg, "--global-cache-dir")) {6913 } else if (mem.eql(u8, arg, "--global-cache-dir")) {
6914 if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});6914 if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
6915 i += 1;6915 i += 1;
...@@ -7020,7 +7020,7 @@ fn cmdFetch(...@@ -7020,7 +7020,7 @@ fn cmdFetch(
7020 var stdout = Io.File.stdout().writerStreaming(io, &stdout_buffer);7020 var stdout = Io.File.stdout().writerStreaming(io, &stdout_buffer);
7021 try stdout.interface.print("{s}\n", .{package_hash_slice});7021 try stdout.interface.print("{s}\n", .{package_hash_slice});
7022 try stdout.interface.flush();7022 try stdout.interface.flush();
7023 return cleanExit();7023 return cleanExit(io);
7024 },7024 },
7025 .yes, .exact => |name| name: {7025 .yes, .exact => |name| name: {
7026 if (name) |n| break :name n;7026 if (name) |n| break :name n;
...@@ -7159,7 +7159,7 @@ fn cmdFetch(...@@ -7159,7 +7159,7 @@ fn cmdFetch(
7159 fatal("unable to write {s} file: {t}", .{ Package.Manifest.basename, err });7159 fatal("unable to write {s} file: {t}", .{ Package.Manifest.basename, err });
7160 };7160 };
71617161
7162 return cleanExit();7162 return cleanExit(io);
7163}7163}
71647164
7165fn createEmptyDependenciesModule(7165fn createEmptyDependenciesModule(