diff --git a/packages/plugin/src/server/process.test.ts b/packages/plugin/src/server/process.test.ts index 82b19a7ac8..cee1eed9cc 100644 --- a/packages/plugin/src/server/process.test.ts +++ b/packages/plugin/src/server/process.test.ts @@ -140,6 +140,26 @@ test("a Windows launch quotes for cmd.exe only when cmd.exe parses the command l }); }); +// The daemon runs git with `shell: false` on hot paths that must not block on synchronous IO, so +// an explicit `shell: false` starts a bare name directly and leaves the PATH search to Windows. +test("a Windows bare name searches PATH for a script only when the caller allows a shell", async () => { + const directory = await mkdtemp(path.join(os.tmpdir(), "plugin-path-shim-")); + roots.push(directory); + await writeFile(path.join(directory, "tool.CMD"), ""); + const env = { ...windowsEnv, PATH: directory }; + expect(resolveSpawnInvocation("tool", ["a & b"], false, "win32", env)).toEqual({ + command: "tool", + args: ["a & b"], + shell: false, + }); + expect(resolveSpawnInvocation("tool", ["a & b"], undefined, "win32", env)).toEqual({ + command: windowsEnv.ComSpec, + args: ["/d", "/s", "/c", `"${path.join(directory, "tool.CMD")} ^"a^ ^&^ b^""`], + shell: false, + windowsVerbatimArguments: true, + }); +}); + // Real Git Bash on Windows: the arguments reach Bash as given, without cmd.exe escapes. test.runIf(process.platform === "win32")( "execCommand passes arguments as given to a Windows shell other than cmd.exe", diff --git a/packages/plugin/src/server/process.ts b/packages/plugin/src/server/process.ts index 20d9bb0232..37a3775044 100644 --- a/packages/plugin/src/server/process.ts +++ b/packages/plugin/src/server/process.ts @@ -51,8 +51,8 @@ function namesCmd(shell: boolean | string | undefined): boolean { /** * How `spawnProcess` and `execCommand` start `command` on `platform`. * - * On Windows a bare name is first found on `PATH`, as cmd.exe would find it. A program then starts - * directly. A `.cmd` or `.bat` script, a name that is not on `PATH` (a cmd.exe builtin), or a + * On Windows a bare name is first found on `PATH`, as cmd.exe would find it, unless the caller + * passes `shell: false`. A program then starts directly. A `.cmd` or `.bat` script, a name that is not on `PATH` (a cmd.exe builtin), or a * `shell: true` launch runs as `cmd.exe /d /s /c "..."`, quoted by `windowsCommandLine`. A shell * the caller names other than cmd.exe gets the command and arguments as given. */ @@ -68,7 +68,8 @@ export function resolveSpawnInvocation( return { command, args: [...args], shell: requestedShell }; } const bareName = !/[\\/]/u.test(command) && !extname(command); - const found = bareName ? findWindowsCommand(command, env) : undefined; + // `shell: false` starts a bare name directly, and Windows searches PATH without blocking Node. + const found = bareName && requestedShell !== false ? findWindowsCommand(command, env) : undefined; const target = found ?? command; const commandScript = isWindowsCommandScript(target, platform); // Only cmd.exe knows a builtin, so a bare name off `PATH` goes to it unless the caller said no shell. diff --git a/packages/server/src/utils/git-repository-discovery.ts b/packages/server/src/utils/git-repository-discovery.ts index f56c2c1239..70aa3c91b0 100644 --- a/packages/server/src/utils/git-repository-discovery.ts +++ b/packages/server/src/utils/git-repository-discovery.ts @@ -113,7 +113,8 @@ let safeBareRepositorySupport: Promise | null = null; /** Reads `git --version` once per process. */ export function readSafeBareRepositorySupport(): Promise { - safeBareRepositorySupport ??= execCommand("git", ["--version"]).then( + // Started like every daemon git command, directly and without a PATH lookup in Node. + safeBareRepositorySupport ??= execCommand("git", ["--version"], { shell: false }).then( ({ stdout }) => parseSafeBareRepositorySupport(stdout), () => false, );