Elffuss BraidHub

utopia-ia/elffuss-code public

main · 4.1 KiBRawPermalink
1#!/usr/bin/env node
2// Reproducibility check for the level 2 vendor bundle.
3//
4// node verify-repro.mjs [--keep]
5//
6// Copies the build inputs into two fresh temporary folders with different
7// absolute paths, runs `npm ci --ignore-scripts` and `node build.mjs --out …`
8// in each, and requires:
9// - byte-identical outputs from the two builds (every .gz, LICENSES.txt and
10// the manifest);
11// - that output equal to the committed web/js/ext2/vendor-manifest.json;
12// - the path sweep passing in both (build.mjs runs it, with each temporary
13// folder as a forbidden needle).
14// It refuses to start with less than 15 GB of free disk. The temporary
15// folders go under $X2_REPRO_TMP when set, else under the system temp folder,
16// and are removed afterwards unless --keep is given.
17import fs from 'node:fs';
18import os from 'node:os';
19import path from 'node:path';
20import { execFileSync } from 'node:child_process';
21import { fileURLToPath } from 'node:url';
22
23const HERE = path.dirname(fileURLToPath(import.meta.url));
24const REPO = path.resolve(HERE, '../..');
25const MIN_FREE = 15 * 1024 ** 3;
26const INPUTS = ['package.json', 'package-lock.json', '.npmrc', '.node-version', 'vite.config.mjs', 'build.mjs', 'postbuild.mjs', 'sweep.mjs', 'sweep-allowlist.json', 'src'];
27
28function freeBytes(dir) {
29 const s = fs.statfsSync(dir);
30 return Number(s.bavail) * Number(s.bsize);
31}
32
33function listFiles(dir, rel = '') {
34 const out = [];
35 for (const name of fs.readdirSync(path.join(dir, rel)).sort()) {
36 const r = rel ? rel + '/' + name : name;
37 if (fs.statSync(path.join(dir, r)).isDirectory()) out.push(...listFiles(dir, r));
38 else out.push(r);
39 }
40 return out;
41}
42
43function prepare(base, label) {
44 const dir = fs.mkdtempSync(path.join(base, `x2-repro-${label}-`));
45 const work = path.join(dir, label === 'a' ? 'w' : 'another-work-folder');
46 return { dir, work, out: path.join(dir, 'out') };
47}
48
49function buildIn({ work, out }) {
50 fs.mkdirSync(work, { recursive: true });
51 for (const f of INPUTS) fs.cpSync(path.join(HERE, f), path.join(work, f), { recursive: true });
52 const env = { ...process.env, npm_config_update_notifier: 'false' };
53 execFileSync('npm', ['ci', '--ignore-scripts', '--prefer-offline', '--no-audit', '--no-fund', '--loglevel=error'], { cwd: work, env, stdio: ['ignore', 'ignore', 'inherit'] });
54 execFileSync(process.execPath, ['build.mjs', '--out', out], { cwd: work, env, stdio: ['ignore', 'pipe', 'inherit'] });
55}
56
57function main() {
58 const keep = process.argv.includes('--keep');
59 const base = path.resolve(process.env.X2_REPRO_TMP || os.tmpdir());
60 fs.mkdirSync(base, { recursive: true });
61 const free = freeBytes(base);
62 if (free < MIN_FREE) throw new Error(`only ${(free / 1024 ** 3).toFixed(1)} GB free; this check needs at least 15 GB`);
63
64 const runs = [];
65 try {
66 for (const label of ['a', 'b']) { const r = prepare(base, label); runs.push(r); buildIn(r); }
67 const [a, b] = runs;
68 const fa = listFiles(a.out), fb = listFiles(b.out);
69 const problems = [];
70 if (JSON.stringify(fa) !== JSON.stringify(fb)) problems.push(`different file lists:\n ${fa.join(', ')}\n ${fb.join(', ')}`);
71 for (const f of fa) {
72 if (!fb.includes(f)) continue;
73 if (!fs.readFileSync(path.join(a.out, f)).equals(fs.readFileSync(path.join(b.out, f)))) problems.push(`${f} differs between the two builds`);
74 }
75 const committed = fs.readFileSync(path.join(REPO, 'web/js/ext2/vendor-manifest.json'));
76 const built = fs.readFileSync(path.join(a.out, 'js/ext2/vendor-manifest.json'));
77 if (!committed.equals(built)) problems.push('the rebuilt manifest differs from the committed web/js/ext2/vendor-manifest.json');
78 const manifest = JSON.parse(built);
79 if (problems.length) throw new Error('NOT reproducible:\n ' + problems.join('\n '));
80 console.log(`reproducible: buildId ${manifest.buildId}, ${fa.length} files byte-identical across two clean builds in different folders, equal to the committed manifest; sweep passed in both`);
81 } finally {
82 if (!keep) for (const r of runs) fs.rmSync(r.dir, { recursive: true, force: true });
83 }
84}
85
86try { main(); } catch (e) { console.error(String(e?.message || e)); process.exit(1); }