Files
Maximo GukandClaude Opus 5 3562627ed0 Fix nested pnpm spawns on Windows so run-local and dev-server start (#235)
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>
2026-08-17 18:57:31 -05:00

52 lines
2.5 KiB
TypeScript

import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { pnpmCommand } from "./pnpm-command.ts";
// Real `npm_execpath` values, as measured on Windows: pnpm installed through npm, pnpm installed
// standalone, and -- the case the guard exists for -- the same variable under `npm run`.
const PNPM_MJS = "C:\\nvm4w\\nodejs\\node_modules\\pnpm\\bin\\pnpm.mjs";
const PNPM_CJS = "C:\\Users\\dev\\AppData\\Local\\pnpm\\bin\\pnpm.cjs";
const NPM_CLI = "C:\\Users\\dev\\AppData\\Local\\nvm\\node_modules\\npm\\bin\\npm-cli.js";
describe("pnpmCommand", () => {
it("runs pnpm's own entry point through node on Windows", () => {
const [command, args] = pnpmCommand(["install"], { npm_execpath: PNPM_MJS }, "win32");
assert.equal(command, process.execPath);
assert.deepEqual(args, [PNPM_MJS, "install"]);
});
it("recognises a .cjs entry as well as .mjs", () => {
const [command, args] = pnpmCommand(["install"], { npm_execpath: PNPM_CJS }, "win32");
assert.equal(command, process.execPath);
assert.deepEqual(args, [PNPM_CJS, "install"]);
});
// The reason `shell: true` was not an option: a shell would split this argument in two.
it("passes arguments through untouched, including one containing a space", () => {
const configPath = "C:\\Users\\Some Name\\cloudflare-os\\wrangler.jsonc";
const [, args] = pnpmCommand(
["exec", "wrangler", "dev", "-c", configPath], { npm_execpath: PNPM_MJS }, "win32");
assert.deepEqual(args, [PNPM_MJS, "exec", "wrangler", "dev", "-c", configPath]);
});
// Substituting this unchecked would run `npm install` against a pnpm workspace.
it("rejects npm's CLI rather than using the wrong package manager", () => {
assert.deepEqual(
pnpmCommand(["install"], { npm_execpath: NPM_CLI }, "win32"), ["pnpm", ["install"]]);
});
// A direct `node scripts/run-local.ts` has no pnpm ancestor to inherit the variable from. Nothing
// can be substituted, so the call keeps whatever behaviour it has today.
it("falls back to bare pnpm when npm_execpath is absent or empty", () => {
assert.deepEqual(pnpmCommand(["install"], {}, "win32"), ["pnpm", ["install"]]);
assert.deepEqual(pnpmCommand(["install"], { npm_execpath: "" }, "win32"), ["pnpm", ["install"]]);
});
it("leaves other platforms exactly as they were", () => {
assert.deepEqual(
pnpmCommand(["install"], { npm_execpath: PNPM_MJS }, "linux"), ["pnpm", ["install"]]);
assert.deepEqual(
pnpmCommand(["install"], { npm_execpath: PNPM_MJS }, "darwin"), ["pnpm", ["install"]]);
});
});