Files
Ryan Duguid 9a399b28f6 fix(scripts): run npm through its Node entry point in the MCP package check
scripts/test-mcp-package.mjs spawned "npm" without a shell, which fails on
Windows with spawn npm ENOENT because npm is a .cmd shim there. Under
"npm run test:mcp" the npm CLI is known: run process.execPath with the
npm-cli.js entry point from npm_execpath, and fall back to "npm" on PATH
elsewhere. A direct Windows run without that entry point now explains the
supported command instead of failing later.

The selection lives in scripts/lib/npm-command.mjs so the unit tests import
it directly. It recognises the npm CLI by its entry-point filename, parsed
with the path rules of the platform under test, rather than by the
configurable npm_config_user_agent. Tests cover npm on both platforms,
paths with spaces, a customised user agent, a direct Windows run, an empty
npm_execpath and other package managers.

(cherry picked from commit 2b4ac4ac0c)
2026-10-22 19:42:44 -07:00

25 lines
1.1 KiB
JavaScript

import { posix, win32 } from 'node:path';
/**
* Choose how to run npm from a script started by an npm lifecycle command.
*
* child_process cannot spawn the "npm" .cmd shim on Windows without a shell.
* Under "npm run" the npm CLI is known through npm_execpath, so run its
* npm-cli.js entry point with the current Node executable. Elsewhere fall back
* to "npm" on PATH. A direct Windows run without that entry point is not
* supported here, so name the supported command instead of failing later
* with ENOENT.
*
* @param {{ platform: string, env: NodeJS.ProcessEnv, execPath: string }} runtime
* @returns {{ file: string, args: string[] }} executable and leading arguments
*/
export function selectNpmCommand({ platform, env, execPath }) {
const pathApi = platform === 'win32' ? win32 : posix;
const execpath = env.npm_execpath ?? '';
const npmCli = pathApi.basename(execpath) === 'npm-cli.js' ? execpath : undefined;
if (platform === 'win32' && !npmCli) {
throw new Error('Run the MCP package check with "npm run test:mcp" on Windows.');
}
return npmCli ? { file: execPath, args: [npmCli] } : { file: 'npm', args: [] };
}