fb5-patch-pkg: an update build refuses a stage without node_modules or public/vendor when a base has them (a --no-install build would list every dependency file as removed and break each install it is applied to); the names no update deletes are split off in diffBases (left, named in the summary); test/update-package.test.js (9): the base-folder filters and the refusal

This commit is contained in:
sganggs committed 2026-10-07 11:31:36 +08:00
1 parent 79fc442f8e
commit 9fc7e38a01
3 files changed
+38 -20

No files matched your search

+7 -3
View File
@@ -190,13 +190,14 @@ export function readBase(source, { folder = 'Stronghold-Protocol', skip = () =>
}
/**
* Compare the new full stage with the bases.
* Compare the new full stage with the bases. `removable(rel)`: false for a path no update deletes (it goes to `left`).
* @param {Map<string, { size: number, sha256: string }>} next every file of the new version (the update files left out)
* @param {{ version: string, files: Map<string, { size: number, sha256: string }> }[]} bases
* @returns {{ ship: string[], removed: { path: string, sha256: string[] }[], caseOnly: string[],
* @param {{ removable?: (rel: string) => boolean }} [opts]
* @returns {{ ship: string[], removed: { path: string, sha256: string[] }[], caseOnly: string[], left: string[],
* perBase: { version: string, added: number, changed: number, unchanged: number, removed: number }[] }}
*/
export function diffBases(next, bases) {
export function diffBases(next, bases, { removable = () => true } = {}) {
const ship = new Set();
const perBase = [];
for (const b of bases) {
@@ -211,10 +212,12 @@ export function diffBases(next, bases) {
const lower = new Set([...next.keys()].map((f) => f.toLowerCase()));
const removed = new Map();
const caseOnly = new Set();
const left = new Set();
for (const b of bases) {
for (const [rel, d] of b.files) {
if (next.has(rel)) continue;
if (lower.has(rel.toLowerCase())) { caseOnly.add(rel); continue; }
if (!removable(rel)) { left.add(rel); continue; }
if (!removed.has(rel)) removed.set(rel, new Set());
removed.get(rel).add(d.sha256);
}
@@ -223,6 +226,7 @@ export function diffBases(next, bases) {
ship: [...ship].sort(),
removed: [...removed.keys()].sort().map((rel) => ({ path: rel, sha256: [...removed.get(rel)].sort() })),
caseOnly: [...caseOnly].sort(),
left: [...left].sort(),
perBase,
};
}
+9 -4
View File
@@ -585,14 +585,19 @@ export function buildUpdate(root, p, bases, { out, install = true, force = false
log(`comparing ${staged.length} files with ${bases.map((b) => `v${b.version}`).join(', ')} …`);
const next = new Map();
for (const f of staged) if (f !== MANIFEST_FILE) next.set(f, digestOf(full, f));
const diff = diffBases(next, bases);
// without npm ci the stage has no node_modules / public/vendor, and the update would delete the bases' copies
for (const lib of ['node_modules/', 'public/vendor/']) {
const inBase = bases.find((b) => [...b.files.keys()].some((f) => f.startsWith(lib)));
if (inBase && ![...next.keys()].some((f) => f.startsWith(lib))) {
throw new Error(`v${inBase.version} ships ${lib} but this build has none (--no-install?): an update built so would delete it from every install`);
}
}
// a path no update deletes (a per-machine name such as node_modules/x/.env.example) stays behind, named in the summary
diff.left = diff.removed.filter((r) => removalProblem(r.path)).map((r) => r.path);
const diff = diffBases(next, bases, { removable: (rel) => !removalProblem(rel) });
for (const rel of diff.left) {
const bad = pathProblem(rel);
if (bad) throw new Error(`a base ships ${rel}, which is ${bad}`);
}
diff.removed = diff.removed.filter((r) => !removalProblem(r.path));
for (const rel of diff.ship) copyInto(full, stage, rel);
copyInto(full, stage, MANIFEST_FILE);
const update = { app: p.version, from: bases.map((b) => b.version), files: new Map(diff.ship.map((f) => [f, next.get(f)])), removed: diff.removed };
@@ -632,7 +637,7 @@ export function formatUpdate(r, version) {
const gone = r.diff.removed.map((x) => x.path);
lines.push(`removed: ${gone.length} file(s)${gone.length ? ` (${gone.slice(0, 8).join(', ')}${gone.length > 8 ? ' …' : ''})` : ''}`);
if (r.diff.caseOnly.length) lines.push(`not removed (a new file differs only in case): ${r.diff.caseOnly.join(', ')}`);
if (r.diff.left?.length) lines.push(`not removed (an update never deletes these names): ${r.diff.left.join(', ')}`);
if (r.diff.left.length) lines.push(`not removed (an update never deletes these names): ${r.diff.left.join(', ')}`);
return lines.join('\n') + '\n';
}