aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Liptak <squeek502@hotmail.com>2022-12-18 06:30:21 -0800
committerAndrew Kelley <andrew@ziglang.org>2022-12-19 04:12:46 -0500
commit3db8cffa3b383011471f425983a7e98ad8a46aa5 (patch)
treeca5e55fe37d770c3774112f01bdb04448abdfcd6
parent3bfae2a0d9503dbd832774c3b39a74dbd55e8b6e (diff)
downloadzig-3db8cffa3b383011471f425983a7e98ad8a46aa5.tar.gz
zig-3db8cffa3b383011471f425983a7e98ad8a46aa5.zip
spawnWindows: Fix PATH searching when cwd is absolute
Fixes a regression caused by https://github.com/ziglang/zig/pull/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.
-rw-r--r--lib/std/child_process.zig8
-rw-r--r--test/standalone/windows_spawn/main.zig9
2 files changed, 15 insertions, 2 deletions
diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig
index 1cdbd2e9d1..7bff9fe089 100644
--- a/lib/std/child_process.zig
+++ b/lib/std/child_process.zig
@@ -1022,8 +1022,12 @@ pub const ChildProcess = struct {
};
// If the app name had path separators, that disallows PATH searching,
- // and there's no need to search the PATH if the cwd path is absolute.
- if (app_dirname_w != null or fs.path.isAbsoluteWindowsWTF16(cwd_path_w)) {
+ // and there's no need to search the PATH if the app name is absolute.
+ // 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.
+ if (app_dirname_w != null or app_name_is_absolute) {
return original_err;
}
diff --git a/test/standalone/windows_spawn/main.zig b/test/standalone/windows_spawn/main.zig
index 970ff959f3..c0cfd15133 100644
--- a/test/standalone/windows_spawn/main.zig
+++ b/test/standalone/windows_spawn/main.zig
@@ -142,6 +142,10 @@ pub fn main() anyerror!void {
defer allocator.free(goodbye_abs_path);
// then the PATH should not be searched and we should get InvalidExe
try testExecError(error.InvalidExe, allocator, goodbye_abs_path);
+
+ // If we try to exec but provide a cwd that is an absolute path, the PATH
+ // should still be searched and the goodbye.exe in something should be found.
+ try testExecWithCwd(allocator, "goodbye", tmp_absolute_path, "hello from exe\n");
}
fn testExecError(err: anyerror, allocator: std.mem.Allocator, command: []const u8) !void {
@@ -149,9 +153,14 @@ fn testExecError(err: anyerror, allocator: std.mem.Allocator, command: []const u
}
fn testExec(allocator: std.mem.Allocator, command: []const u8, expected_stdout: []const u8) !void {
+ return testExecWithCwd(allocator, command, null, expected_stdout);
+}
+
+fn testExecWithCwd(allocator: std.mem.Allocator, command: []const u8, cwd: ?[]const u8, expected_stdout: []const u8) !void {
var result = try std.ChildProcess.exec(.{
.allocator = allocator,
.argv = &[_][]const u8{command},
+ .cwd = cwd,
});
defer allocator.free(result.stdout);
defer allocator.free(result.stderr);