mirror of
https://github.com/cloudflare/cloudflare-os.git
synced 2026-11-25 02:38:04 +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>
66 lines
3.2 KiB
TypeScript
66 lines
3.2 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { dirname, join } from "node:path";
|
|
import { after, describe, it } from "node:test";
|
|
import { resolveBinEntry } from "./bin-entry.ts";
|
|
|
|
// A fresh temp directory per run: `node --test` runs files in parallel, and anything written inside
|
|
// the repo would land in `run-local`'s source hash. Torn down even when a test fails. Realpathed
|
|
// because resolveBinEntry is, and on macOS `tmpdir()` is a symlink (/var -> /private/var).
|
|
const root = realpathSync(mkdtempSync(join(tmpdir(), "bin-entry-")));
|
|
after(() => rmSync(root, { recursive: true, force: true }));
|
|
|
|
let caseCount = 0;
|
|
|
|
// A package directory holding `node_modules/<name>` with the given manifest, plus each named entry
|
|
// file. `entries` is separate from `bin` so a manifest can point at a file that is not there.
|
|
function pkgWith(name: string, bin: unknown, entries: string[]): string {
|
|
const pkgDir = join(root, `case-${++caseCount}`);
|
|
const installed = join(pkgDir, "node_modules", name);
|
|
mkdirSync(installed, { recursive: true });
|
|
writeFileSync(join(installed, "package.json"), JSON.stringify({ name, bin }));
|
|
for (const entry of entries) {
|
|
const file = join(installed, entry);
|
|
mkdirSync(dirname(file), { recursive: true });
|
|
writeFileSync(file, "");
|
|
}
|
|
return pkgDir;
|
|
}
|
|
|
|
describe("resolveBinEntry", () => {
|
|
it("resolves a string `bin` to an absolute entry path", () => {
|
|
const pkgDir = pkgWith("vite", "bin/vite.js", ["bin/vite.js"]);
|
|
assert.equal(
|
|
resolveBinEntry(pkgDir, "vite"),
|
|
join(pkgDir, "node_modules", "vite", "bin", "vite.js"));
|
|
});
|
|
|
|
it("resolves the matching key of an object `bin`", () => {
|
|
const pkgDir = pkgWith("wrangler", { wrangler: "./main.js", other: "./other.js" }, ["main.js"]);
|
|
assert.equal(
|
|
resolveBinEntry(pkgDir, "wrangler"),
|
|
join(pkgDir, "node_modules", "wrangler", "main.js"));
|
|
});
|
|
|
|
// The case that broke the `shell: true` attempt at this bug: a checkout under a path with a space
|
|
// is re-split by the shell, so the entry has to reach the caller as one intact argv element.
|
|
it("resolves from a package directory whose path contains a space", () => {
|
|
const spaced = join(root, "cf os", "repo");
|
|
const installed = join(spaced, "node_modules", "vite");
|
|
mkdirSync(installed, { recursive: true });
|
|
writeFileSync(join(installed, "package.json"), JSON.stringify({ name: "vite", bin: "./cli.js" }));
|
|
writeFileSync(join(installed, "cli.js"), "");
|
|
assert.equal(resolveBinEntry(spaced, "vite"), join(installed, "cli.js"));
|
|
});
|
|
|
|
// Callers treat null as "leave the command as written", so a wrong guess must never be returned.
|
|
it("returns null when the bin cannot be resolved to a file that exists", () => {
|
|
assert.equal(resolveBinEntry(join(root, "nonexistent"), "vite"), null);
|
|
assert.equal(
|
|
resolveBinEntry(pkgWith("vite", { other: "./other.js" }, ["other.js"]), "vite"), null);
|
|
assert.equal(resolveBinEntry(pkgWith("vite", undefined, []), "vite"), null);
|
|
assert.equal(resolveBinEntry(pkgWith("vite", "./missing.js", []), "vite"), null);
|
|
});
|
|
});
|