Skip to content

Commit f2e5e76

Browse files
authored
fix(update): stage global-style so dependencies land inside the package (#2087)
Release-audit blocker: local-style npm install hoists bun/zod to stageRoot/node_modules, so the swapped-in package directory shipped without its dependencies and every post-upgrade 'ocx update' failed verification. Stage with npm install -g --prefix instead — the global layout nests dependencies inside the package exactly like the live tree — and resolve the staged path across the POSIX lib/node_modules and Windows node_modules shapes. Proven against the real registry: staging @bitkyc08/[email protected] verifies and swaps end-to-end with dependencies inside the package.
1 parent 7535186 commit f2e5e76

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎src/update/transactional-install.mjs‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,16 @@ export function transactionalNpmUpdate({
154154
const rename = deps.rename ?? renameSync;
155155
const scopeDir = dirname(packageDir);
156156
const stageRoot = join(scopeDir, stampedName(".ocx-staging"));
157-
const stagedPackage = join(stageRoot, "node_modules", ...pkgName.split("/"));
157+
// GLOBAL-style staging (-g --prefix): npm nests the package's dependencies INSIDE the
158+
// package dir, exactly like the live global tree this stage will replace. A local-style
159+
// install would hoist bun/zod to stageRoot/node_modules — siblings that the swap would
160+
// leave behind, shipping a dependency-less live tree (release-audit blocker).
161+
// Layout: <stageRoot>/lib/node_modules/<pkg> on POSIX, <stageRoot>/node_modules/<pkg>
162+
// on Windows.
163+
const stagedCandidates = [
164+
join(stageRoot, "lib", "node_modules", ...pkgName.split("/")),
165+
join(stageRoot, "node_modules", ...pkgName.split("/")),
166+
];
158167

159168
// D1: stage to the side. --prefix keeps npm entirely inside stageRoot; the live tree
160169
// and the npm bin shims are untouched until the swap. A failure HERE (mkdir EACCES,
@@ -167,11 +176,16 @@ export function transactionalNpmUpdate({
167176
}
168177
const spec = pkgName + "@" + (targetVersion || tag);
169178
log("Staging " + spec + " into " + stageRoot);
170-
const install = runNpm(["install", "--prefix", stageRoot, "--no-audit", "--no-fund", spec]);
179+
const install = runNpm(["install", "-g", "--prefix", stageRoot, "--no-audit", "--no-fund", spec]);
171180
if (install.status !== 0) {
172181
try { rmSync(stageRoot, { recursive: true, force: true }); } catch { /* best effort */ }
173182
return { ok: false, phase: "stage", error: "npm staging install failed (" + (install.status ?? "?") + ")" };
174183
}
184+
const stagedPackage = stagedCandidates.find(dir => existsSync(join(dir, "package.json")));
185+
if (!stagedPackage) {
186+
try { rmSync(stageRoot, { recursive: true, force: true }); } catch { /* best effort */ }
187+
return { ok: false, phase: "verify", error: "staged package directory not found under " + stageRoot };
188+
}
175189

176190
// D2: verify INSIDE the stage. Live is still untouched on any failure here.
177191
const staged = verifyInstallTree(stagedPackage, targetVersion || undefined);

‎tests/update-transactional.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ function liveVersion(packageDir: string): string | undefined {
3636
}
3737
}
3838

39-
/** npm stub that materializes a staged tree under the --prefix root. */
39+
/** npm stub that materializes a GLOBAL-style staged tree (deps nested inside the package). */
4040
function stagingNpm(version: string, opts: { fail?: boolean; truncate?: boolean } = {}) {
4141
return (args: string[]) => {
4242
if (opts.fail) return { status: 1 };
4343
const prefixIndex = args.indexOf("--prefix");
4444
const stageRoot = args[prefixIndex + 1]!;
45-
const staged = join(stageRoot, "node_modules", ...PKG.split("/"));
45+
// Mirror npm -g layout on POSIX: <prefix>/lib/node_modules/<pkg>.
46+
const staged = join(stageRoot, "lib", "node_modules", ...PKG.split("/"));
4647
writeTree(staged, version);
4748
if (opts.truncate) rmSync(join(staged, "bin", "ocx.mjs"));
4849
return { status: 0 };

0 commit comments

Comments
 (0)