Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(windows spawn): use cmd to call batch scripts #11355

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/bun.js/api/bun/process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1508,14 +1508,28 @@ pub fn spawnProcessWindows(
bun.markWindowsOnly();
bun.Analytics.Features.spawn += 1;

var uv_process_options = std.mem.zeroes(uv.uv_process_options_t);
var stack_allocator = std.heap.stackFallback(8192, bun.default_allocator);
const allocator = stack_allocator.get();

var file = options.argv0 orelse argv[0].?;
var args = argv;
var force_verbatim_arguments = false;
const extension = std.fs.path.extension(std.mem.sliceTo(file, 0));
if (bun.strings.eqlCaseInsensitiveASCIIICheckLength(extension, ".cmd") or bun.strings.eqlCaseInsensitiveASCIIICheckLength(extension, ".bat")) {
force_verbatim_arguments = true;
file = "cmd.exe";
const args_slice = std.mem.span(args);
args = try allocator.allocSentinel(?[*:0]const u8, args_slice.len + 2, null);
args[0..2].* = .{ file, "/c" };
args[2] = try std.fmt.allocPrintZ(allocator, "\"{?s}\"", .{args_slice[0]});
@memcpy(args[3..], args_slice[1..]);
}

uv_process_options.args = argv;
var uv_process_options = std.mem.zeroes(uv.uv_process_options_t);
uv_process_options.file = file;
uv_process_options.args = args;
uv_process_options.env = envp;
uv_process_options.file = options.argv0 orelse argv[0].?;
uv_process_options.exit_cb = &Process.onExitUV;
var stack_allocator = std.heap.stackFallback(8192, bun.default_allocator);
const allocator = stack_allocator.get();
const loop = options.windows.loop.platformEventLoop().uv_loop;

const cwd = try allocator.dupeZ(u8, options.cwd);
Expand All @@ -1540,7 +1554,7 @@ pub fn spawnProcessWindows(
uv_process_options.flags |= uv.UV_PROCESS_WINDOWS_HIDE;
}

if (options.windows.verbatim_arguments) {
if (options.windows.verbatim_arguments or force_verbatim_arguments) {
uv_process_options.flags |= uv.UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS;
}

Expand Down
5 changes: 5 additions & 0 deletions test/js/bun/shell/bunshell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,11 @@ ${temp_dir}`
.stdout("complex > command; $(execute)\n")
.runAsTest("complex_mixed_special_chars");
});

test("spaces in path and args", async () => {
const fields = "name scripts";
expect(async () => { await $`npm pkg get ${fields}` }).not.toThrow();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that this test case does not reproduce the original issue, since bun install does not use .cmd files.

can you create a cmd script using tempDirWithFiles and running the a script with that directory in $PATH

});
});

describe("deno_task", () => {
Expand Down