authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-02 19:56:40+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-02 19:56:40+01:00
logb71e593df442f04eb21d18fc829fb50c29989014
treecb3fc030e20efdda9e427d08568e7ecf26562d06
parentaacf8ce03d2b52e52830873c40eabb1cb1eb8272
parent05346e123bc5dbc20a7a60655b06b55030a84f47

Merge pull request 'Add process.Child.Cwd, use it for cwd and remove cwd_dir field' (#31090) from squeek502/zig:unify-child-cwd into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31090 Reviewed-by: Andrew Kelley <andrew@ziglang.org>

13 files changed, 120 insertions(+), 84 deletions(-)

lib/compiler/std-docs.zig+5-5
......@@ -398,7 +398,7 @@ fn buildWasmBinary(
398398 if (code != 0) {
399399 std.log.err(
400400 "the following command exited with error code {d}:\n{s}",
401 .{ code, try std.Build.Step.allocPrintCmd(arena, null, null, argv.items) },
401 .{ code, try std.Build.Step.allocPrintCmd(arena, .inherit, null, argv.items) },
402402 );
403403 return error.WasmCompilationFailed;
404404 }
......@@ -406,14 +406,14 @@ fn buildWasmBinary(
406406 .signal => |sig| {
407407 std.log.err(
408408 "the following command terminated with signal {t}:\n{s}",
409 .{ sig, try std.Build.Step.allocPrintCmd(arena, null, null, argv.items) },
409 .{ sig, try std.Build.Step.allocPrintCmd(arena, .inherit, null, argv.items) },
410410 );
411411 return error.WasmCompilationFailed;
412412 },
413413 .stopped, .unknown => {
414414 std.log.err(
415415 "the following command terminated unexpectedly:\n{s}",
416 .{try std.Build.Step.allocPrintCmd(arena, null, null, argv.items)},
416 .{try std.Build.Step.allocPrintCmd(arena, .inherit, null, argv.items)},
417417 );
418418 return error.WasmCompilationFailed;
419419 },
......@@ -423,14 +423,14 @@ fn buildWasmBinary(
423423 try result_error_bundle.renderToStderr(io, .{}, .auto);
424424 std.log.err("the following command failed with {d} compilation errors:\n{s}", .{
425425 result_error_bundle.errorMessageCount(),
426 try std.Build.Step.allocPrintCmd(arena, null, null, argv.items),
426 try std.Build.Step.allocPrintCmd(arena, .inherit, null, argv.items),
427427 });
428428 return error.WasmCompilationFailed;
429429 }
430430
431431 return result orelse {
432432 std.log.err("child process failed to report result\n{s}", .{
433 try std.Build.Step.allocPrintCmd(arena, null, null, argv.items),
433 try std.Build.Step.allocPrintCmd(arena, .inherit, null, argv.items),
434434 });
435435 return error.WasmCompilationFailed;
436436 };
lib/std/Build.zig+2-2
......@@ -1868,7 +1868,7 @@ pub fn runAllowFail(
18681868 const io = graph.io;
18691869
18701870 const max_output_size = 400 * 1024;
1871 try Step.handleVerbose2(b, null, &graph.environ_map, argv);
1871 try Step.handleVerbose2(b, .inherit, &graph.environ_map, argv);
18721872
18731873 var child = try std.process.spawn(io, .{
18741874 .argv = argv,
......@@ -1911,7 +1911,7 @@ pub fn run(b: *Build, argv: []const []const u8) []u8 {
19111911 var code: u8 = undefined;
19121912 return b.runAllowFail(argv, &code, .inherit) catch |err| process.fatal(
19131913 "the following command failed with {t}:\n{s}",
1914 .{ err, Step.allocPrintCmd(b.allocator, null, null, argv) catch @panic("OOM") },
1914 .{ err, Step.allocPrintCmd(b.allocator, .inherit, null, argv) catch @panic("OOM") },
19151915 );
19161916}
19171917
lib/std/Build/Step.zig+16-12
......@@ -350,10 +350,10 @@ pub fn captureChildProcess(
350350
351351 // If an error occurs, it's happened in this command:
352352 assert(s.result_failed_command == null);
353 s.result_failed_command = try allocPrintCmd(gpa, null, null, argv);
353 s.result_failed_command = try allocPrintCmd(gpa, .inherit, null, argv);
354354
355355 try handleChildProcUnsupported(s);
356 try handleVerbose(s.owner, null, argv);
356 try handleVerbose(s.owner, .inherit, argv);
357357
358358 const result = std.process.run(arena, io, .{
359359 .argv = argv,
......@@ -410,7 +410,7 @@ pub fn evalZigProcess(
410410
411411 // If an error occurs, it's happened in this command:
412412 assert(s.result_failed_command == null);
413 s.result_failed_command = try allocPrintCmd(gpa, null, null, argv);
413 s.result_failed_command = try allocPrintCmd(gpa, .inherit, null, argv);
414414
415415 if (s.getZigProcess()) |zp| update: {
416416 assert(watch);
......@@ -449,7 +449,7 @@ pub fn evalZigProcess(
449449 assert(argv.len != 0);
450450
451451 try handleChildProcUnsupported(s);
452 try handleVerbose(s.owner, null, argv);
452 try handleVerbose(s.owner, .inherit, argv);
453453
454454 const zp = try gpa.create(ZigProcess);
455455 defer if (!watch) gpa.destroy(zp);
......@@ -515,7 +515,7 @@ pub fn installFile(s: *Step, src_lazy_path: Build.LazyPath, dest_path: []const u
515515 const b = s.owner;
516516 const io = b.graph.io;
517517 const src_path = src_lazy_path.getPath3(b, s);
518 try handleVerbose(b, null, &.{ "install", "-C", b.fmt("{f}", .{src_path}), dest_path });
518 try handleVerbose(b, .inherit, &.{ "install", "-C", b.fmt("{f}", .{src_path}), dest_path });
519519 return Io.Dir.updateFile(src_path.root_dir.handle, io, src_path.sub_path, .cwd(), dest_path, .{}) catch |err|
520520 return s.fail("unable to update file from '{f}' to '{s}': {t}", .{ src_path, dest_path, err });
521521}
......@@ -524,7 +524,7 @@ pub fn installFile(s: *Step, src_lazy_path: Build.LazyPath, dest_path: []const u
524524pub fn installDir(s: *Step, dest_path: []const u8) !Io.Dir.CreatePathStatus {
525525 const b = s.owner;
526526 const io = b.graph.io;
527 try handleVerbose(b, null, &.{ "install", "-d", dest_path });
527 try handleVerbose(b, .inherit, &.{ "install", "-d", dest_path });
528528 return Io.Dir.cwd().createDirPathStatus(io, dest_path, .default_dir) catch |err|
529529 return s.fail("unable to create dir '{s}': {t}", .{ dest_path, err });
530530}
......@@ -700,15 +700,15 @@ fn sendMessage(io: Io, file: Io.File, tag: std.zig.Client.Message.Tag) !void {
700700
701701pub fn handleVerbose(
702702 b: *Build,
703 opt_cwd: ?[]const u8,
703 cwd: std.process.Child.Cwd,
704704 argv: []const []const u8,
705705) error{OutOfMemory}!void {
706 return handleVerbose2(b, opt_cwd, null, argv);
706 return handleVerbose2(b, cwd, null, argv);
707707}
708708
709709pub fn handleVerbose2(
710710 b: *Build,
711 opt_cwd: ?[]const u8,
711 cwd: std.process.Child.Cwd,
712712 opt_env: ?*const std.process.Environ.Map,
713713 argv: []const []const u8,
714714) error{OutOfMemory}!void {
......@@ -716,7 +716,7 @@ pub fn handleVerbose2(
716716 const graph = b.graph;
717717 // Intention of verbose is to print all sub-process command lines to
718718 // stderr before spawning them.
719 const text = try allocPrintCmd(b.allocator, opt_cwd, if (opt_env) |env| .{
719 const text = try allocPrintCmd(b.allocator, cwd, if (opt_env) |env| .{
720720 .child = env,
721721 .parent = &graph.environ_map,
722722 } else null, argv);
......@@ -751,7 +751,7 @@ pub fn handleChildProcessTerm(s: *Step, term: std.process.Child.Term) error{ Mak
751751
752752pub fn allocPrintCmd(
753753 gpa: Allocator,
754 opt_cwd: ?[]const u8,
754 cwd: std.process.Child.Cwd,
755755 opt_env: ?struct {
756756 child: *const std.process.Environ.Map,
757757 parent: *const std.process.Environ.Map,
......@@ -796,7 +796,11 @@ pub fn allocPrintCmd(
796796 var aw: Io.Writer.Allocating = .init(gpa);
797797 defer aw.deinit();
798798 const writer = &aw.writer;
799 if (opt_cwd) |cwd| writer.print("cd {s} && ", .{cwd}) catch return error.OutOfMemory;
799 switch (cwd) {
800 .inherit => {},
801 .path => |path| writer.print("cd {s} && ", .{path}) catch return error.OutOfMemory,
802 .dir => @panic("TODO"),
803 }
800804 if (opt_env) |env| {
801805 var it = env.child.iterator();
802806 while (it.next()) |entry| {
lib/std/Build/Step/InstallArtifact.zig+1-1
......@@ -187,7 +187,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
187187 const full_dest_path = b.pathJoin(&.{ full_h_prefix, entry.path });
188188 switch (entry.kind) {
189189 .directory => {
190 try Step.handleVerbose(b, null, &.{ "install", "-d", full_dest_path });
190 try Step.handleVerbose(b, .inherit, &.{ "install", "-d", full_dest_path });
191191 const p = try step.installDir(full_dest_path);
192192 all_cached = all_cached and p == .existed;
193193 },
lib/std/Build/Step/Run.zig+2-2
......@@ -1227,7 +1227,7 @@ fn runCommand(
12271227 const gpa = options.gpa;
12281228 const io = b.graph.io;
12291229
1230 const cwd: ?[]const u8 = if (run.cwd) |lazy_cwd| lazy_cwd.getPath2(b, step) else null;
1230 const cwd: process.Child.Cwd = if (run.cwd) |lazy_cwd| .{ .path = lazy_cwd.getPath2(b, step) } else .inherit;
12311231
12321232 try step.handleChildProcUnsupported();
12331233 try Step.handleVerbose2(step.owner, cwd, run.environ_map, argv);
......@@ -1549,7 +1549,7 @@ fn spawnChildAndCollect(
15491549 assert(run.stdio == .zig_test);
15501550 }
15511551
1552 const child_cwd = if (run.cwd) |lazy_cwd| lazy_cwd.getPath2(b, &run.step) else null;
1552 const child_cwd: process.Child.Cwd = if (run.cwd) |lazy_cwd| .{ .path = lazy_cwd.getPath2(b, &run.step) } else .inherit;
15531553
15541554 // If an error occurs, it's caused by this command:
15551555 assert(run.step.result_failed_command == null);
lib/std/Build/WebServer.zig+5-5
......@@ -652,7 +652,7 @@ fn buildClientWasm(ws: *WebServer, arena: Allocator, optimize: std.builtin.Optim
652652 if (code != 0) {
653653 log.err(
654654 "the following command exited with error code {d}:\n{s}",
655 .{ code, try Build.Step.allocPrintCmd(arena, null, null, argv.items) },
655 .{ code, try Build.Step.allocPrintCmd(arena, .inherit, null, argv.items) },
656656 );
657657 return error.WasmCompilationFailed;
658658 }
......@@ -660,14 +660,14 @@ fn buildClientWasm(ws: *WebServer, arena: Allocator, optimize: std.builtin.Optim
660660 .signal => |sig| {
661661 log.err(
662662 "the following command terminated with signal {t}:\n{s}",
663 .{ sig, try Build.Step.allocPrintCmd(arena, null, null, argv.items) },
663 .{ sig, try Build.Step.allocPrintCmd(arena, .inherit, null, argv.items) },
664664 );
665665 return error.WasmCompilationFailed;
666666 },
667667 .stopped, .unknown => {
668668 log.err(
669669 "the following command terminated unexpectedly:\n{s}",
670 .{try Build.Step.allocPrintCmd(arena, null, null, argv.items)},
670 .{try Build.Step.allocPrintCmd(arena, .inherit, null, argv.items)},
671671 );
672672 return error.WasmCompilationFailed;
673673 },
......@@ -677,14 +677,14 @@ fn buildClientWasm(ws: *WebServer, arena: Allocator, optimize: std.builtin.Optim
677677 try result_error_bundle.renderToStderr(io, .{}, .auto);
678678 log.err("the following command failed with {d} compilation errors:\n{s}", .{
679679 result_error_bundle.errorMessageCount(),
680 try Build.Step.allocPrintCmd(arena, null, null, argv.items),
680 try Build.Step.allocPrintCmd(arena, .inherit, null, argv.items),
681681 });
682682 return error.WasmCompilationFailed;
683683 }
684684
685685 const base_path = result orelse {
686686 log.err("child process failed to report result\n{s}", .{
687 try Build.Step.allocPrintCmd(arena, null, null, argv.items),
687 try Build.Step.allocPrintCmd(arena, .inherit, null, argv.items),
688688 });
689689 return error.WasmCompilationFailed;
690690 };
lib/std/Io/Threaded.zig+32-10
......@@ -14606,10 +14606,14 @@ fn spawnPosix(t: *Threaded, options: process.SpawnOptions) process.SpawnError!Sp
1460614606 setUpChildIo(options.stdout, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch |err| forkBail(ep1, err);
1460714607 setUpChildIo(options.stderr, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch |err| forkBail(ep1, err);
1460814608
14609 if (options.cwd_dir) |cwd| {
14610 fchdir(cwd.handle) catch |err| forkBail(ep1, err);
14611 } else if (options.cwd) |cwd| {
14612 chdir(cwd) catch |err| forkBail(ep1, err);
14609 switch (options.cwd) {
14610 .inherit => {},
14611 .dir => |cwd| {
14612 fchdir(cwd.handle) catch |err| forkBail(ep1, err);
14613 },
14614 .path => |cwd| {
14615 chdir(cwd) catch |err| forkBail(ep1, err);
14616 },
1461314617 }
1461414618
1461514619 // Must happen after fchdir above, the cwd file descriptor might be
......@@ -15193,7 +15197,28 @@ fn processSpawnWindows(userdata: ?*anyopaque, options: process.SpawnOptions) pro
1519315197 defer arena_allocator.deinit();
1519415198 const arena = arena_allocator.allocator();
1519515199
15196 const cwd_w = if (options.cwd) |cwd| try std.unicode.wtf8ToWtf16LeAllocZ(arena, cwd) else null;
15200 const cwd_w = cwd_w: {
15201 switch (options.cwd) {
15202 .inherit => break :cwd_w null,
15203 .dir => |cwd_dir| {
15204 var dir_path_buffer = try arena.alloc(u16, windows.PATH_MAX_WIDE + 1);
15205 // TODO move GetFinalPathNameByHandle logic into std.Io.Threaded and add cancel checks
15206 try Thread.checkCancel();
15207 const dir_path = try windows.GetFinalPathNameByHandle(
15208 cwd_dir.handle,
15209 .{},
15210 dir_path_buffer[0..windows.PATH_MAX_WIDE],
15211 );
15212 dir_path_buffer[dir_path.len] = 0;
15213 // Shrink the allocation down to just the path buffer + sentinel
15214 dir_path_buffer = try arena.realloc(dir_path_buffer, dir_path.len + 1);
15215 break :cwd_w dir_path_buffer[0..dir_path.len :0];
15216 },
15217 .path => |cwd| {
15218 break :cwd_w try std.unicode.wtf8ToWtf16LeAllocZ(arena, cwd);
15219 },
15220 }
15221 };
1519715222 const cwd_w_ptr = if (cwd_w) |cwd| cwd.ptr else null;
1519815223
1519915224 const maybe_envp_buf = if (options.environ_map) |environ_map| try environ_map.createBlockWindows(arena) else null;
......@@ -15204,16 +15229,13 @@ fn processSpawnWindows(userdata: ?*anyopaque, options: process.SpawnOptions) pro
1520415229
1520515230 // The cwd provided by options is in effect when choosing the executable
1520615231 // path to match POSIX semantics.
15207 var cwd_path_w_needs_free = false;
1520815232 const cwd_path_w = x: {
1520915233 // If the app name is absolute, then we need to use its dirname as the cwd
1521015234 if (app_name_is_absolute) {
15211 cwd_path_w_needs_free = true;
1521215235 const dir = Dir.path.dirname(app_name_wtf8).?;
1521315236 break :x try std.unicode.wtf8ToWtf16LeAllocZ(arena, dir);
15214 } else if (options.cwd) |cwd| {
15215 cwd_path_w_needs_free = true;
15216 break :x try std.unicode.wtf8ToWtf16LeAllocZ(arena, cwd);
15237 } else if (cwd_w) |cwd| {
15238 break :x cwd;
1521715239 } else {
1521815240 break :x &[_:0]u16{}; // empty for cwd
1521915241 }
lib/std/process.zig+5-11
......@@ -359,17 +359,16 @@ pub const SpawnError = error{
359359 /// children of the calling process and the child had already performed an
360360 /// image replacement.
361361 ProcessAlreadyExec,
362 /// On Windows, the volume does not contain a recognized file system. File
363 /// system drivers might not be loaded, or the volume may be corrupt.
364 UnrecognizedVolume,
362365} || Io.Dir.PathNameError || Io.Cancelable || Io.UnexpectedError;
363366
364367pub const SpawnOptions = struct {
365368 argv: []const []const u8,
366369
367370 /// Set to change the current working directory when spawning the child process.
368 cwd: ?[]const u8 = null,
369 /// Set to change the current working directory when spawning the child process.
370 /// This is not yet implemented for Windows. See https://github.com/ziglang/zig/issues/5190
371 /// Once that is done, `cwd` will be deprecated in favor of this field.
372 cwd_dir: ?Io.Dir = null,
371 cwd: Child.Cwd = .inherit,
373372 /// Replaces the child environment when provided. The PATH value from here
374373 /// is not used to resolve `argv[0]`; that resolution always uses parent
375374 /// environment.
......@@ -465,11 +464,7 @@ pub const RunOptions = struct {
465464 reserve_amount: usize = 64,
466465
467466 /// Set to change the current working directory when spawning the child process.
468 cwd: ?[]const u8 = null,
469 /// Set to change the current working directory when spawning the child process.
470 /// This is not yet implemented for Windows. See https://github.com/ziglang/zig/issues/5190
471 /// Once that is done, `cwd` will be deprecated in favor of this field.
472 cwd_dir: ?Io.Dir = null,
467 cwd: Child.Cwd = .inherit,
473468 /// Replaces the child environment when provided. The PATH value from here
474469 /// is not used to resolve `argv[0]`; that resolution always uses parent
475470 /// environment.
......@@ -503,7 +498,6 @@ pub fn run(gpa: Allocator, io: Io, options: RunOptions) RunError!RunResult {
503498 var child = try spawn(io, .{
504499 .argv = options.argv,
505500 .cwd = options.cwd,
506 .cwd_dir = options.cwd_dir,
507501 .environ_map = options.environ_map,
508502 .expand_arg0 = options.expand_arg0,
509503 .progress_node = options.progress_node,
lib/std/process/Child.zig+11
......@@ -98,6 +98,17 @@ pub const Term = union(enum) {
9898 unknown: u32,
9999};
100100
101pub const Cwd = union(enum) {
102 /// CWD of the child is the same as the current CWD.
103 inherit,
104 /// On POSIX systems, `fchdir` is called after `fork` using this handle.
105 /// On Windows, the path is inferred from the provided handle and that path is used when calling `CreateProcessW`.
106 dir: Io.Dir,
107 /// On POSIX systems, `chdir` is called after `fork` using this path.
108 /// On Windows, this path is used when calling `CreateProcessW`.
109 path: []const u8,
110};
111
101112/// Requests for the operating system to forcibly terminate the child process,
102113/// then blocks until it terminates, then cleans up all resources.
103114///
test/standalone/windows_paths/test.zig+20-22
......@@ -32,39 +32,39 @@ pub fn main(init: std.process.Init) !void {
3232
3333 // With the special =X: environment variable set, drive-relative paths that
3434 // don't match the CWD's drive letter are resolved against that env var.
35 try checkRelative(arena, io, "..\\..\\bar", &.{ exe_path, drive_rel, drive_abs }, null, &alt_drive_env_map);
36 try checkRelative(arena, io, "..\\baz\\foo", &.{ exe_path, drive_abs, drive_rel }, null, &alt_drive_env_map);
35 try checkRelative(arena, io, "..\\..\\bar", &.{ exe_path, drive_rel, drive_abs }, &alt_drive_env_map);
36 try checkRelative(arena, io, "..\\baz\\foo", &.{ exe_path, drive_abs, drive_rel }, &alt_drive_env_map);
3737
3838 // Without that environment variable set, drive-relative paths that don't match the
3939 // CWD's drive letter are resolved against the root of the drive.
40 try checkRelative(arena, io, "..\\bar", &.{ exe_path, drive_rel, drive_abs }, null, &empty_env);
41 try checkRelative(arena, io, "..\\foo", &.{ exe_path, drive_abs, drive_rel }, null, &empty_env);
40 try checkRelative(arena, io, "..\\bar", &.{ exe_path, drive_rel, drive_abs }, &empty_env);
41 try checkRelative(arena, io, "..\\foo", &.{ exe_path, drive_abs, drive_rel }, &empty_env);
4242
4343 // Bare drive-relative path with no components
44 try checkRelative(arena, io, "bar", &.{ exe_path, drive_rel[0..2], drive_abs }, null, &empty_env);
45 try checkRelative(arena, io, "..", &.{ exe_path, drive_abs, drive_rel[0..2] }, null, &empty_env);
44 try checkRelative(arena, io, "bar", &.{ exe_path, drive_rel[0..2], drive_abs }, &empty_env);
45 try checkRelative(arena, io, "..", &.{ exe_path, drive_abs, drive_rel[0..2] }, &empty_env);
4646
4747 // Bare drive-relative path with no components, drive-CWD set
48 try checkRelative(arena, io, "..\\bar", &.{ exe_path, drive_rel[0..2], drive_abs }, null, &alt_drive_env_map);
49 try checkRelative(arena, io, "..\\baz", &.{ exe_path, drive_abs, drive_rel[0..2] }, null, &alt_drive_env_map);
48 try checkRelative(arena, io, "..\\bar", &.{ exe_path, drive_rel[0..2], drive_abs }, &alt_drive_env_map);
49 try checkRelative(arena, io, "..\\baz", &.{ exe_path, drive_abs, drive_rel[0..2] }, &alt_drive_env_map);
5050
5151 // Bare drive-relative path relative to the CWD should be equivalent if drive-CWD is set
52 try checkRelative(arena, io, "", &.{ exe_path, alt_drive_cwd, drive_rel[0..2] }, null, &alt_drive_env_map);
53 try checkRelative(arena, io, "", &.{ exe_path, drive_rel[0..2], alt_drive_cwd }, null, &alt_drive_env_map);
52 try checkRelative(arena, io, "", &.{ exe_path, alt_drive_cwd, drive_rel[0..2] }, &alt_drive_env_map);
53 try checkRelative(arena, io, "", &.{ exe_path, drive_rel[0..2], alt_drive_cwd }, &alt_drive_env_map);
5454
5555 // Bare drive-relative should always be equivalent to itself
56 try checkRelative(arena, io, "", &.{ exe_path, drive_rel[0..2], drive_rel[0..2] }, null, &alt_drive_env_map);
57 try checkRelative(arena, io, "", &.{ exe_path, drive_rel[0..2], drive_rel[0..2] }, null, &alt_drive_env_map);
58 try checkRelative(arena, io, "", &.{ exe_path, drive_rel[0..2], drive_rel[0..2] }, null, &empty_env);
59 try checkRelative(arena, io, "", &.{ exe_path, drive_rel[0..2], drive_rel[0..2] }, null, &empty_env);
56 try checkRelative(arena, io, "", &.{ exe_path, drive_rel[0..2], drive_rel[0..2] }, &alt_drive_env_map);
57 try checkRelative(arena, io, "", &.{ exe_path, drive_rel[0..2], drive_rel[0..2] }, &alt_drive_env_map);
58 try checkRelative(arena, io, "", &.{ exe_path, drive_rel[0..2], drive_rel[0..2] }, &empty_env);
59 try checkRelative(arena, io, "", &.{ exe_path, drive_rel[0..2], drive_rel[0..2] }, &empty_env);
6060 }
6161
6262 if (parsed_cwd_path.kind == .unc_absolute) {
6363 const drive_abs_path = try std.fmt.allocPrint(arena, "{c}:\\foo\\bar", .{alt_drive_letter});
6464
6565 {
66 try checkRelative(arena, io, drive_abs_path, &.{ exe_path, cwd_path, drive_abs_path }, null, &empty_env);
67 try checkRelative(arena, io, cwd_path, &.{ exe_path, drive_abs_path, cwd_path }, null, &empty_env);
66 try checkRelative(arena, io, drive_abs_path, &.{ exe_path, cwd_path, drive_abs_path }, &empty_env);
67 try checkRelative(arena, io, cwd_path, &.{ exe_path, drive_abs_path, cwd_path }, &empty_env);
6868 }
6969 } else if (parsed_cwd_path.kind == .drive_absolute) {
7070 const cur_drive_letter = parsed_cwd_path.root[0];
......@@ -72,14 +72,14 @@ pub fn main(init: std.process.Init) !void {
7272 const unc_cwd = try std.fmt.allocPrint(arena, "\\\\127.0.0.1\\{c}$\\{s}", .{ cur_drive_letter, path_beyond_root });
7373
7474 {
75 try checkRelative(arena, io, cwd_path, &.{ exe_path, unc_cwd, cwd_path }, null, &empty_env);
76 try checkRelative(arena, io, unc_cwd, &.{ exe_path, cwd_path, unc_cwd }, null, &empty_env);
75 try checkRelative(arena, io, cwd_path, &.{ exe_path, unc_cwd, cwd_path }, &empty_env);
76 try checkRelative(arena, io, unc_cwd, &.{ exe_path, cwd_path, unc_cwd }, &empty_env);
7777 }
7878 {
7979 const drive_abs = cwd_path;
8080 const drive_rel = parsed_cwd_path.root[0..2];
81 try checkRelative(arena, io, "", &.{ exe_path, drive_abs, drive_rel }, null, &empty_env);
82 try checkRelative(arena, io, "", &.{ exe_path, drive_rel, drive_abs }, null, &empty_env);
81 try checkRelative(arena, io, "", &.{ exe_path, drive_abs, drive_rel }, &empty_env);
82 try checkRelative(arena, io, "", &.{ exe_path, drive_rel, drive_abs }, &empty_env);
8383 }
8484 } else {
8585 return error.UnexpectedPathType;
......@@ -91,12 +91,10 @@ fn checkRelative(
9191 io: Io,
9292 expected_stdout: []const u8,
9393 argv: []const []const u8,
94 cwd: ?[]const u8,
9594 environ_map: ?*const std.process.Environ.Map,
9695) !void {
9796 const result = try std.process.run(allocator, io, .{
9897 .argv = argv,
99 .cwd = cwd,
10098 .environ_map = environ_map,
10199 });
102100 defer allocator.free(result.stdout);
test/standalone/windows_spawn/main.zig+12-2
......@@ -207,10 +207,20 @@ fn testExecError(err: anyerror, gpa: Allocator, io: Io, command: []const u8) !vo
207207}
208208
209209fn testExec(gpa: Allocator, io: Io, command: []const u8, expected_stdout: []const u8) !void {
210 return testExecWithCwd(gpa, io, command, null, expected_stdout);
210 return testExecWithCwdInner(gpa, io, command, .inherit, expected_stdout);
211211}
212212
213fn testExecWithCwd(gpa: Allocator, io: Io, command: []const u8, cwd: ?[]const u8, expected_stdout: []const u8) !void {
213fn testExecWithCwd(gpa: Allocator, io: Io, command: []const u8, cwd: []const u8, expected_stdout: []const u8) !void {
214 // Test by passing CWD as both a path and a Dir
215 try testExecWithCwdInner(gpa, io, command, .{ .path = cwd }, expected_stdout);
216
217 var cwd_dir = try Io.Dir.cwd().openDir(io, cwd, .{});
218 defer cwd_dir.close(io);
219
220 try testExecWithCwdInner(gpa, io, command, .{ .dir = cwd_dir }, expected_stdout);
221}
222
223fn testExecWithCwdInner(gpa: Allocator, io: Io, command: []const u8, cwd: std.process.Child.Cwd, expected_stdout: []const u8) !void {
214224 const result = try std.process.run(gpa, io, .{
215225 .argv = &[_][]const u8{command},
216226 .cwd = cwd,
tools/doctest.zig+6-6
......@@ -199,7 +199,7 @@ fn printOutput(
199199 if (expected_outcome == .build_fail) {
200200 const result = try process.run(arena, io, .{
201201 .argv = build_args.items,
202 .cwd = tmp_dir_path,
202 .cwd = .{ .path = tmp_dir_path },
203203 .environ_map = environ_map,
204204 });
205205 switch (result.term) {
......@@ -255,7 +255,7 @@ fn printOutput(
255255 const result = try process.run(arena, io, .{
256256 .argv = run_args,
257257 .environ_map = environ_map,
258 .cwd = tmp_dir_path,
258 .cwd = .{ .path = tmp_dir_path },
259259 });
260260 switch (result.term) {
261261 .exited => |exit_code| {
......@@ -373,7 +373,7 @@ fn printOutput(
373373 const result = try process.run(arena, io, .{
374374 .argv = test_args.items,
375375 .environ_map = environ_map,
376 .cwd = tmp_dir_path,
376 .cwd = .{ .path = tmp_dir_path },
377377 });
378378 switch (result.term) {
379379 .exited => |exit_code| {
......@@ -428,7 +428,7 @@ fn printOutput(
428428 const result = try process.run(arena, io, .{
429429 .argv = test_args.items,
430430 .environ_map = environ_map,
431 .cwd = tmp_dir_path,
431 .cwd = .{ .path = tmp_dir_path },
432432 });
433433 switch (result.term) {
434434 .exited => |exit_code| {
......@@ -503,7 +503,7 @@ fn printOutput(
503503 const result = try process.run(arena, io, .{
504504 .argv = build_args.items,
505505 .environ_map = environ_map,
506 .cwd = tmp_dir_path,
506 .cwd = .{ .path = tmp_dir_path },
507507 });
508508 switch (result.term) {
509509 .exited => |exit_code| {
......@@ -1126,7 +1126,7 @@ fn run(
11261126 const result = try process.run(allocator, io, .{
11271127 .argv = args,
11281128 .environ_map = environ_map,
1129 .cwd = cwd,
1129 .cwd = .{ .path = cwd },
11301130 });
11311131 switch (result.term) {
11321132 .exited => |exit_code| {
tools/incr-check.zig+3-6
......@@ -202,8 +202,7 @@ pub fn main(init: std.process.Init) !void {
202202 .stdout = .pipe,
203203 .stderr = .pipe,
204204 .progress_node = zig_prog_node,
205 .cwd_dir = tmp_dir,
206 .cwd = tmp_dir_path,
205 .cwd = .{ .path = tmp_dir_path },
207206 });
208207 defer child.kill(io);
209208
......@@ -533,8 +532,7 @@ const Eval = struct {
533532
534533 const result = std.process.run(eval.arena, io, .{
535534 .argv = argv,
536 .cwd_dir = eval.tmp_dir,
537 .cwd = eval.tmp_dir_path,
535 .cwd = .{ .path = eval.tmp_dir_path },
538536 }) catch |err| {
539537 if (is_foreign) {
540538 // Chances are the foreign executor isn't available. Skip this evaluation.
......@@ -626,8 +624,7 @@ const Eval = struct {
626624
627625 const result = std.process.run(eval.arena, eval.io, .{
628626 .argv = eval.cc_child_args.items,
629 .cwd_dir = eval.tmp_dir,
630 .cwd = eval.tmp_dir_path,
627 .cwd = .{ .path = eval.tmp_dir_path },
631628 .progress_node = child_prog_node,
632629 }) catch |err| {
633630 eval.fatal("failed to spawn zig cc for '{s}': {t}", .{ c_path, err });