Skip to content

Commit 185b08c

Browse files
authored
Merge pull request #2545 from github/squad/2538-codeql-workflows
fix: resolve Windows workspace launcher shims
2 parents 3138524 + 7226263 commit 185b08c

4 files changed

Lines changed: 62 additions & 6 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@bradygaster/squad-cli": patch
3+
"@bradygaster/squad-sdk": patch
4+
---
5+
6+
Fix Windows launcher resolution for workspace-local Squad and npx shims.

‎packages/squad-sdk/src/client/adapters/claude-plugin.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,10 @@ function withoutShell(command, prefix) {
300300
if (!win || !/\\.(cmd|bat)$/i.test(command)) return [command, prefix];
301301
const shim = path.isAbsolute(command) ? command : onPath(path.basename(command, path.extname(command)));
302302
const dir = shim ? path.dirname(shim) : path.dirname(process.execPath);
303+
const modules = path.basename(dir).toLowerCase() === '.bin' ? path.dirname(dir) : path.join(dir, 'node_modules');
303304
const candidates = path.basename(command).toLowerCase() === 'npx.cmd'
304-
? [path.join(dir, 'node_modules/npm/bin/npx-cli.js'), path.join(path.dirname(process.execPath), 'node_modules/npm/bin/npx-cli.js')]
305-
: [path.join(dir, 'node_modules', ...PKG.split('/'), 'dist/cli-entry.js')];
305+
? [path.join(modules, 'npm/bin/npx-cli.js'), path.join(path.dirname(process.execPath), 'node_modules/npm/bin/npx-cli.js')]
306+
: [path.join(modules, ...PKG.split('/'), 'dist/cli-entry.js')];
306307
const entry = candidates.find((candidate) => fs.existsSync(candidate));
307308
if (!entry) throw new Error(\`Cannot resolve the JavaScript entry for \${command}; set SQUAD_CLI to cli-entry.js\`);
308309
return [process.execPath, [entry, ...prefix]];

‎plugins/squad/scripts/squad.mjs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,10 @@ function withoutShell(command, prefix) {
187187
if (!win || !/\.(cmd|bat)$/i.test(command)) return [command, prefix];
188188
const shim = path.isAbsolute(command) ? command : onPath(path.basename(command, path.extname(command)));
189189
const dir = shim ? path.dirname(shim) : path.dirname(process.execPath);
190+
const modules = path.basename(dir).toLowerCase() === '.bin' ? path.dirname(dir) : path.join(dir, 'node_modules');
190191
const candidates = path.basename(command).toLowerCase() === 'npx.cmd'
191-
? [path.join(dir, 'node_modules/npm/bin/npx-cli.js'), path.join(path.dirname(process.execPath), 'node_modules/npm/bin/npx-cli.js')]
192-
: [path.join(dir, 'node_modules', ...PKG.split('/'), 'dist/cli-entry.js')];
192+
? [path.join(modules, 'npm/bin/npx-cli.js'), path.join(path.dirname(process.execPath), 'node_modules/npm/bin/npx-cli.js')]
193+
: [path.join(modules, ...PKG.split('/'), 'dist/cli-entry.js')];
193194
const entry = candidates.find((candidate) => fs.existsSync(candidate));
194195
if (!entry) throw new Error(`Cannot resolve the JavaScript entry for ${command}; set SQUAD_CLI to cli-entry.js`);
195196
return [process.execPath, [entry, ...prefix]];

‎test/launcher-argv.test.ts‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ function write(file: string, body: string, mode?: number): void {
6262
}
6363

6464
/** A shim that must never run: under cmd.exe it prints SHELL-USED. */
65-
function cmdShim(name: string): void {
66-
write(path.join(bin, `${name}.cmd`), '@echo SHELL-USED %*\r\n');
65+
function cmdShim(name: string, dir = bin): void {
66+
write(path.join(dir, `${name}.cmd`), '@echo SHELL-USED %*\r\n');
6767
}
6868

6969
function run(args: string[], env: Record<string, string> = {}) {
@@ -112,6 +112,53 @@ describe('the launcher passes argv through unchanged', () => {
112112
expect(parsed(run(HOSTILE, { SQUAD_ALLOW_NPX: '1' }))).toEqual(['-y', `${PKG.join('/')}@2.0.0`, ...HOSTILE]);
113113
});
114114

115+
describe('when a .cmd shim has no JavaScript entry beside it', () => {
116+
it.runIf(win)('resolves a workspace .bin shim to its sibling package without a shell', () => {
117+
const modules = path.join(root, 'workspace', 'node_modules');
118+
const workspaceBin = path.join(modules, '.bin');
119+
cmdShim('squad', workspaceBin);
120+
const pkg = path.join(modules, ...PKG);
121+
write(path.join(pkg, 'package.json'), JSON.stringify({ name: PKG.join('/'), version: '2.0.0' }));
122+
write(path.join(pkg, 'dist', 'cli-entry.js'), ECHO);
123+
expect(parsed(run(HOSTILE, { PATH: [workspaceBin, path.dirname(process.execPath)].join(path.delimiter) }))).toEqual(HOSTILE);
124+
});
125+
126+
it.runIf(win)('resolves an explicitly configured workspace .bin shim', () => {
127+
const modules = path.join(root, 'workspace', 'node_modules');
128+
const workspaceBin = path.join(modules, '.bin');
129+
cmdShim('squad', workspaceBin);
130+
write(path.join(modules, ...PKG, 'dist', 'cli-entry.js'), ECHO);
131+
expect(parsed(run(HOSTILE, { SQUAD_CLI: path.join(workspaceBin, 'squad.cmd') }))).toEqual(HOSTILE);
132+
});
133+
134+
it.runIf(win)('skips an outdated workspace .bin shim in favor of a recorded CLI', () => {
135+
const modules = path.join(root, 'workspace', 'node_modules');
136+
const workspaceBin = path.join(modules, '.bin');
137+
cmdShim('squad', workspaceBin);
138+
const pkg = path.join(modules, ...PKG);
139+
write(path.join(pkg, 'package.json'), JSON.stringify({ name: PKG.join('/'), version: '1.0.0' }));
140+
write(path.join(pkg, 'dist', 'cli-entry.js'), "throw new Error('outdated CLI selected');\n");
141+
const recorded = path.join(root, 'recorded', 'node_modules', ...PKG);
142+
const entry = path.join(recorded, 'dist', 'cli-entry.js');
143+
write(path.join(recorded, 'package.json'), JSON.stringify({ name: PKG.join('/'), version: '2.0.0' }));
144+
write(entry, ECHO);
145+
write(path.join(root, 'squad-home', 'cli.json'), JSON.stringify({ entry }));
146+
expect(parsed(run(HOSTILE, { PATH: [workspaceBin, path.dirname(process.execPath)].join(path.delimiter) }))).toEqual(HOSTILE);
147+
});
148+
149+
it.runIf(win)('resolves a workspace npx .bin shim without a shell', () => {
150+
const modules = path.join(root, 'workspace', 'node_modules');
151+
const workspaceBin = path.join(modules, '.bin');
152+
cmdShim('npx', workspaceBin);
153+
write(path.join(modules, 'npm', 'bin', 'npx-cli.js'), ECHO);
154+
const out = run(HOSTILE, {
155+
PATH: [workspaceBin, path.dirname(process.execPath)].join(path.delimiter),
156+
SQUAD_ALLOW_NPX: '1',
157+
});
158+
expect(parsed(out)).toEqual(['-y', `${PKG.join('/')}@2.0.0`, ...HOSTILE]);
159+
});
160+
});
161+
115162
it('to SQUAD_CLI given as a script', () => {
116163
const entry = path.join(root, 'cli dir', 'cli-entry.js');
117164
write(entry, ECHO);
@@ -127,6 +174,7 @@ describe('the launcher passes argv through unchanged', () => {
127174

128175
it.runIf(win)('fails closed when a .cmd shim has no JavaScript entry beside it', () => {
129176
cmdShim('squad');
177+
write(path.join(bin, 'node_modules', ...PKG, 'package.json'), JSON.stringify({ name: PKG.join('/'), version: '2.0.0' }));
130178
const out = run(['state-mcp']);
131179
expect(out.status).toBe(1);
132180
expect(out.stdout).not.toContain('SHELL-USED');

0 commit comments

Comments
 (0)