fix(shared): Windows .cmd shims no longer print DEP0190 on every spawn (#19385)

Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5.5 authored and GitHub committed 2026-10-14 04:17:42 -07:00
1 parent f5b5f9dd7b
commit c08093c2b1
8 files changed
+89 -75

No files matched your search

+25 -11
View File
@@ -681,11 +681,19 @@ effectIt.layer(NodeServices.layer)("resolveSpawnCommand", (it) => {
}),
);
it.effect("escapes the executable and arguments for Windows command shims", () =>
it.effect("builds one escaped cmd.exe line for Windows command shims", () =>
Effect.gen(function* () {
const command = yield* resolveSpawnCommand(
"vp",
["run", "value & calc", "%PATH%", 'quote"value'],
[
"run",
"value & calc",
"%PATH%",
'quote"value',
"C:\\dir with space\\",
'slash\\"quote',
"x|y<z>^",
],
{ env: { PATH: "", PATHEXT: ".COM;.EXE;.BAT;.CMD" } },
).pipe(
Effect.provideService(HostProcess.Platform, "win32"),
@@ -696,15 +704,21 @@ effectIt.layer(NodeServices.layer)("resolveSpawnCommand", (it) => {
),
);
expect(command.shell).toBe(true);
expect(command.command).not.toContain(" & ");
expect(command.command).toContain("^&");
expect(command.args).toEqual([
'^"run^"',
'^"value^ ^&^ calc^"',
'^"^%PATH^%^"',
'^"quote\\^"value^"',
]);
// No args alongside `shell: true`: Node prints DEP0190 for that pairing.
expect(command).toEqual({
command: [
String.raw`^"C:\Program^ Files\npm^ ^&^ tools\vp.cmd^"`,
String.raw`^"run^"`,
String.raw`^"value^ ^&^ calc^"`,
String.raw`^"^%PATH^%^"`,
String.raw`^"quote\^"value^"`,
String.raw`^"C:\dir^ with^ space\\^"`,
String.raw`^"slash\\\^"quote^"`,
String.raw`^"x^|y^<z^>^^^"`,
].join(" "),
args: [],
shell: true,
});
}),
);
+9 -18
View File
@@ -46,10 +46,10 @@ const WINDOWS_SHELL_META_CHARS = /([()\][%!^"`<>&|;, *?])/g;
/**
* Escapes a single argument for `cmd.exe` shell mode (`spawn(..., { shell: true })`
* on Windows). Node joins the command and arguments with spaces and hands the
* resulting string to `cmd.exe` without any quoting, so every dynamic argument
* must be escaped to survive both cmd.exe parsing and the target program's
* `CommandLineToArgvW` parsing. Mirrors cross-spawn's argument escaping.
* on Windows). Node hands the command string to `cmd.exe /d /s /c "<command>"`
* without any quoting, so every dynamic argument must be escaped to survive both
* cmd.exe parsing and the target program's `CommandLineToArgvW` parsing.
* Mirrors cross-spawn's argument escaping.
*/
function escapeWindowsShellArg(arg: string): string {
// Double up backslashes that precede a double quote, then escape the quote
@@ -63,18 +63,6 @@ function escapeWindowsShellArg(arg: string): string {
return escaped.replace(WINDOWS_SHELL_META_CHARS, "^$1");
}
/**
* Escapes arguments for shell-mode spawns: applies {@link escapeWindowsShellArg}
* when the platform is `win32` (where `shell: true` routes through `cmd.exe`)
* and returns the arguments untouched everywhere else.
*/
function sanitizeShellModeArgsForPlatform(
args: ReadonlyArray<string>,
platform: NodeJS.Platform,
): Array<string> {
return platform === "win32" ? args.map(escapeWindowsShellArg) : [...args];
}
export interface ResolvedSpawnCommand {
readonly command: string;
readonly args: ReadonlyArray<string>;
@@ -773,9 +761,12 @@ export const resolveSpawnCommand = Effect.fnUntraced(function* (
return { command: resolvedCommand, args: [...args], shell: false };
}
// Node runs a shell spawn as `cmd.exe /d /s /c "<command> <args...>"`, but
// passing args with `shell: true` prints DEP0190. Build the same escaped line
// here and pass no args.
return {
command: escapeWindowsShellArg(resolvedCommand),
args: sanitizeShellModeArgsForPlatform(args, platform),
command: [resolvedCommand, ...args].map(escapeWindowsShellArg).join(" "),
args: [],
shell: true,
};
});