hermes-agent/apps/desktop/electron/windows-child-options.ts
Brooklyn Nicholson b9cb268def fix(desktop): quote spaced Windows shell commands before cmd.exe truncates them
With shell: true, spawn hands the command line to cmd.exe, which cuts it at
the first unquoted space, so installs under spaced paths (C:\Users\John Doe\…)
fail every probe and backend launch. Adds windowsShellCommand() and applies it
at the four shell-delegating sites: the --version probe, the serve --help
probe, and both spawnOwnedBackend call sites.

Adapted from #74073 (thanks @dk3yyyy) to current site locations.
2026-09-29 18:42:10 -05:00

61 lines
2.5 KiB
TypeScript

/**
* windows-child-options.ts
*
* Shared helper for opting Windows child processes (spawn/execFileSync) into
* a hidden console. Windows spawns a visible console window per child by
* default; every desktop-launched helper process (git, curl, taskkill, the
* backend itself, the bootstrap PowerShell runner, ...) needs `windowsHide:
* true` so the user doesn't see consoles flashing on screen.
*
* Extracted into its own dependency-free module (no electron import) so it
* can be unit-tested directly for both platforms without reading source
* text, and so main.ts and bootstrap-runner.ts share exactly one
* implementation instead of each defining their own copy.
*/
import type { ExecFileSyncOptionsWithStringEncoding } from 'node:child_process'
/**
* Quote an executable token when Windows delegates execution to cmd.exe.
*
* With `shell: true`, spawn hands the whole command line to cmd.exe, which
* truncates it at the first unquoted space: `C:\Users\John Doe\...\hermes.cmd
* --version` runs `C:\Users\John` (#74064). Wrapping the executable in double
* quotes makes cmd.exe (with its `/s` strip-outer-quotes semantics) treat the
* path as one token; `command` must contain only the executable path — keep
* arguments in the separate array passed to spawn/execFileSync, and only the
* shell-delegating call sites pass `shell: true`. Already-quoted commands and
* non-Windows platforms are returned unchanged.
*/
export function windowsShellCommand(
command: string,
shell: boolean,
isWindows: boolean = process.platform === 'win32'
): string {
if (!isWindows || !shell || !command || command.startsWith('"')) {
return command
}
return `"${command}"`
}
/**
* Merge `windowsHide: true` into `options` when running on Windows, unless
* the caller already specified a `windowsHide` value (which is preserved
* as-is, including an explicit `false` for cases that intentionally want a
* visible/interactive console). No-op on non-Windows platforms.
*
* @param options - spawn/execFileSync options to (possibly) augment.
* @param isWindows - defaults to the real platform check; injectable for
* tests so both branches can be exercised without mocking process.platform.
*/
export function hiddenWindowsChildOptions(
options: any = {},
isWindows: boolean = process.platform === 'win32'
): ExecFileSyncOptionsWithStringEncoding {
if (!isWindows || Object.prototype.hasOwnProperty.call(options, 'windowsHide')) {
return options as any
}
return { ...options, windowsHide: true } as any
}