diff --git a/.changeset/fix-windows-workspace-shim.md b/.changeset/fix-windows-workspace-shim.md new file mode 100644 index 000000000..30f7fd090 --- /dev/null +++ b/.changeset/fix-windows-workspace-shim.md @@ -0,0 +1,6 @@ +--- +"@bradygaster/squad-cli": patch +"@bradygaster/squad-sdk": patch +--- + +Fix Windows launcher resolution for workspace-local Squad and npx shims. diff --git a/packages/squad-sdk/src/client/adapters/claude-plugin.ts b/packages/squad-sdk/src/client/adapters/claude-plugin.ts index a0cb7beec..fc5ec098d 100644 --- a/packages/squad-sdk/src/client/adapters/claude-plugin.ts +++ b/packages/squad-sdk/src/client/adapters/claude-plugin.ts @@ -300,9 +300,10 @@ function withoutShell(command, prefix) { if (!win || !/\\.(cmd|bat)$/i.test(command)) return [command, prefix]; const shim = path.isAbsolute(command) ? command : onPath(path.basename(command, path.extname(command))); const dir = shim ? path.dirname(shim) : path.dirname(process.execPath); + const modules = path.basename(dir).toLowerCase() === '.bin' ? path.dirname(dir) : path.join(dir, 'node_modules'); const candidates = path.basename(command).toLowerCase() === 'npx.cmd' - ? [path.join(dir, 'node_modules/npm/bin/npx-cli.js'), path.join(path.dirname(process.execPath), 'node_modules/npm/bin/npx-cli.js')] - : [path.join(dir, 'node_modules', ...PKG.split('/'), 'dist/cli-entry.js')]; + ? [path.join(modules, 'npm/bin/npx-cli.js'), path.join(path.dirname(process.execPath), 'node_modules/npm/bin/npx-cli.js')] + : [path.join(modules, ...PKG.split('/'), 'dist/cli-entry.js')]; const entry = candidates.find((candidate) => fs.existsSync(candidate)); if (!entry) throw new Error(\`Cannot resolve the JavaScript entry for \${command}; set SQUAD_CLI to cli-entry.js\`); return [process.execPath, [entry, ...prefix]]; diff --git a/plugins/squad/scripts/squad.mjs b/plugins/squad/scripts/squad.mjs index 8f2e9ceca..eaf6460b5 100755 --- a/plugins/squad/scripts/squad.mjs +++ b/plugins/squad/scripts/squad.mjs @@ -187,9 +187,10 @@ function withoutShell(command, prefix) { if (!win || !/\.(cmd|bat)$/i.test(command)) return [command, prefix]; const shim = path.isAbsolute(command) ? command : onPath(path.basename(command, path.extname(command))); const dir = shim ? path.dirname(shim) : path.dirname(process.execPath); + const modules = path.basename(dir).toLowerCase() === '.bin' ? path.dirname(dir) : path.join(dir, 'node_modules'); const candidates = path.basename(command).toLowerCase() === 'npx.cmd' - ? [path.join(dir, 'node_modules/npm/bin/npx-cli.js'), path.join(path.dirname(process.execPath), 'node_modules/npm/bin/npx-cli.js')] - : [path.join(dir, 'node_modules', ...PKG.split('/'), 'dist/cli-entry.js')]; + ? [path.join(modules, 'npm/bin/npx-cli.js'), path.join(path.dirname(process.execPath), 'node_modules/npm/bin/npx-cli.js')] + : [path.join(modules, ...PKG.split('/'), 'dist/cli-entry.js')]; const entry = candidates.find((candidate) => fs.existsSync(candidate)); if (!entry) throw new Error(`Cannot resolve the JavaScript entry for ${command}; set SQUAD_CLI to cli-entry.js`); return [process.execPath, [entry, ...prefix]]; diff --git a/test/launcher-argv.test.ts b/test/launcher-argv.test.ts index b18adb1c4..42e13cf71 100644 --- a/test/launcher-argv.test.ts +++ b/test/launcher-argv.test.ts @@ -62,8 +62,8 @@ function write(file: string, body: string, mode?: number): void { } /** A shim that must never run: under cmd.exe it prints SHELL-USED. */ -function cmdShim(name: string): void { - write(path.join(bin, `${name}.cmd`), '@echo SHELL-USED %*\r\n'); +function cmdShim(name: string, dir = bin): void { + write(path.join(dir, `${name}.cmd`), '@echo SHELL-USED %*\r\n'); } function run(args: string[], env: Record = {}) { @@ -112,6 +112,53 @@ describe('the launcher passes argv through unchanged', () => { expect(parsed(run(HOSTILE, { SQUAD_ALLOW_NPX: '1' }))).toEqual(['-y', `${PKG.join('/')}@2.0.0`, ...HOSTILE]); }); + describe('when a .cmd shim has no JavaScript entry beside it', () => { + it.runIf(win)('resolves a workspace .bin shim to its sibling package without a shell', () => { + const modules = path.join(root, 'workspace', 'node_modules'); + const workspaceBin = path.join(modules, '.bin'); + cmdShim('squad', workspaceBin); + const pkg = path.join(modules, ...PKG); + write(path.join(pkg, 'package.json'), JSON.stringify({ name: PKG.join('/'), version: '2.0.0' })); + write(path.join(pkg, 'dist', 'cli-entry.js'), ECHO); + expect(parsed(run(HOSTILE, { PATH: [workspaceBin, path.dirname(process.execPath)].join(path.delimiter) }))).toEqual(HOSTILE); + }); + + it.runIf(win)('resolves an explicitly configured workspace .bin shim', () => { + const modules = path.join(root, 'workspace', 'node_modules'); + const workspaceBin = path.join(modules, '.bin'); + cmdShim('squad', workspaceBin); + write(path.join(modules, ...PKG, 'dist', 'cli-entry.js'), ECHO); + expect(parsed(run(HOSTILE, { SQUAD_CLI: path.join(workspaceBin, 'squad.cmd') }))).toEqual(HOSTILE); + }); + + it.runIf(win)('skips an outdated workspace .bin shim in favor of a recorded CLI', () => { + const modules = path.join(root, 'workspace', 'node_modules'); + const workspaceBin = path.join(modules, '.bin'); + cmdShim('squad', workspaceBin); + const pkg = path.join(modules, ...PKG); + write(path.join(pkg, 'package.json'), JSON.stringify({ name: PKG.join('/'), version: '1.0.0' })); + write(path.join(pkg, 'dist', 'cli-entry.js'), "throw new Error('outdated CLI selected');\n"); + const recorded = path.join(root, 'recorded', 'node_modules', ...PKG); + const entry = path.join(recorded, 'dist', 'cli-entry.js'); + write(path.join(recorded, 'package.json'), JSON.stringify({ name: PKG.join('/'), version: '2.0.0' })); + write(entry, ECHO); + write(path.join(root, 'squad-home', 'cli.json'), JSON.stringify({ entry })); + expect(parsed(run(HOSTILE, { PATH: [workspaceBin, path.dirname(process.execPath)].join(path.delimiter) }))).toEqual(HOSTILE); + }); + + it.runIf(win)('resolves a workspace npx .bin shim without a shell', () => { + const modules = path.join(root, 'workspace', 'node_modules'); + const workspaceBin = path.join(modules, '.bin'); + cmdShim('npx', workspaceBin); + write(path.join(modules, 'npm', 'bin', 'npx-cli.js'), ECHO); + const out = run(HOSTILE, { + PATH: [workspaceBin, path.dirname(process.execPath)].join(path.delimiter), + SQUAD_ALLOW_NPX: '1', + }); + expect(parsed(out)).toEqual(['-y', `${PKG.join('/')}@2.0.0`, ...HOSTILE]); + }); + }); + it('to SQUAD_CLI given as a script', () => { const entry = path.join(root, 'cli dir', 'cli-entry.js'); write(entry, ECHO); @@ -127,6 +174,7 @@ describe('the launcher passes argv through unchanged', () => { it.runIf(win)('fails closed when a .cmd shim has no JavaScript entry beside it', () => { cmdShim('squad'); + write(path.join(bin, 'node_modules', ...PKG, 'package.json'), JSON.stringify({ name: PKG.join('/'), version: '2.0.0' })); const out = run(['state-mcp']); expect(out.status).toBe(1); expect(out.stdout).not.toContain('SHELL-USED');