Skip to content

Commit 3db8cff

Browse files
squeek502andrewrk
authored andcommitted
spawnWindows: Fix PATH searching when cwd is absolute
Fixes a regression caused by #13983 From the added comment: We still search the path if the cwd is absolute because of the "cwd set in ChildProcess is in effect when choosing the executable path to match posix semantics" behavior--we don't want to skip searching the PATH just because we were trying to set the cwd of the child process.
1 parent 3bfae2a commit 3db8cff

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/std/child_process.zig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,8 +1022,12 @@ pub const ChildProcess = struct {
10221022
};
10231023

10241024
// If the app name had path separators, that disallows PATH searching,
1025-
// and there's no need to search the PATH if the cwd path is absolute.
1026-
if (app_dirname_w != null or fs.path.isAbsoluteWindowsWTF16(cwd_path_w)) {
1025+
// and there's no need to search the PATH if the app name is absolute.
1026+
// We still search the path if the cwd is absolute because of the
1027+
// "cwd set in ChildProcess is in effect when choosing the executable path
1028+
// to match posix semantics" behavior--we don't want to skip searching
1029+
// the PATH just because we were trying to set the cwd of the child process.
1030+
if (app_dirname_w != null or app_name_is_absolute) {
10271031
return original_err;
10281032
}
10291033

test/standalone/windows_spawn/main.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,25 @@ pub fn main() anyerror!void {
142142
defer allocator.free(goodbye_abs_path);
143143
// then the PATH should not be searched and we should get InvalidExe
144144
try testExecError(error.InvalidExe, allocator, goodbye_abs_path);
145+
146+
// If we try to exec but provide a cwd that is an absolute path, the PATH
147+
// should still be searched and the goodbye.exe in something should be found.
148+
try testExecWithCwd(allocator, "goodbye", tmp_absolute_path, "hello from exe\n");
145149
}
146150

147151
fn testExecError(err: anyerror, allocator: std.mem.Allocator, command: []const u8) !void {
148152
return std.testing.expectError(err, testExec(allocator, command, ""));
149153
}
150154

151155
fn testExec(allocator: std.mem.Allocator, command: []const u8, expected_stdout: []const u8) !void {
156+
return testExecWithCwd(allocator, command, null, expected_stdout);
157+
}
158+
159+
fn testExecWithCwd(allocator: std.mem.Allocator, command: []const u8, cwd: ?[]const u8, expected_stdout: []const u8) !void {
152160
var result = try std.ChildProcess.exec(.{
153161
.allocator = allocator,
154162
.argv = &[_][]const u8{command},
163+
.cwd = cwd,
155164
});
156165
defer allocator.free(result.stdout);
157166
defer allocator.free(result.stderr);

0 commit comments

Comments
 (0)