Skip to content

Commit cdc2df3

Browse files
committed
fix(cli): handle pnpm_execpath binaries
(cherry picked from commit 3674d44a8f3c171a176aadc7f9f10624dab9fc4a)
1 parent 91a6846 commit cdc2df3

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import path from 'node:path';
2+
3+
import { describe, expect, test } from '@voidzero-dev/vite-plus-test';
4+
5+
import { getPnpmInvocation } from '../local-cli.ts';
6+
7+
describe('getPnpmInvocation()', () => {
8+
test('falls back to pnpm binary when npm_execpath is unset', () => {
9+
const expectedCommand = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm';
10+
11+
expect(getPnpmInvocation(undefined)).toEqual({
12+
command: expectedCommand,
13+
args: [],
14+
});
15+
});
16+
17+
test('runs pnpm through node when npm_execpath points to a JS script', () => {
18+
const execPath = path.join('/tmp', 'pnpm.cjs');
19+
20+
expect(getPnpmInvocation(execPath)).toEqual({
21+
command: process.execPath,
22+
args: [execPath],
23+
});
24+
});
25+
26+
test('runs pnpm directly when npm_execpath points to a native binary', () => {
27+
const execPath = path.join('/home/runner/setup-pnpm/node_modules/.bin/store', 'pnpm');
28+
29+
expect(getPnpmInvocation(execPath)).toEqual({
30+
command: execPath,
31+
args: [],
32+
});
33+
});
34+
});

packages/tools/src/local-cli.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,30 @@ function runCommand(step: string, command: string, args: string[], options: Comm
295295
process.exit(result.status ?? 1);
296296
}
297297

298+
export function getPnpmInvocation(execPath: string | undefined) {
299+
if (!execPath) {
300+
return {
301+
command: pnpmBin,
302+
args: [] as string[],
303+
};
304+
}
305+
306+
const ext = path.extname(execPath);
307+
if (ext === '.js' || ext === '.cjs' || ext === '.mjs') {
308+
return {
309+
command: process.execPath,
310+
args: [execPath],
311+
};
312+
}
313+
314+
return {
315+
command: execPath,
316+
args: [] as string[],
317+
};
318+
}
319+
298320
function runPnpmCommand(step: string, args: string[], options: CommandOptions = {}) {
299-
const baseArgs = pnpmExecPath ? [pnpmExecPath] : [];
300-
const command = pnpmExecPath ? process.execPath : pnpmBin;
321+
const { command, args: baseArgs } = getPnpmInvocation(pnpmExecPath);
301322

302323
runCommand(step, command, [...baseArgs, ...args], options);
303324
}

0 commit comments

Comments
 (0)