diff --git a/src/dsh-channel.ts b/src/dsh-channel.ts index b41ea06211..2673ee0e13 100644 --- a/src/dsh-channel.ts +++ b/src/dsh-channel.ts @@ -16,6 +16,10 @@ import { promisify } from "node:util"; import { resolveDshHome } from "./client-config.js"; export const DSH_PACKAGE = "billion-context"; +// The profile owned by dsh's own desktop app. Its billion-context copy is +// updated in place by bili itself (#1575 owner decision — the CLI refuses this +// profile by design), so every channel-side refresh path must skip it. +export const DSH_DESKTOP_PROFILE = "desktop"; const DSH_EXEC_TIMEOUT_MS = 5 * 60 * 1000; // cold pnpm store + slow network @@ -411,6 +415,13 @@ export async function refreshDshProfileBundles( log("info", `[update] dsh profile ${name}: billion-context pinned to ${spec} (local source) — leaving it alone`); continue; } + if (name === DSH_DESKTOP_PROFILE) { + // #1575 owner decision: the CLI refuses --profile desktop by design, and the + // in-app plugin manager cannot be relied on — bili owns that copy in place, + // driven by update.ts refreshDshDesktopCopy alongside every call site of + // this function. Spawning the CLI here would just log a guaranteed failure. + continue; + } if (installedProfileVersion(dir) === targetVersion) continue; // #1803: already in step try { await runDshPluginAsync(["plugin", "--profile", name, "add", `${DSH_PACKAGE}@${targetVersion}`], env); diff --git a/src/update.ts b/src/update.ts index 50193f15b4..1fb4c5dfc9 100644 --- a/src/update.ts +++ b/src/update.ts @@ -20,7 +20,7 @@ * version and stops trying. No notified Set — failed installs retry next * cycle automatically. */ -import { readFile, writeFile, mkdir, access, constants, rm, cp, unlink } from "node:fs/promises"; +import { readFile, writeFile, mkdir, access, constants, rm, cp, unlink, lstat, rename } from "node:fs/promises"; import { execFile } from "node:child_process"; import { realpathSync } from "node:fs"; import crypto from "node:crypto"; @@ -30,7 +30,7 @@ import os from "node:os"; import { fileURLToPath } from "node:url"; import { cacheDir } from "./paths.js"; import { log as loggerLog, type Logger } from "./logger.js"; -import { refreshDshProfileBundles, isDshProfileCopy, dshProfileDirs, dshProfileDependsOnBili, dshProfileDepSpec, isRegistryDepSpec, DSH_PACKAGE } from "./dsh-channel.js"; +import { refreshDshProfileBundles, isDshProfileCopy, dshProfileDirs, dshProfileDependsOnBili, dshProfileDepSpec, isRegistryDepSpec, DSH_PACKAGE, DSH_DESKTOP_PROFILE } from "./dsh-channel.js"; import { isPiNpmCopy, piNpmEntrySpec, runPiAsync, PI_NPM_SPEC } from "./pi-channel.js"; import { resolveDshHome, resolveKimiHome, resolveOmpHome, resolvePiHome } from "./client-config.js"; import { proxyDispatcher } from "./upstream-proxy.js"; @@ -270,6 +270,23 @@ export interface HostManagedInstall { * store. Returns the owner + its update channel, or undefined when the copy * is bili-owned (npm global, manual install) and may be updated in place. * Exported for tests. */ +// #1575 (owner decision 2026-10-01, recorded on the issue): the dsh DESKTOP +// profile copy is bili-owned IN PLACE. Every other update channel for that +// lane is closed by design upstream — the dsh CLI refuses `--profile desktop` +// outright ("managed exclusively by the Electron application") and the +// in-app plugin manager does not reliably pull newer bundled deps — so the +// global self-update / periodic check is the only working path and it must be +// allowed to overwrite in place. Residual second-writer risk with the app's +// own manager is explicitly accepted by the owner; reverting means dropping +// this check. +function isDshDesktopBiliCopy(installDir: string, real: string, env: NodeJS.ProcessEnv): boolean { + const anchor = path.join(resolveDshHome(env), "profiles", DSH_DESKTOP_PROFILE).split(path.sep).join("/"); + return [installDir, real].some((dir) => { + const norm = dir.split(path.sep).join("/"); + return norm.startsWith(anchor + "/") && norm.endsWith(`/node_modules/${DSH_PACKAGE}`); + }); +} + export function hostManagedInstall(installDir: string, env: NodeJS.ProcessEnv = process.env): HostManagedInstall | undefined { let real = installDir; try { @@ -277,6 +294,7 @@ export function hostManagedInstall(installDir: string, env: NodeJS.ProcessEnv = } catch { // nonexistent or unreadable — evaluate the literal path } + if (isDshDesktopBiliCopy(installDir, real, env)) return undefined; for (const dir of [installDir, real]) { if (dir.split(path.sep).some((seg) => seg === ".pnpm")) { return { @@ -638,11 +656,52 @@ export async function refreshDshProfileCopy( } try { await refreshDshProfileBundles(latest, log, env); + await refreshDshDesktopCopy(latest, log, env); } finally { await lock.release(); } } +/** #1575 (owner decision): refresh the dsh DESKTOP profile copy IN PLACE. The + * CLI refuses --profile desktop outright ("managed exclusively by the Electron + * application") and the in-app plugin manager cannot be relied on, so bili owns + * that copy: a verified-registry-tarball install through the standard + * junction-safe installer (whose hostManagedInstall exemption recognizes this + * exact layout), keyed off the running user's DSH_HOME. Called alongside + * refreshDshProfileBundles at every driver site; all sites hold the shared + * update lock. Silent while the copy is missing, in step, or ahead; failures + * log and retry next cycle; never throws. */ +export async function refreshDshDesktopCopy( + targetVersion: string, + log: Logger = loggerLog, + env: NodeJS.ProcessEnv = process.env, + resolveProxy?: (url: string) => string | undefined, +): Promise { + const flat = path.join(resolveDshHome(env), "profiles", DSH_DESKTOP_PROFILE, "node_modules", DSH_PACKAGE); + try { + try { + await access(flat, constants.F_OK); + } catch { + return; // no desktop profile on this machine — nothing to keep in step + } + const diskVersion = await readDiskVersion(flat); + if (!isVersionNewer(targetVersion, diskVersion ?? "0.0.0")) return; // in step or ahead — never downgrade + const doc = await fetchVersionDoc({ resolveProxy }, DSH_PACKAGE, targetVersion); + if (!doc?.tarball) { + log("warn", `[update] dsh ${DSH_DESKTOP_PROFILE}: in-place refresh to ${targetVersion} failed \u2014 no dist.tarball for that version on the registry; retrying next cycle`); + return; + } + const result = await installViaTarball(targetVersion, doc.tarball, flat, doc.integrity, doc.shasum, egressDispatcher({ resolveProxy }, doc.tarball), env); + if (result.ok) { + log("info", `[update] refreshed dsh ${DSH_DESKTOP_PROFILE} profile copy in place (${diskVersion ?? "?"} \u2192 ${targetVersion}) \u2014 restart dsh to load it`); + } else { + log("warn", `[update] dsh ${DSH_DESKTOP_PROFILE}: in-place refresh to ${targetVersion} failed: ${result.error}; retrying next cycle`); + } + } catch (err) { + log("warn", `[update] dsh ${DSH_DESKTOP_PROFILE} in-place refresh check failed: ${err instanceof Error ? err.message : String(err)}; leaving the copy untouched`); + } +} + /** #1196-class fix for the pi lane: the copy under /npm is pi's own * materialization of the settings `npm:billion-context` entry — #991 keeps * the global updater out, and pi has no background package updater, so the @@ -710,7 +769,6 @@ async function refreshOwnerManagedCopies( await refreshDshProfileCopy(installDir, opts, env, log); await refreshPiNpmCopy(installDir, opts, env, log); } - /** Registry-pinned dsh profile copies whose installed version is older * than the global one ("name@version" per entry). Dev pins (link:/file:) * and declared-but-not-installed mounts are out of scope: neither @@ -767,6 +825,7 @@ export async function convergeDshProfileBundles( } try { await refreshDshProfileBundles(globalVersion, log, env); + await refreshDshDesktopCopy(globalVersion, log, env); } finally { await lock.release(); } @@ -912,6 +971,7 @@ export async function checkForUpdate(opts: UpdateOptions, force = false): Promis // the old version next to the new global one (#953). Best-effort: // never fails the update itself. await refreshDshProfileBundles(latest, loggerLog); + await refreshDshDesktopCopy(latest, loggerLog, process.env, opts.resolveProxy); notifyStaleInstall(opts, latest); } else { loggerLog("warn", `[update] install failed: ${result.error}. Will retry next cycle.`); @@ -965,6 +1025,7 @@ export async function installViaTarball( integrity?: string, shasum?: string, dispatcher?: object, + env: NodeJS.ProcessEnv = process.env, ): Promise<{ ok: boolean; error?: string }> { if (!installDir) { return { ok: false, error: "cannot determine install directory (package.json not found walking up from running binary)" }; @@ -987,7 +1048,8 @@ export async function installViaTarball( // #991 single-writer: refuse to overwrite a host-managed copy (pnpm // store, host agent data trees) — only its owner may update it. - const managed = hostManagedInstall(installDir); + // Exception: the dsh desktop-profile copy is bili-owned in place (#1575). + const managed = hostManagedInstall(installDir, env); if (managed) { return { ok: false, error: `install dir is managed by ${managed.owner} (${installDir}) \u2014 refusing in-place overwrite (single-writer); update via ${managed.channel}` }; } @@ -1080,27 +1142,65 @@ export async function installViaTarball( await rm(tmpFile, { force: true }); } + // pnpm virtual-store copy (#1575 desktop lane): the flat node_modules + // entry is a directory symlink/junction into /.pnpm/. fs.cp would + // FOLLOW the link and rewrite shared store content (hardlinked, + // integrity-checked by pnpm), so displace the LINK itself and lay down the + // verified package as a real directory at this path instead. + let pnpmOldLink: string | null = null; + { + let linkStat: Awaited> | null = null; + try { + linkStat = await lstat(installDir); + } catch { + // vanished mid-update — treat as a plain missing/real dir below + } + if (linkStat?.isSymbolicLink()) { + pnpmOldLink = `${installDir}.pnpm-${Date.now()}`; + try { + await rename(installDir, pnpmOldLink); + } catch (e) { + return { ok: false, error: `failed to move the pnpm link aside (${pnpmOldLink}): ${String(e)} (install left untouched)` }; + } + } + } + // Back up the current install before overwriting. If anything fails after // the copy (partial copy, version drift, corrupted entry), the backup is // restored so the previously working version keeps running. const backupDir = path.join(cacheDir(), `.update-backup-${version}`); - try { - await rm(backupDir, { recursive: true, force: true }); - await cp(installDir, backupDir, { recursive: true, force: true }); - } catch (e) { - // Fail closed: without a backup we refuse to overwrite the running - // install — the current version keeps working. - return { ok: false, error: `backup of current install failed (install left untouched): ${String(e)}` }; + if (pnpmOldLink) { + // The displaced artifact IS the link itself (kept at pnpmOldLink) and + // the store contents it points into were never touched — nothing to + // back up as files. + try { + await rm(backupDir, { recursive: true, force: true }); + } catch { + // clearing a stale backup dir failing is not fatal in this branch + } + } else { + try { + await rm(backupDir, { recursive: true, force: true }); + await cp(installDir, backupDir, { recursive: true, force: true }); + } catch (e) { + // Fail closed: without a backup we refuse to overwrite the running + // install — the current version keeps working. + return { ok: false, error: `backup of current install failed (install left untouched): ${String(e)}` }; + } } const restoreFromBackup = async (): Promise => { try { await rm(installDir, { recursive: true, force: true }); - await cp(backupDir, installDir, { recursive: true, force: true }); + if (pnpmOldLink) { + await rename(pnpmOldLink, installDir); + } else { + await cp(backupDir, installDir, { recursive: true, force: true }); + } return null; } catch (e) { - // Keep the backup dir — it is the only healthy copy left. - return `ROLLBACK FAILED — restore ${backupDir} to ${installDir} manually: ${String(e)}`; + // Keep the backup — it is the only healthy copy left. + return `ROLLBACK FAILED — restore ${pnpmOldLink ?? backupDir} to ${installDir} manually: ${String(e)}`; } }; @@ -1137,8 +1237,16 @@ export async function installViaTarball( return { ok: false, error: rb ?? postEntryErr }; } - // Success: the backup is no longer needed. + // Success: the backup is no longer needed, and neither is the displaced + // pnpm link (the store copy underneath it was left byte-identical). await rm(backupDir, { recursive: true, force: true }); + if (pnpmOldLink) { + try { + await unlink(pnpmOldLink); + } catch { + // inert once installDir holds the fresh real directory + } + } return { ok: true }; } @@ -1247,6 +1355,7 @@ export async function forceInstallVersion( if (result.ok) { loggerLog("info", `[update] advisory ${advisoryId}: installed ${diskUnderLock ?? opts.currentVersion} → ${targetVersion}. Restart to finish.`); await refreshDshProfileBundles(targetVersion, loggerLog); + await refreshDshDesktopCopy(targetVersion, loggerLog, process.env, opts.resolveProxy); notifyStaleInstall(opts, targetVersion); return { ok: true }; } diff --git a/tests/update-desktop-inplace.test.ts b/tests/update-desktop-inplace.test.ts new file mode 100644 index 0000000000..757f1580c8 --- /dev/null +++ b/tests/update-desktop-inplace.test.ts @@ -0,0 +1,233 @@ +// #1575 (owner decision): the dsh DESKTOP profile copy is updated IN PLACE by +// bili's own tarball installer. On disk the flat node_modules entry is a pnpm +// symlink/junction into /.pnpm/billion-context@/.... The +// installer must displace the LINK itself (fs.cp would follow it into the +// shared, hardlinked, integrity-checked store) and lay down the verified +// package as a real directory at the same path. The store copy underneath +// stays byte-identical. + +import { test } from "node:test"; +import assert from "node:assert/strict"; +import crypto from "node:crypto"; +import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, symlinkSync, lstatSync, readdirSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import * as tar from "tar"; +import { installViaTarball, readDiskVersion, refreshDshDesktopCopy } from "../src/update.ts"; +import { refreshDshProfileBundles } from "../src/dsh-channel.ts"; +import { rmrf } from "./tmp-rm.ts"; + +function integrityField(buf: Buffer, alg = "sha512"): string { + return `${alg}-${crypto.createHash(alg).update(buf).digest("base64")}`; +} + +function writePkg(dir: string, version: string): void { + mkdirSync(path.join(dir, "dist"), { recursive: true }); + writeFileSync( + path.join(dir, "package.json"), + JSON.stringify({ name: "billion-context", version, type: "module", main: "dist/index.js", bin: { bili: "./dist/index.js" } }), + ); + writeFileSync(path.join(dir, "dist", "index.js"), `export const loaded = '${version}';\n`); +} + +/** Create the flat node_modules link the way pnpm does on each platform. + * Windows directory junctions need no SeCreateSymbolicLinkPrivilege, so the + * junction path (rename the link itself, never follow it) stays covered on + * windows runners instead of being skipped (EPERM). */ +function makePnpmLink(target: string, linkPath: string): void { + symlinkSync(target, linkPath, process.platform === "win32" ? "junction" : "dir"); +} + +test("desktop profile copy: link displaced, real dir laid down, .pnpm store untouched (#1575)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-inplace-")); + const prevDshHome = process.env.DSH_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + rmrf(root); + }); + try { + const dshHome = path.join(root, "dsh-home"); + const profileDir = path.join(dshHome, "profiles", "desktop"); + const storeCopy = path.join(profileDir, ".pnpm", "billion-context@1.2.3", "node_modules", "billion-context"); + const flat = path.join(profileDir, "node_modules", "billion-context"); + writePkg(storeCopy, "1.2.3"); + mkdirSync(path.join(profileDir, "node_modules"), { recursive: true }); + makePnpmLink(storeCopy, flat); + process.env.DSH_HOME = dshHome; + + const src = path.join(root, "pkg", "package"); + writePkg(src, "2.0.0"); + const tgzPath = path.join(root, "pkg.tgz"); + tar.c({ cwd: path.join(root, "pkg"), file: tgzPath, gzip: true, sync: true }, ["package"]); + const tgz = readFileSync(tgzPath); + + globalThis.fetch = (() => Promise.resolve(new Response(tgz))) as unknown as typeof fetch; + const r = await installViaTarball("2.0.0", "https://registry.test/x.tgz", flat, integrityField(tgz)); + assert.equal(r.ok, true, r.error ?? "install failed without reason"); + + assert.equal(lstatSync(flat).isSymbolicLink(), false, "the pnpm link must be replaced, not followed"); + assert.equal(lstatSync(flat).isDirectory(), true); + assert.equal(await readDiskVersion(flat), "2.0.0"); + assert.equal(readFileSync(path.join(flat, "dist", "index.js"), "utf8"), "export const loaded = '2.0.0';\n"); + + // the shared store copy the link pointed into stays byte-identical + assert.equal(await readDiskVersion(storeCopy), "1.2.3"); + assert.equal(readFileSync(path.join(storeCopy, "dist", "index.js"), "utf8"), "export const loaded = '1.2.3';\n"); + + // no displaced-link litter next to the install entry + for (const entry of readdirSync(path.dirname(flat))) { + assert.ok(!entry.startsWith("billion-context.pnpm-"), `leftover displaced link: ${entry}`); + } + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); + +// —— drive-path coverage: refreshDshDesktopCopy + the channel skip (#1575) ————————— + +interface DesktopFixture { root: string; dshHome: string; storeCopy: string; flat: string; tgz: Buffer; } + +function buildDesktopFixture(root: string, storeVersion: string, tarballVersion: string): DesktopFixture { + const dshHome = path.join(root, "dsh-home"); + const profileDir = path.join(dshHome, "profiles", "desktop"); + const storeCopy = path.join(profileDir, ".pnpm", `billion-context@${storeVersion}`, "node_modules", "billion-context"); + const flat = path.join(profileDir, "node_modules", "billion-context"); + writePkg(storeCopy, storeVersion); + mkdirSync(path.join(profileDir, "node_modules"), { recursive: true }); + makePnpmLink(storeCopy, flat); + const src = path.join(root, "pkg", "package"); + writePkg(src, tarballVersion); + const tgzPath = path.join(root, "pkg.tgz"); + tar.c({ cwd: path.join(root, "pkg"), file: tgzPath, gzip: true, sync: true }, ["package"]); + return { root, dshHome, storeCopy, flat, tgz: readFileSync(tgzPath) }; +} + +function stubRegistryFetch(tgz: Buffer, tarballUrl: string, version: string, integrity: string): void { + globalThis.fetch = ((url: unknown) => { + const u = String(url); + if (u.endsWith(`/billion-context/${version}`)) { + return Promise.resolve(new Response(JSON.stringify({ dist: { tarball: tarballUrl, integrity } }), { headers: { "content-type": "application/json" } })); + } + if (u === tarballUrl) { + return Promise.resolve(new Response(tgz)); + } + return Promise.reject(new Error(`unexpected fetch in test: ${u}`)); + }) as unknown as typeof fetch; +} + +type CaptureLog = (level: "info" | "warn", msg: string) => void; + +function captureLog(): { lines: string[]; log: CaptureLog } { + const lines: string[] = []; + const log: CaptureLog = (level, msg) => { lines.push(`${level}: ${msg}`); }; + return { lines, log }; +} + +test("refreshDshDesktopCopy: stale junction displaced in place through the full drive path (#1575)", { timeout: 30_000 }, async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-drive-")); + const prevDshHome = process.env.DSH_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + rmrf(root); + }); + try { + const fx = buildDesktopFixture(root, "1.2.3", "2.0.0"); + process.env.DSH_HOME = fx.dshHome; + stubRegistryFetch(fx.tgz, "https://registry.test/bc-2.0.0.tgz", "2.0.0", integrityField(fx.tgz)); + const { lines, log } = captureLog(); + await refreshDshDesktopCopy("2.0.0", log, process.env); + + assert.equal(lstatSync(fx.flat).isSymbolicLink(), false, "the pnpm link must be replaced, not followed"); + assert.equal(lstatSync(fx.flat).isDirectory(), true); + assert.equal(await readDiskVersion(fx.flat), "2.0.0"); + // shared store copy stays byte-identical + assert.equal(await readDiskVersion(fx.storeCopy), "1.2.3"); + for (const entry of readdirSync(path.dirname(fx.flat))) { + assert.ok(!entry.startsWith("billion-context.pnpm-"), `leftover displaced link: ${entry}`); + } + assert.ok(lines.some((l) => l.startsWith("info:") && l.includes("in place") && l.includes("2.0.0")), `expected an in-place success line, got: ${lines.join(" | ")}`); + assert.ok(!lines.some((l) => l.startsWith("warn:")), `unexpected warns: ${lines.join(" | ")}`); + } catch (e) { + if (String(e).includes("EPERM")) { + t.skip("creating directory symlinks requires elevated privileges on this platform"); + return; + } + throw e; + } +}); + +test("refreshDshDesktopCopy: in-step copy untouched, no registry traffic (#1575)", async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-step-")); + const prevDshHome = process.env.DSH_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + rmrf(root); + }); + const dshHome = path.join(root, "dsh-home"); + const flat = path.join(dshHome, "profiles", "desktop", "node_modules", "billion-context"); + writePkg(flat, "2.0.0"); + process.env.DSH_HOME = dshHome; + globalThis.fetch = (() => Promise.reject(new Error("no registry traffic expected while in step"))) as unknown as typeof fetch; + const { lines, log } = captureLog(); + await refreshDshDesktopCopy("2.0.0", log, process.env); + assert.deepEqual(lines, [], `in-step must be silent, got: ${lines.join(" | ")}`); + assert.equal(readFileSync(path.join(flat, "dist", "index.js"), "utf8"), "export const loaded = '2.0.0';\n"); +}); + +test("refreshDshDesktopCopy: absent desktop profile returns silently (#1575)", async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-absent-")); + const prevDshHome = process.env.DSH_HOME; + const originalFetch = globalThis.fetch; + t.after(() => { + globalThis.fetch = originalFetch; + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + rmrf(root); + }); + const dshHome = path.join(root, "dsh-home"); + mkdirSync(path.join(dshHome, "profiles", "desktop"), { recursive: true }); + process.env.DSH_HOME = dshHome; + globalThis.fetch = (() => Promise.reject(new Error("no fetch expected when the desktop profile is absent"))) as unknown as typeof fetch; + const { lines, log } = captureLog(); + await refreshDshDesktopCopy("9.9.9", log, process.env); + assert.deepEqual(lines, []); +}); + +test("refreshDshProfileBundles skips the desktop profile without spawning the CLI (#1575)", async (t) => { + const root = mkdtempSync(path.join(tmpdir(), "bc-desktop-skip-")); + const prevDshHome = process.env.DSH_HOME; + t.after(() => { + if (prevDshHome === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prevDshHome; + rmrf(root); + }); + const dshHome = path.join(root, "dsh-home"); + for (const name of ["web", "desktop"]) { + const dir = path.join(dshHome, "profiles", name); + mkdirSync(dir, { recursive: true }); + writeFileSync(path.join(dir, "package.json"), JSON.stringify({ name: `@dsh/profile-${name}`, dependencies: { "billion-context": "^0.1.170" } })); + } + process.env.DSH_HOME = dshHome; + const env = { ...process.env, BILI_DSH_BIN: path.join(root, "no-such-dsh-binary") }; + const { lines, log } = captureLog(); + const refreshed = await refreshDshProfileBundles("2.0.0", log, env); + assert.equal(refreshed, 0); + assert.ok(!lines.some((l) => l.includes("profile desktop")), `must not attempt the CLI for desktop: ${lines.join(" | ")}`); + // control: a non-desktop registry-pinned stale profile DID attempt the CLI, + // proving this harness detects attempts — the desktop silence above is a skip, + // not a missing probe. + assert.ok(lines.some((l) => l.includes("profile web") && l.includes("failed")), `expected the web profile's CLI attempt to fail visibly: ${lines.join(" | ")}`); +}); diff --git a/tests/update-host-guard.test.ts b/tests/update-host-guard.test.ts index 60bce1740e..66e654dba2 100644 --- a/tests/update-host-guard.test.ts +++ b/tests/update-host-guard.test.ts @@ -60,6 +60,22 @@ test("hostManagedInstall: host agent homes own their trees", () => { } }); +test("hostManagedInstall: dsh desktop profile copy is bili-owned in place (#1575)", () => { + const base = mkdtempSync(path.join(tmpdir(), "bc-host-guard-")); + try { + const ds = path.join(base, "dsh-root"); + const env = { DSH_HOME: ds }; + // flat node_modules entry (the pnpm forwarder path the proxy runs from) + assert.equal(hostManagedInstall(path.join(ds, "profiles", "desktop", "node_modules", "billion-context"), env), undefined); + // realpath form: the virtual-store target beneath the desktop profile + assert.equal(hostManagedInstall(path.join(ds, "profiles", "desktop", ".pnpm", "billion-context@0.1.178_x", "node_modules", "billion-context"), env), undefined); + // identical shape under any OTHER profile stays pnpm-owned + assert.equal(hostManagedInstall(path.join(ds, "profiles", "web", ".pnpm", "billion-context@0.1.178_x", "node_modules", "billion-context"), env)?.owner, "pnpm"); + } finally { + rmrf(base); + } +}); + test("hostManagedInstall: bili-owned dirs (npm global layout, scratch) stay updatable", () => { const base = mkdtempSync(path.join(tmpdir(), "bc-host-guard-")); try {