Commit 6a1021fb7d

Ryan Liptak <squeek502@hotmail.com>
2022-12-17 04:33:06
ChildProcess.spawnWindows: Fix PATH search when the ext is in the command
For example, if the command is specified as `something.exe`, the retry will now try: ``` C:\some\path\something.exe C:\some\path\something.exe.COM C:\some\path\something.exe.EXE C:\some\path\something.exe.BAT ... etc ... ``` whereas before it would only try the versions with an added extension from `PATHEXT`, which would cause the retry to fail on things that it should find.
1 parent 077b003
Changed files (1)
lib/std/child_process.zig
@@ -995,12 +995,22 @@ pub const ChildProcess = struct {
                 const path_no_ext = try fs.path.join(self.allocator, &[_][]const u8{ search_path, app_name });
                 defer self.allocator.free(path_no_ext);
 
+                const path_no_ext_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, path_no_ext);
+                defer self.allocator.free(path_no_ext_w);
+
+                if (windowsCreateProcess(path_no_ext_w.ptr, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo)) |_| {
+                    break :retry;
+                } else |err| switch (err) {
+                    error.FileNotFound, error.AccessDenied => {},
+                    else => return err,
+                }
+
                 var ext_it = mem.tokenize(u8, PATHEXT, ";");
                 while (ext_it.next()) |app_ext| {
-                    const joined_path = try mem.concat(self.allocator, u8, &[_][]const u8{ path_no_ext, app_ext });
-                    defer self.allocator.free(joined_path);
+                    const ext_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, app_ext);
+                    defer self.allocator.free(ext_w);
 
-                    const joined_path_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, joined_path);
+                    const joined_path_w = try std.mem.concatWithSentinel(self.allocator, u16, &.{ path_no_ext_w, ext_w }, 0);
                     defer self.allocator.free(joined_path_w);
 
                     if (windowsCreateProcess(joined_path_w.ptr, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo)) |_| {