mirror of
https://github.com/cloudflare/cloudflare-os.git
synced 2026-11-23 01:58:45 +08:00
On Windows the `pnpm` on PATH is a `.cmd` shim. Node spawns processes directly rather than through a shell, so there is no extensionless `pnpm` to execute (ENOENT), and naming the shim explicitly fails too since the CVE-2024-27980 fix (EINVAL). `shell: true` is not a fix here: a shell re-splits the command line, and these callers pass absolute paths built from the checkout location, which break apart on a path containing a space. Add scripts/pnpm-command.ts, which runs pnpm's own JS entry point under `node` when `npm_execpath` points at it, so argv survives byte-for-byte with no shell. Guarded on the path, because under `npm run` that variable points at npm and substituting it unchecked would run npm against a pnpm workspace. Where pnpm is a native executable the plain `pnpm` fallback still resolves it, since PATH lookup appends `.exe` but not `.cmd`. That alone is not enough for the `pnpm exec` sites: Vite+ runs task commands with a filtered environment that omits `npm_execpath`, so build-app.mjs has no way back to pnpm at all. Extract the existing resolveBinEntry() out of run-dev-server.ts into scripts/bin-entry.ts and reach those binaries directly, which also skips ~0.33s of pnpm startup per call. Verified on Windows 11 / Node 24.19.0 / pnpm 11.17.0: `pnpm run-local` goes from failing at the first `pnpm install` to serving HTTP 200 on 8787. Fixes #19 Fixes #120 Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
40 lines
2.0 KiB
TypeScript
40 lines
2.0 KiB
TypeScript
// Spawn pnpm from a script without going through a shell.
|
|
//
|
|
// On Windows the `pnpm` on PATH is a `.cmd` shim. Node spawns processes directly rather than through
|
|
// a shell, so there is no extensionless `pnpm` to execute and the call fails with ENOENT. Naming the
|
|
// shim explicitly does not help either: since the fix for CVE-2024-27980 Node refuses to spawn
|
|
// `.cmd`/`.bat` without a shell and fails with EINVAL instead.
|
|
//
|
|
// `shell: true` makes the call work but is not safe here. A shell re-splits the command line, and
|
|
// the callers pass absolute paths built from the checkout location -- on a checkout whose path
|
|
// contains a space (`C:\Users\Some Name\...`) those arguments break apart.
|
|
//
|
|
// `npm_execpath` is the way out: under `pnpm run` it holds pnpm's own JS entry point, which `node`
|
|
// executes directly with no shell, so every argument keeps its exact value. Under `npm run` the same
|
|
// variable points at npm's CLI instead, so it is checked before being used -- substituting it
|
|
// unchecked would silently run `npm install` against a pnpm workspace.
|
|
|
|
// pnpm's JS entry, as `npm_execpath` spells it (`.cjs` or `.mjs` depending on how pnpm was
|
|
// installed). npm's `npm-cli.js` and the standalone `pnpm.exe`/`pnpm.cmd` shims do not match.
|
|
const PNPM_JS_ENTRY = /[\\/]pnpm\.[cm]?js$/i;
|
|
|
|
/**
|
|
* `[command, args]` for running pnpm with `args`, to hand to `spawn`, `spawnSync` or `execFileSync`.
|
|
*
|
|
* Off Windows, and on Windows whenever the launcher was not pnpm, this is a plain `["pnpm", args]`:
|
|
* an entry path this cannot support keeps today's loud ENOENT rather than quietly reaching for a
|
|
* different package manager.
|
|
*
|
|
* `env` and `platform` are parameters only so the Windows branch stays testable on other platforms.
|
|
*/
|
|
export function pnpmCommand(
|
|
args: string[],
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
platform: NodeJS.Platform = process.platform,
|
|
): [string, string[]] {
|
|
const execPath = env.npm_execpath ?? "";
|
|
return platform === "win32" && PNPM_JS_ENTRY.test(execPath)
|
|
? [process.execPath, [execPath, ...args]]
|
|
: ["pnpm", args];
|
|
}
|