Files
thedotmack__claude-mem/tests/shared/spawn-cmd-verbatim.test.ts
Sam Syed 36c0aa943f fix(windows): outer-wrap + verbatim the cmd.exe shim payload
buildSpawnSyncInvocation wraps .cmd/.bat shims in a hand-built
`cmd.exe /d /s /c <payload>`. The payload quoted each token but (1) never
wrapped the whole command line in an outer quote pair, so `cmd /s` stripped
the real per-arg quotes, and (2) never set windowsVerbatimArguments, so Node
re-escaped the payload's `"` into `\"`. Either defect alone breaks the call;
together they made every codex.cmd invocation fail with "'...' is not
recognized as an internal or external command", so the Codex CLI step of
`npx claude-mem install` failed on Windows.

Wrap the per-arg-quoted command line in one outer quote pair and set
windowsVerbatimArguments -- the same command line Node's own shell:true path
builds. Confirmed against the real codex.cmd and a space-containing arg.

The spawn-contract-windows assertions had encoded the broken payload (they
checked the constructed string but never ran it through cmd.exe); updated them
and added a dependency-light regression test that imports only src/shared/spawn.

Follow-up to #2695.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-13 05:24:30 +01:00

51 lines
2.4 KiB
TypeScript

import { describe, it, expect } from 'bun:test';
import { buildSpawnSyncInvocation } from '../../src/shared/spawn.js';
// Regression for the Windows codex.cmd spawn failure (issue #2695 follow-up):
// buildSpawnSyncInvocation builds ONE pre-quoted `cmd.exe /d /s /c "<shim>" ...`
// payload string. Two invariants keep that payload intact end-to-end:
// 1. windowsVerbatimArguments — without it Node re-applies its own argv
// escaping, turning the leading `"` into `\"`, so cmd.exe tries to run a
// program literally named `\"C:\...\codex.cmd\"` and dies.
// 2. an outer quote pair around the whole command line — `cmd /s /c` strips
// the outermost quotes, so without the wrap it eats the real per-arg quotes
// and mangles the command line (breaking any arg that contains spaces).
// This file only imports src/shared/spawn.ts (no heavy deps), so it runs even in
// a minimal checkout, unlike tests/services/integrations/spawn-contract-windows.
describe('buildSpawnSyncInvocation Windows cmd.exe verbatim contract', () => {
it('outer-wraps the pre-quoted cmd.exe payload and marks it verbatim', () => {
const invocation = buildSpawnSyncInvocation(
'C:\\Users\\tester\\AppData\\Local\\Programs\\nodejs\\codex.cmd',
['--version'],
{ encoding: 'utf-8', stdio: ['ignore', 'pipe', 'pipe'] },
'win32',
);
// command is process.env.ComSpec ?? 'cmd.exe' — a full path on a real box.
expect(invocation.command.toLowerCase().endsWith('cmd.exe')).toBe(true);
expect(invocation.args).toEqual([
'/d',
'/s',
'/c',
'""C:\\Users\\tester\\AppData\\Local\\Programs\\nodejs\\codex.cmd" "--version""',
]);
expect(invocation.options.windowsVerbatimArguments).toBe(true);
});
it('does NOT force verbatim args for directly-spawned native executables', () => {
// .exe is spawned directly with its own arg array; forcing verbatim here
// would break normal Node quoting for args that contain spaces.
const invocation = buildSpawnSyncInvocation(
'C:\\Tools\\codex.exe',
['run', 'C:\\Path With Spaces'],
{ encoding: 'utf-8', stdio: ['ignore', 'pipe', 'pipe'] },
'win32',
);
expect(invocation.command).toBe('C:\\Tools\\codex.exe');
expect(invocation.args).toEqual(['run', 'C:\\Path With Spaces']);
expect(invocation.options.windowsVerbatimArguments).not.toBe(true);
});
});