diff --git a/.github/workflows/ci-registry.yml b/.github/workflows/ci-registry.yml index 3191747e3b..aa24d3e98a 100644 --- a/.github/workflows/ci-registry.yml +++ b/.github/workflows/ci-registry.yml @@ -11,8 +11,17 @@ concurrency: jobs: e2e-registry: name: hermetic registry e2e (local verdaccio; loopback only, zero secrets) - runs-on: ubuntu-latest - timeout-minutes: 15 + runs-on: ${{ matrix.os }} + timeout-minutes: 25 + strategy: + fail-fast: false + matrix: + # Windows is where real installs live (npm -g shims, NTFS junctions, + # Program Files paths) — validated on a real win10 VM before adding. + os: [ubuntu-latest, windows-latest] + defaults: + run: + shell: bash steps: - uses: actions/checkout@v4 @@ -34,11 +43,12 @@ jobs: if: failure() run: | mkdir -p tmp/e2e-artifacts - for d in tmp/e2e-registry-* tmp/e2e-advisory-*; do [ -d "$d" ] && cp -r "$d" tmp/e2e-artifacts/ || true; done + for d in tmp/e2e-registry-* tmp/e2e-advisory-* tmp/e2e-npm-global-*; do [ -d "$d" ] && cp -r "$d" tmp/e2e-artifacts/ || true; done - uses: actions/upload-artifact@v4 if: failure() + name: e2e-registry-artifacts (with os suffix) with: - name: e2e-registry-artifacts + name: e2e-registry-artifacts-${{ matrix.os }} path: tmp/e2e-artifacts if-no-files-found: ignore diff --git a/tests/e2e/crossplat.ts b/tests/e2e/crossplat.ts new file mode 100644 index 0000000000..f9b6855a6b --- /dev/null +++ b/tests/e2e/crossplat.ts @@ -0,0 +1,114 @@ +// Cross-platform helpers for the hermetic e2e suites (registry, advisory +// rollback, release canary). Windows compatibility notes learned on a real +// win10 VM (#1872 follow-up audit): +// - `execFile("npm")` / `spawnSync("npm")` cannot spawn npm.cmd without a +// shell (ENOENT) — run npm as `node ` resolved next to the +// running node so no shell is involved and argv stays literal. +// - A fully stripped env breaks node children on Windows (SystemRoot is +// required); npm additionally keys its userconfig off USERPROFILE, not +// HOME. Mirror the isolated HOME onto the Windows profile vars. +// - Fake client bins: plain `#!/bin/sh` scripts cannot execute on Windows — +// emit a .cmd shim instead (the product's client probe already routes +// .cmd/.bat through the shell with quoting, see detectOpencodeMajor). +import { execFileSync } from "node:child_process"; +import fs from "node:fs"; +import path from "node:path"; + +export const IS_WIN = process.platform === "win32"; + +/** npm's cli.js next to the running node — the standard layout on every + * platform and CI runner (nodejs dir / hostedtoolcache / nvm symlinks). */ +export function npmCliPath(): string | undefined { + const exeDir = path.dirname(process.execPath); + const candidates = IS_WIN + ? [path.join(exeDir, "node_modules", "npm", "bin", "npm-cli.js")] + : [ + path.join(exeDir, "..", "lib", "node_modules", "npm", "bin", "npm-cli.js"), + path.join(exeDir, "lib", "node_modules", "npm", "bin", "npm-cli.js"), + ]; + for (const c of candidates) if (fs.existsSync(c)) return c; + return undefined; +} + +/** System vars node/npm need even in an isolated env on Windows. */ +export function windowsSystemEnv(): Record { + if (!IS_WIN) return {}; + const keep: Record = {}; + for (const key of ["SystemRoot", "SystemDrive", "ComSpec", "TEMP", "TMP", "PATHEXT", "ProgramFiles", "NUMBER_OF_PROCESSORS", "PROCESSOR_ARCHITECTURE"]) { + const v = process.env[key]; + if (v) keep[key] = v; + } + return keep; +} + +/** Env pointing HOME (and its Windows mirrors) at an isolated `homeDir`. */ +export function npmHomeEnv(homeDir: string): Record { + if (!IS_WIN) return { HOME: homeDir }; + return { + HOME: homeDir, + USERPROFILE: homeDir, + APPDATA: path.join(homeDir, "AppData", "Roaming"), + LOCALAPPDATA: path.join(homeDir, "AppData", "Local"), + }; +} + +/** Isolated HOME/XDG_* (+ Windows mirrors) under `work`, same shape the + * suites used to build inline (POSIX-only before this helper). */ +export function isolatedEnv(work: string): Record { + const env: Record = {}; + const pairs: Array<[string, string]> = [ + ["HOME", "home"], + ["XDG_CONFIG_HOME", "config"], + ["XDG_CACHE_HOME", "cache"], + ["XDG_STATE_HOME", "state"], + ["XDG_DATA_HOME", "data"], + ]; + if (IS_WIN) { + pairs.push( + ["USERPROFILE", "home"], + ["APPDATA", path.join("home", "AppData", "Roaming")], + ["LOCALAPPDATA", path.join("home", "AppData", "Local")], + ); + } + for (const [key, dir] of pairs) { + const p = path.join(work, dir); + fs.mkdirSync(p, { recursive: true }); + env[key] = p; + } + return env; +} + +/** Env for spawning the bili CLI: PATH + isolation + required system vars. */ +export function biliSpawnEnv(iso: Record): Record { + return { PATH: process.env.PATH ?? "", ...iso, ...windowsSystemEnv() }; +} + +/** Run npm synchronously without a shell: `node npm-cli.js args...`. Only + * falls back to the npm shim through a shell when cli.js cannot be located. */ +export function npmRunSync(args: string[], opts: { cwd: string; env: Record }): string { + const env: Record = { ...opts.env, ...windowsSystemEnv() }; + if (opts.env.NPM_ALLOW_DANGEROUS) env.NPM_ALLOW_DANGEROUS = opts.env.NPM_ALLOW_DANGEROUS; + const cli = npmCliPath(); + if (cli) { + return execFileSync(process.execPath, [cli, ...args], { cwd: opts.cwd, encoding: "utf8", timeout: 180_000, env, windowsHide: true }); + } + const line = ["npm", ...args.map((a) => (/\s/.test(a) ? `"${a}"` : a))].join(" "); + return execFileSync(line, { cwd: opts.cwd, encoding: "utf8", timeout: 180_000, shell: true, env, windowsHide: true }); +} + +/** Cross-platform fake client bin that prints `version` for ` + * --version` probes (detectOpencodeMajor & friends). Returns a path the + * product can spawn as-is: shebang script on POSIX, .cmd shim on Windows. */ +export function fakeVersionBin(work: string, name: string, version: string): string { + const dir = path.join(work, "bin"); + fs.mkdirSync(dir, { recursive: true }); + if (!IS_WIN) { + const shim = path.join(dir, name); + fs.writeFileSync(shim, `#!/bin/sh\necho ${version}\n`); + fs.chmodSync(shim, 0o755); + return shim; + } + const cmd = path.join(dir, `${name}.cmd`); + fs.writeFileSync(cmd, `@echo ${version}\r\n`); + return cmd; +} diff --git a/tests/e2e/e2e-advisory-rollback.test.ts b/tests/e2e/e2e-advisory-rollback.test.ts index 4a5d54507e..b4f4f83fe7 100644 --- a/tests/e2e/e2e-advisory-rollback.test.ts +++ b/tests/e2e/e2e-advisory-rollback.test.ts @@ -16,7 +16,6 @@ // Loopback only, zero secrets, zero tokens. Gated like ACP_TEST_REGISTRY so // plain `npm test` stays free; requires `npm run build` first. import { spawn, type ChildProcess } from "node:child_process"; -import { execFileSync } from "node:child_process"; import fs from "node:fs"; import net from "node:net"; import path from "node:path"; @@ -24,6 +23,7 @@ import { strict as assert } from "node:assert"; import { test } from "node:test"; import * as tar from "tar"; import { startRegistry } from "./registry-fixture.js"; +import { biliSpawnEnv, isolatedEnv, npmHomeEnv, npmRunSync } from "./crossplat.ts"; import { rmrf } from "../tmp-rm.ts"; const run = process.env.ACP_TEST_REGISTRY === "1"; @@ -73,26 +73,20 @@ function freePort(): Promise { }); } -function isolatedEnv(work: string): Record { - const env: Record = {}; - for (const [key, dir] of [ - ["HOME", "home"], - ["XDG_CONFIG_HOME", "config"], - ["XDG_CACHE_HOME", "cache"], - ["XDG_STATE_HOME", "state"], - ["XDG_DATA_HOME", "data"], - ] as const) { - const p = path.join(work, dir); - fs.mkdirSync(p, { recursive: true }); - env[key] = p; - } - return env; -} async function readPkgVersion(dir: string): Promise { return (JSON.parse(await fs.promises.readFile(path.join(dir, "package.json"), "utf8")) as { version: string }).version; } +function packTarball(stage: string, packs: string, home: string): string { + const listing = npmRunSync(["pack", "--silent", "--pack-destination", packs], { cwd: stage, env: { PATH: process.env.PATH ?? "", ...npmHomeEnv(home) } }) + .trim() + .split("\n") + .pop() + ?.trim(); + return listing ?? ""; +} + // Stage a publishable tarball of THIS package at a synthetic version (same // shape as e2e-registry.test.ts's helper). async function makeFixtureTarball(work: string, version: string): Promise { @@ -107,17 +101,9 @@ async function makeFixtureTarball(work: string, version: string): Promise { const home = path.join(work, "home-pkg"); const packs = path.join(work, "packs"); fs.mkdirSync(packs, { recursive: true }); - const listing = execFileSync("npm", ["pack", "--silent", "--pack-destination", packs], { - cwd: stage, - encoding: "utf8", - env: { PATH: process.env.PATH ?? "", HOME: home }, - }) - .trim() - .split("\n") - .pop() - ?.trim(); - assert.ok(listing?.endsWith(".tgz"), `npm pack produced no advisory tarball: ${listing}`); - return path.join(packs, listing!); + const listing = packTarball(stage, packs, home); + assert.ok(listing.endsWith(".tgz"), `npm pack produced no advisory tarball: ${listing}`); + return path.join(packs, listing); } // The fake install MUST sit under a node_modules directory (isNpmInstallForm @@ -171,7 +149,7 @@ type BiliProc = { child: ChildProcess; port: number; stderr: string }; function spawnBiliStart(installDir: string, port: number, env: Record): BiliProc { const child = spawn(process.execPath, [path.join(installDir, "dist", "index.js"), "start", "--port", String(port)], { - env: { PATH: process.env.PATH ?? "", ...env }, + env: biliSpawnEnv(env), }); const proc: BiliProc = { child, port, stderr: "" }; child.stderr?.on("data", (d) => (proc.stderr += d)); diff --git a/tests/e2e/e2e-registry.test.ts b/tests/e2e/e2e-registry.test.ts index 1ded864e30..262074b314 100644 --- a/tests/e2e/e2e-registry.test.ts +++ b/tests/e2e/e2e-registry.test.ts @@ -3,13 +3,14 @@ // in-place install → disk flip — plus the post-update opencode plugin entry, // against a local verdaccio. Loopback only, zero secrets, zero tokens. // Gated like ACP_TEST_E2E so plain `npm test` stays free. -import { execFileSync, spawnSync } from "node:child_process"; +import { spawnSync } from "node:child_process"; import fs from "node:fs"; import path from "node:path"; import { strict as assert } from "node:assert"; import { test } from "node:test"; import * as tar from "tar"; import { startRegistry } from "./registry-fixture.js"; +import { IS_WIN, biliSpawnEnv, fakeVersionBin, isolatedEnv, npmHomeEnv, npmRunSync } from "./crossplat.ts"; import { rmrf } from "../tmp-rm.ts"; const run = process.env.ACP_TEST_REGISTRY === "1"; @@ -32,27 +33,11 @@ function escapeRe(s: string): string { return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } -function isolatedEnv(work: string): Record { - const env: Record = {}; - for (const [key, dir] of [ - ["HOME", "home"], - ["XDG_CONFIG_HOME", "config"], - ["XDG_CACHE_HOME", "cache"], - ["XDG_STATE_HOME", "state"], - ["XDG_DATA_HOME", "data"], - ] as const) { - const p = path.join(work, dir); - fs.mkdirSync(p, { recursive: true }); - env[key] = p; - } - return env; -} - function runBili(installDir: string, args: string[], env: Record): { code: number; stdout: string; stderr: string } { const res = spawnSync(process.execPath, [path.join(installDir, "dist", "index.js"), ...args], { encoding: "utf8", timeout: 180_000, - env: { PATH: process.env.PATH ?? "", ...env }, + env: biliSpawnEnv(env), }); return { code: res.status ?? -1, stdout: res.stdout ?? "", stderr: res.stderr ?? "" }; } @@ -63,23 +48,24 @@ async function readPkgVersion(dir: string): Promise { // Stage a publishable tarball of THIS package at a synthetic version: same // files field, same dist build, rewritten package.json version. -async function makeFixtureTarball(work: string, version: string): Promise { +// `bare` strips runtime dependencies — needed when the REAL npm client +// resolves the tree (`install -g`): the hermetic registry has no uplinks, so +// any dependency fetch would 404. The flip/layout mechanics under test +// don't involve dependency resolution. +async function makeFixtureTarball(work: string, version: string, opts?: { bare?: boolean }): Promise { const packs = path.join(work, "packs"); fs.mkdirSync(packs, { recursive: true }); const stage = path.join(work, "fixtures", version); fs.mkdirSync(stage, { recursive: true }); - fs.writeFileSync(path.join(stage, "package.json"), `${JSON.stringify({ ...PKG, version }, null, 2)}\n`); + const stagedPkg = opts?.bare ? { ...PKG, version, dependencies: {}, optionalDependencies: {} } : { ...PKG, version }; + fs.writeFileSync(path.join(stage, "package.json"), `${JSON.stringify(stagedPkg, null, 2)}\n`); for (const entry of PKG.files) { const src = path.join(REPO_ROOT, entry); if (fs.existsSync(src)) await fs.promises.cp(src, path.join(stage, entry), { recursive: true }); } const home = path.join(work, "home-pkg"); fs.mkdirSync(home, { recursive: true }); - const listing = execFileSync("npm", ["pack", "--silent", "--pack-destination", packs], { - cwd: stage, - encoding: "utf8", - env: { PATH: process.env.PATH ?? "", HOME: home }, - }) + const listing = npmRunSync(["pack", "--silent", "--pack-destination", packs], { cwd: stage, env: { PATH: process.env.PATH ?? "", ...npmHomeEnv(home) } }) .trim() .split("\n") .pop() @@ -110,13 +96,10 @@ function seedOpencodeConfig(work: string): void { // Deterministic host-major probe target: prints a 2.x version so // detectOpencodeMajor() lands on key "plugins" regardless of what (if -// anything) is installed on the host. +// anything) is installed on the host. Cross-platform: .cmd shim on Windows +// (the product routes .cmd through the shell), shebang script on POSIX. function fakeOpencodeBin(work: string): string { - const bin = path.join(work, "bin", "fake-opencode"); - fs.mkdirSync(path.dirname(bin), { recursive: true }); - fs.writeFileSync(bin, "#!/bin/sh\necho 2.4.0\n"); - fs.chmodSync(bin, 0o755); - return bin; + return fakeVersionBin(work, "fake-opencode", "2.4.0"); } test("hermetic registry e2e", { skip: skipReason }, async (t) => { @@ -180,3 +163,48 @@ test("hermetic registry e2e", { skip: skipReason }, async (t) => { assert.match(res.stdout, /plugins present/); }); }); + +test("hermetic npm -g install e2e (real npm client, real global layout)", { skip: skipReason }, async (t) => { + assert.ok(fs.existsSync(DIST_ENTRY), "dist/index.js missing — run `npm run build` first"); + const workRoot = path.join(process.cwd(), "tmp"); + fs.mkdirSync(workRoot, { recursive: true }); + const work = fs.mkdtempSync(path.join(workRoot, "e2e-npm-global-")); + t.after(() => rmrf(work)); + + const reg = await startRegistry(path.join(work, "registry")); + t.after(() => reg.stop()); + await reg.publish(await makeFixtureTarball(work, OLD_VERSION, { bare: true })); + await reg.publish(await makeFixtureTarball(work, NEW_VERSION, { bare: true })); + const envBase = isolatedEnv(work); + + // The REAL npm client installing globally into an isolated prefix — not + // the manual tar extraction the flip test above uses. This is the layout + // real users get from `npm install -g billion-context`, per platform: + // win32: /node_modules/ + /bili.cmd shim + // POSIX: /lib/node_modules/ + /bin/bili shim + const prefix = path.join(work, "npm-global"); + fs.mkdirSync(prefix, { recursive: true }); + await reg.npm(["install", "--global", `${PKG.name}@${OLD_VERSION}`, "--prefix", prefix]); + const installDir = fs.existsSync(path.join(prefix, "node_modules", PKG.name)) + ? path.join(prefix, "node_modules", PKG.name) + : path.join(prefix, "lib", "node_modules", PKG.name); + assert.equal(await readPkgVersion(installDir), OLD_VERSION, `npm -g did not lay the package at ${installDir}`); + const shim = IS_WIN ? path.join(prefix, "bili.cmd") : path.join(prefix, "bin", "bili"); + assert.ok(fs.existsSync(shim), `npm -g must lay the bili shim at ${shim} for this platform`); + + // Self-update must classify the npm-global layout as bili-owned + // (hostManagedInstall) and flip it in place over the real chain. + const res = runBili(installDir, ["update"], { ...envBase, BILI_UPDATE_REGISTRY: reg.url }); + assert.equal(res.code, 0, `bili update failed:\n${res.stderr}`); + assert.match(res.stderr, new RegExp(`installed ${escapeRe(OLD_VERSION)} → ${escapeRe(NEW_VERSION)}\. Restart to finish\.`)); + assert.equal(await readPkgVersion(installDir), NEW_VERSION, "npm-global on-disk version must flip to the published one"); + + // The shim npm laid down still resolves through to the flipped tree. + // (.cmd shims cannot be spawned without a shell on Windows — route them + // through cmd like the product's own client probe does.) + const probe = IS_WIN + ? spawnSync(`"${shim}" --version`, { shell: true, encoding: "utf8", timeout: 60_000, env: biliSpawnEnv(envBase) }) + : spawnSync(shim, ["--version"], { encoding: "utf8", timeout: 60_000, env: biliSpawnEnv(envBase) }); + const shimVersion = (probe.stdout ?? "").trim(); + assert.match(shimVersion, new RegExp(`^v?${escapeRe(NEW_VERSION)}$`), `bili shim must report the flipped version, got: ${JSON.stringify(shimVersion)}`); +}); diff --git a/tests/e2e/registry-fixture.ts b/tests/e2e/registry-fixture.ts index 9db15494a9..b92d52d7d2 100644 --- a/tests/e2e/registry-fixture.ts +++ b/tests/e2e/registry-fixture.ts @@ -10,6 +10,7 @@ import net from "node:net"; import fs from "node:fs"; import path from "node:path"; import { assertPortDead } from "../port-race.js"; +import { npmCliPath, npmHomeEnv, windowsSystemEnv } from "./crossplat.ts"; export interface RegistryFixture { /** Base URL, e.g. http://127.0.0.1:43210 */ @@ -130,10 +131,17 @@ export async function startRegistry(root: string): Promise { const runNpm = (args: string[]): Promise<{ stdout: string; stderr: string }> => new Promise((resolve, reject) => { + // Windows: npm.cmd cannot be spawned without a shell — run + // `node npm-cli.js` instead (argv stays literal, no quoting). + // Shell fallback passes the whole command as the file string: + // with shell:true, execFile prepends file to its args (`npm npm pack …`). + const cli = npmCliPath(); + const tail = [...args, "--registry", url, "--no-audit", "--no-fund"]; + const shellLine = `npm ${tail.map((a) => (/\s/.test(a) ? `"${a}"` : a)).join(" ")}`; execFile( - "npm", - [...args, "--registry", url, "--no-audit", "--no-fund"], - { cwd: root, encoding: "utf8", timeout: NPM_TIMEOUT_MS, env: { PATH: process.env.PATH ?? "", HOME: homeDir, ...(process.env.NPM_ALLOW_DANGEROUS ? { NPM_ALLOW_DANGEROUS: process.env.NPM_ALLOW_DANGEROUS } : {}) } }, + cli ? process.execPath : shellLine, + cli ? [cli, ...tail] : [], + { cwd: root, encoding: "utf8", timeout: NPM_TIMEOUT_MS, shell: cli ? false : true, windowsHide: true, env: { PATH: process.env.PATH ?? "", ...npmHomeEnv(homeDir), ...windowsSystemEnv(), ...(process.env.NPM_ALLOW_DANGEROUS ? { NPM_ALLOW_DANGEROUS: process.env.NPM_ALLOW_DANGEROUS } : {}) } }, (error, stdout, stderr) => { if (error) reject(new Error(`npm ${args.join(" ")} failed: ${(stderr || error.message).slice(0, 4000)}`)); else resolve({ stdout, stderr }); @@ -141,7 +149,7 @@ export async function startRegistry(root: string): Promise { ); }); - return { url, port, homeDir, root, stop, publish: (tgz) => runNpm(["publish", tgz]), npm: runNpm }; + return { url, port, homeDir, root, stop, publish: async (tgz) => { await runNpm(["publish", tgz]); }, npm: runNpm }; } catch (err) { await stop(); throw err;