Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

fix(cli): handle Windows exe healthchecks with spaces (#3111)

EmpRider51345bf2e944
diff --git a/src/shared/services/cliRuntime.ts b/src/shared/services/cliRuntime.ts
index 1c865d9b3f..9ca853eaa7 100644
--- a/src/shared/services/cliRuntime.ts
+++ b/src/shared/services/cliRuntime.ts
@@ -261,13 +261,22 @@ const parseBoolean = (value: unknown, defaultValue = true) => {
return !FALSE_VALUES.has(String(value).trim().toLowerCase());
};
+export const shouldUseShellForCommand = (command: string): boolean => {
+ if (!isWindows()) return false;
+
+ // Windows npm CLI wrappers are usually .cmd/.bat files and require cmd.exe.
+ // Direct executables should not go through the shell: absolute paths with spaces
+ // (for example C:\Users\Name With Spaces\...\claude.exe) are split by cmd.exe.
+ return /\.(?:cmd|bat)$/i.test(command);
+};
+
const runProcess = (
command: string,
args: string[],
{
env,
timeoutMs = 3000,
- useShell = isWindows(),
+ useShell = shouldUseShellForCommand(command),
}: {
env?: Record<string, string | undefined>;
timeoutMs?: number;
@@ -287,12 +296,16 @@ const runProcess = (
let timedOut = false;
let settled = false;
+ // Do NOT string-interpolate the path into a quoted shell command (hard rule
+ // #13). When useShell is false (.exe and all non-Windows), spawn passes
+ // `command` as a raw argv[0] and the OS loader handles spaces. When useShell
+ // is true (.cmd/.bat on Windows), Node quotes the command for cmd.exe itself.
const child = spawn(command, args, {
env,
stdio: ["ignore", "pipe", "pipe"],
- // On Windows, npm installs CLI wrappers as .cmd scripts (e.g. claude.cmd).
- // Without shell:true, spawn cannot resolve them via PATHEXT and the
- // healthcheck fails even when the CLI is correctly installed (#447).
+ // On Windows, npm installs CLI wrappers as .cmd/.bat scripts. Those still
+ // need cmd.exe, but direct .exe paths must avoid the shell so paths with
+ // spaces are not split before execution.
...(useShell ? { shell: true } : {}),
});
const timer = setTimeout(() => {
diff --git a/tests/unit/cli-runtime-extended.test.ts b/tests/unit/cli-runtime-extended.test.ts
index b519e1c884..cceca1a3aa 100644
--- a/tests/unit/cli-runtime-extended.test.ts
+++ b/tests/unit/cli-runtime-extended.test.ts
@@ -157,6 +157,95 @@ test("getCliRuntimeStatus reports healthcheck_failed when a binary exists but do
assert.equal(status.runtimeMode, "auto");
});
+test("getCliRuntimeStatus healthchecks Windows .exe paths with spaces without shell", async () => {
+ if (process.platform !== "win32") return;
+
+ const tempDir = path.join(createTempDir("omniroute-cli-space-"), "dir with space");
+ const scriptPath = writeScript(
+ tempDir,
+ "claude.exe",
+ "fake executable content padding padding padding"
+ );
+ const spawnCalls = [];
+
+ process.env.CLI_CLAUDE_BIN = scriptPath;
+ childProcess.spawn = (command, args, options) => {
+ spawnCalls.push({ command, args, options });
+ const child = new (require("node:events").EventEmitter)();
+ child.stdout = new (require("node:events").EventEmitter)();
+ child.stderr = new (require("node:events").EventEmitter)();
+ child.kill = () => true;
+ setImmediate(() => {
+ child.stdout.emit("data", "2.1.157 (Claude Code)\n");
+ child.emit("close", 0);
+ });
+ return child;
+ };
+ syncBuiltinESMExports();
+
+ const cliRuntime = await importFresh("windows-exe-space-no-shell");
+ const status = await cliRuntime.getCliRuntimeStatus("claude");
+
+ assert.equal(status.installed, true);
+ assert.equal(status.runnable, true);
+ assert.equal(status.reason, null);
+ assert.equal(status.commandPath, scriptPath);
+ assert.equal(spawnCalls[0].command, scriptPath);
+ assert.deepEqual(spawnCalls[0].args, ["--version"]);
+ assert.equal(spawnCalls[0].options.shell, undefined);
+});
+
+test("getCliRuntimeStatus still healthchecks Windows .cmd wrappers through shell", async () => {
+ if (process.platform !== "win32") return;
+
+ const tempDir = createTempDir("omniroute-cli-cmd-shell-");
+ const scriptPath = writeScript(
+ tempDir,
+ "codex.cmd",
+ "@echo off\r\necho codex 1.2.3\r\nREM padding padding padding\r\n"
+ );
+ const spawnCalls = [];
+
+ process.env.CLI_CODEX_BIN = scriptPath;
+ childProcess.spawn = (command, args, options) => {
+ spawnCalls.push({ command, args, options });
+ const child = new (require("node:events").EventEmitter)();
+ child.stdout = new (require("node:events").EventEmitter)();
+ child.stderr = new (require("node:events").EventEmitter)();
+ child.kill = () => true;
+ setImmediate(() => {
+ child.stdout.emit("data", "codex 1.2.3\n");
+ child.emit("close", 0);
+ });
+ return child;
+ };
+ syncBuiltinESMExports();
+
+ const cliRuntime = await importFresh("windows-cmd-shell");
+ const status = await cliRuntime.getCliRuntimeStatus("codex");
+
+ assert.equal(status.installed, true);
+ assert.equal(status.runnable, true);
+ assert.equal(status.reason, null);
+ // The command is passed to spawn unquoted — Node quotes it for cmd.exe when
+ // shell:true. We must NOT manually interpolate quotes (hard rule #13).
+ assert.equal(spawnCalls[0].command, scriptPath);
+ assert.deepEqual(spawnCalls[0].args, ["--version"]);
+ assert.equal(spawnCalls[0].options.shell, true);
+});
+
+test("shouldUseShellForCommand never uses the shell on non-Windows platforms", async () => {
+ if (process.platform === "win32") return;
+ const cliRuntime = await importFresh("should-use-shell-posix");
+ for (const cmd of ["/usr/bin/claude", "/opt/My App/claude.exe", "tool.cmd", "x.bat"]) {
+ assert.equal(
+ cliRuntime.shouldUseShellForCommand(cmd),
+ false,
+ `expected no shell on POSIX for: ${cmd}`
+ );
+ }
+});
+
test("getCliRuntimeStatus discovers binaries from CLI_EXTRA_PATHS during PATH lookup", async () => {
const tempDir = createTempDir("omniroute-cli-extra-path-");
const scriptName = process.platform === "win32" ? "qodercli.cmd" : "qodercli";