Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fix-windows-workspace-shim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bradygaster/squad-cli": patch
"@bradygaster/squad-sdk": patch
---

Fix Windows launcher resolution for workspace-local Squad and npx shims.
5 changes: 3 additions & 2 deletions packages/squad-sdk/src/client/adapters/claude-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
Expand Down
5 changes: 3 additions & 2 deletions plugins/squad/scripts/squad.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
Expand Down
52 changes: 50 additions & 2 deletions test/launcher-argv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {}) {
Expand Down Expand Up @@ -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);
Expand All @@ -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');
Expand Down
Loading