From 660bcbd60560eee7f791bed250459d57d4e6fefc Mon Sep 17 00:00:00 2001 From: ework-agent Date: Fri, 2 Oct 2026 10:24:26 +0800 Subject: [PATCH 1/2] fix(update): probe pi-stable in resolvePiBinary + e2e no-op assertion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit resolvePiBinary probed only `pi`(.cmd), but a stock `npm install -g pi-stable` lays down just `pi-stable`(.cmd) — the npm package's declared bin name. Every stock npm install missed the probe and fell back to a bare "pi" spawn that ENOENTs. Probe both names, `pi` first (canonical UX name wins when both exist). Also extend the hermetic npm -g e2e with the no-op branch: after the flip, a second `update` run must exit 0 with "(up to date)", never "Restart to finish", and must not touch package.json. Validated end to end on a real Windows 10 VM against a real `npm i -g pi-stable` agent tree: the pi-managed copy's own `update` run takes the #991 managed branch, drives `pi-stable update --extension npm:billion-context`, and the copy converges 0.1.100 → 0.1.179 with the settings entry untouched. --- src/pi-channel.ts | 15 +++++++++++---- tests/e2e/e2e-registry.test.ts | 11 +++++++++++ tests/update-pi-self-refresh.test.ts | 27 ++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/pi-channel.ts b/src/pi-channel.ts index e657a75cf7..c907510110 100644 --- a/src/pi-channel.ts +++ b/src/pi-channel.ts @@ -58,6 +58,11 @@ export function resolvePiBinary( ): string { const override = env.BILI_PI_BIN?.trim(); if (override && override.length > 0) return override; + // Probe both binary names: `pi` is the canonical UX name (what users and + // distros link), but a stock `npm install -g pi-stable` only lays down + // `pi-stable`(.cmd) — the npm package's declared bin name. Probing just + // `pi` misses every stock npm install. + const probeNames = ["pi", "pi-stable"]; const sep = platform === "win32" ? ";" : ":"; const extensions = platform === "win32" ? (env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").map((e) => e.trim().toLowerCase()).filter((e) => e.length > 0) @@ -69,10 +74,12 @@ export function resolvePiBinary( : [nodeDir, "/usr/local/bin", "/opt/homebrew/bin", "/opt/local/bin", home ? home + "/.local/bin" : ""]; for (const dir of [...(env.PATH ?? "").split(sep), ...extraDirs]) { if (!dir) continue; - for (const ext of extensions) { - const name = ext === "" ? "pi" : `pi${ext}`; - const candidate = dir.endsWith("/") || dir.endsWith("\\") ? dir + name : dir + "/" + name; - if (existsImpl(candidate)) return candidate; + for (const name of probeNames) { + for (const ext of extensions) { + const fileName = ext === "" ? name : `${name}${ext}`; + const candidate = dir.endsWith("/") || dir.endsWith("\\") ? dir + fileName : dir + "/" + fileName; + if (existsImpl(candidate)) return candidate; + } } } return "pi"; diff --git a/tests/e2e/e2e-registry.test.ts b/tests/e2e/e2e-registry.test.ts index 262074b314..203e8254ad 100644 --- a/tests/e2e/e2e-registry.test.ts +++ b/tests/e2e/e2e-registry.test.ts @@ -207,4 +207,15 @@ test("hermetic npm -g install e2e (real npm client, real global layout)", { skip : 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)}`); + + // No-op re-run: already sitting at the registry's latest — must exit + // clean, say so, and touch nothing on disk (no re-download, no churn). + const pkgJson = path.join(installDir, "package.json"); + const mtimeBefore = fs.statSync(pkgJson).mtimeMs; + const noop = runBili(installDir, ["update"], { ...envBase, BILI_UPDATE_REGISTRY: reg.url }); + assert.equal(noop.code, 0, `no-op update failed:\n${noop.stderr}`); + assert.match(noop.stderr, /\(up to date\)/, "no-op update must log the up-to-date line"); + assert.doesNotMatch(noop.stderr, /Restart to finish/, "no-op update must not claim an install happened"); + assert.equal(await readPkgVersion(installDir), NEW_VERSION); + assert.equal(fs.statSync(pkgJson).mtimeMs, mtimeBefore, "no-op update must not touch the installed tree"); }); diff --git a/tests/update-pi-self-refresh.test.ts b/tests/update-pi-self-refresh.test.ts index a1451021c0..67cb0c9462 100644 --- a/tests/update-pi-self-refresh.test.ts +++ b/tests/update-pi-self-refresh.test.ts @@ -21,13 +21,38 @@ const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bili-pi-self process.env.XDG_CACHE_HOME = path.join(root, "cache"); const { refreshPiNpmCopy } = await import("../src/update.ts"); -const { isPiNpmCopy, piNpmEntrySpec, _setPiRunnersForTest } = await import("../src/pi-channel.ts"); +const { isPiNpmCopy, piNpmEntrySpec, resolvePiBinary, _setPiRunnersForTest } = await import("../src/pi-channel.ts"); after(() => { delete process.env.XDG_CACHE_HOME; rmrf(root); }); +test("resolvePiBinary: finds a stock npm install's pi-stable(.cmd), prefers canonical pi, honors BILI_PI_BIN", () => { + const binDir = fs.mkdtempSync(path.join(root, "bindir-")); + const norm = (p: string): string => p.split(path.sep).join("/"); + + // Stock `npm install -g pi-stable` on Windows lays down pi-stable.cmd in + // %APPDATA%/npm — and NOT a `pi` shim. Probing only `pi` misses it. + const appdata = fs.mkdtempSync(path.join(root, "appdata-")); + const npmDir = path.join(appdata, "npm"); + fs.mkdirSync(npmDir); + fs.writeFileSync(path.join(npmDir, "pi-stable.cmd"), "@echo off\r\n"); + const win = resolvePiBinary({ PATH: binDir, APPDATA: appdata, PATHEXT: ".COM;.EXE;.BAT;.CMD" }, "win32", fs.existsSync, "C:\\nodejs\\node.exe"); + assert.equal(norm(win), norm(path.join(npmDir, "pi-stable.cmd"))); + + // Canonical `pi` wins when both exist (users and distros link it). + fs.writeFileSync(path.join(binDir, "pi"), "#!/bin/sh\n"); + const posix = resolvePiBinary({ PATH: binDir }, "linux", fs.existsSync, process.execPath); + assert.equal(norm(posix), norm(path.join(binDir, "pi"))); + + // The override short-circuits everything. + assert.equal(resolvePiBinary({ PATH: binDir, BILI_PI_BIN: "/opt/weird/pi-nightly" }, "linux", fs.existsSync, process.execPath), "/opt/weird/pi-nightly"); + + // Nothing found → the bare name (spawn ENOENT is handled by the caller). + assert.equal(resolvePiBinary({ PATH: binDir }, "linux", () => false, process.execPath), "pi"); +}); + // — isPiNpmCopy classification ———————————————————————————————————— test("isPiNpmCopy: pi npm layouts yes, everything else no", () => { From 19a8ef029a8983f90ee6e4b4057bf7a6d679e9b9 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Fri, 2 Oct 2026 10:43:04 +0800 Subject: [PATCH 2/2] test(update): make resolvePiBinary preference sub-case host-independent The 'canonical pi wins' sub-case called resolvePiBinary({PATH: binDir}, 'linux', fs.existsSync, ...) where binDir was a real mkdtemp path. On a Windows host that path carries a drive-letter colon (C:/Users/...); the 'linux' branch splits PATH on ':' and shreds the single directory into two non-matching fragments, so the resolver returned bare 'pi' and the assert failed. Red on both windows-latest jobs; ubuntu has no drive colons so it stayed green. Rewritten with a synthetic colon-free dir plus a stubbed existsImpl reporting both names present - pins the pi-before-pi-stable probe order without depending on host path shape. Production code unchanged. --- tests/update-pi-self-refresh.test.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/update-pi-self-refresh.test.ts b/tests/update-pi-self-refresh.test.ts index 67cb0c9462..dc61a3437a 100644 --- a/tests/update-pi-self-refresh.test.ts +++ b/tests/update-pi-self-refresh.test.ts @@ -41,10 +41,17 @@ test("resolvePiBinary: finds a stock npm install's pi-stable(.cmd), prefers cano const win = resolvePiBinary({ PATH: binDir, APPDATA: appdata, PATHEXT: ".COM;.EXE;.BAT;.CMD" }, "win32", fs.existsSync, "C:\\nodejs\\node.exe"); assert.equal(norm(win), norm(path.join(npmDir, "pi-stable.cmd"))); - // Canonical `pi` wins when both exist (users and distros link it). - fs.writeFileSync(path.join(binDir, "pi"), "#!/bin/sh\n"); - const posix = resolvePiBinary({ PATH: binDir }, "linux", fs.existsSync, process.execPath); - assert.equal(norm(posix), norm(path.join(binDir, "pi"))); + // Canonical `pi` wins when both exist in one dir: `pi` is probed ahead of + // `pi-stable`. Synthetic dir + stubbed exists keeps this host-independent — + // a real temp path carries a drive-letter ':' on Windows, which the linux + // separator below would shred into non-matching fragments. + const pref = resolvePiBinary( + { PATH: "probedir" }, + "linux", + (c) => c === "probedir/pi" || c === "probedir/pi-stable", + process.execPath, + ); + assert.equal(pref, "probedir/pi"); // The override short-circuits everything. assert.equal(resolvePiBinary({ PATH: binDir, BILI_PI_BIN: "/opt/weird/pi-nightly" }, "linux", fs.existsSync, process.execPath), "/opt/weird/pi-nightly");