From dc2c16efdb989bcd537625e5936a557bb70bc76d Mon Sep 17 00:00:00 2001 From: Maciej Lisiewski Date: Mon, 9 Nov 2026 18:31:43 +0100 Subject: [PATCH] fix(build): read node-deps inputs once the checkout lock is held The lockfile, projects and receipt key were read before lockCheckout, which can wait 10 minutes. A run that waited out an update compared the new trees with the old files: it reused a stale receipt or rejected a good install. They are now read and validated after the lock is taken. --- scripts/build/node-deps.mjs | 43 ++++++++++++++++++++++--------------- tests-js/node-deps.test.mjs | 27 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/scripts/build/node-deps.mjs b/scripts/build/node-deps.mjs index faa178c2e7..cc65a8e83b 100644 --- a/scripts/build/node-deps.mjs +++ b/scripts/build/node-deps.mjs @@ -584,13 +584,9 @@ function installedLockMatches(lockText, installedLock, selected) { .every(([name, entry]) => same(entry, wanted[project].optionalDependencies?.[name]))) } -/** Install the full requested workspace union in one strict, locked operation. - * ``electron: false`` leaves Desktop's Electron binary unprepared, for builds that never run it. */ -export function prepareNodeDependencies({ source, workspaces, env = process.env, reuse = false, install = true, nativeToolchain, electron = true }) { - source = resolve(source) - if (!Array.isArray(workspaces) || workspaces.length === 0) { - throw new Error('Select at least one workspace; implicit all-workspace installation is not allowed') - } +// What this run installs and the key its receipt carries, read once the run holds the lock: an +// update that changed them while this run waited is installed, not judged against older files. +function dependencyInputs({ source, workspaces, env, nativeToolchain, electron }) { const lockText = readFileSync(join(source, 'pnpm-lock.yaml'), 'utf8') const projects = Object.keys(lockImporters(lockText)).sort() const selected = [...new Set(workspaces.map(workspace => { @@ -615,15 +611,6 @@ export function prepareNodeDependencies({ source, workspaces, env = process.env, const args = ['install', '--frozen-lockfile', '--trust-lockfile', '--prod=false', ...selected.flatMap(workspace => ['--filter', `{./${workspace}}...`]), ] - // This receipt certifies dependency preparation, never compiled product freshness. - // Keep it inside the cached tree so removing node_modules also removes the receipt. - const receipt = join(source, 'node_modules/.hermes-node-deps') - // Ordinary product builders consume the baseline receipt; preparation also - // binds lifecycle outputs to its compiler/SDK identity. - const nativeReceipt = `${receipt}.native-toolchain` - const nativeKey = nativeToolchain === undefined ? undefined : JSON.stringify(nativeToolchain) - // pnpm's copy of the lockfile it last installed. - const installedLock = join(source, 'node_modules/.pnpm/lock.yaml') const inputs = createHash('sha256').update(JSON.stringify({ node: process.versions.node, pnpm: version, platform: process.platform, arch: process.arch, args, // Windows Python uppercases inherited keys. Cache, store and state locations @@ -636,9 +623,31 @@ export function prepareNodeDependencies({ source, workspaces, env = process.env, for (const file of files) { inputs.update(file).update('\0').update(existsSync(join(source, file)) ? readFileSync(join(source, file)) : '').update('\0') } - const key = inputs.digest('hex') + const nativeKey = nativeToolchain === undefined ? undefined : JSON.stringify(nativeToolchain) + return { lockText, projects, selected, prepareElectron, pnpm, args, key: inputs.digest('hex'), nativeKey } +} + +/** Install the full requested workspace union in one strict, locked operation. + * ``electron: false`` leaves Desktop's Electron binary unprepared, for builds that never run it. */ +export function prepareNodeDependencies({ source, workspaces, env = process.env, reuse = false, install = true, nativeToolchain, electron = true }) { + source = resolve(source) + if (!Array.isArray(workspaces) || workspaces.length === 0) { + throw new Error('Select at least one workspace; implicit all-workspace installation is not allowed') + } + // This receipt certifies dependency preparation, never compiled product freshness. + // Keep it inside the cached tree so removing node_modules also removes the receipt. + const receipt = join(source, 'node_modules/.hermes-node-deps') + // Ordinary product builders consume the baseline receipt; preparation also + // binds lifecycle outputs to its compiler/SDK identity. + const nativeReceipt = `${receipt}.native-toolchain` + // pnpm's copy of the lockfile it last installed. + const installedLock = join(source, 'node_modules/.pnpm/lock.yaml') const lock = lockCheckout(source) + let selected try { + const inputs = dependencyInputs({ source, workspaces, env, nativeToolchain, electron }) + const { lockText, projects, prepareElectron, pnpm, args, key, nativeKey } = inputs + selected = inputs.selected const previous = previousTrees(source, lock.check) previous.recover() if (reuse && reusable(lock, { source, receipt, installedLock, key, nativeKey })) { diff --git a/tests-js/node-deps.test.mjs b/tests-js/node-deps.test.mjs index 8d893f7f67..3afb015dad 100644 --- a/tests-js/node-deps.test.mjs +++ b/tests-js/node-deps.test.mjs @@ -402,6 +402,33 @@ test.skipIf(process.platform === 'win32')('a reuse check waits for an install ru expect(readdirSync(join(source, '.cache'))).toEqual([]) }, 60000) +// A run that waits for the lock reads its inputs once it holds it: an update that changed the +// dependencies meanwhile is installed, not judged against the files read before the wait. +test.skipIf(process.platform === 'win32')('a run that waited for the lock installs the inputs current when it got it', async () => { + const { source, env } = fixture() + prepareNodeDependencies({ source, workspaces: ['web'], reuse: true, env }) + // A live owner: a lock this test keeps fresh, as the owner's heartbeat would. + const lock = join(source, '.cache/node-deps.lock') + json(lock, { token: 'live', pid: process.pid, host: hostname() }) + const beat = setInterval(() => { const now = new Date(); utimesSync(lock, now, now) }, 200) + const waiter = spawn(process.execPath, cli(source, '--workspace', 'web', '--reuse'), { env, stdio: 'pipe' }) + let output = '' + waiter.stdout.on('data', data => { output += data }) + waiter.stderr.on('data', data => { output += data }) + const exit = new Promise(done => waiter.on('exit', done)) + try { + await until(() => output.includes('waiting up to')) + addExtraOnly(source, env) + } finally { + clearInterval(beat) + rmSync(lock, { force: true }) + } + expect(await exit).toBe(0) + expect(output).not.toMatch(/reusing completed install/) + expect(createRequire(join(source, 'web/package.json'))('extra-only')).toBe('extra-only') + prepareNodeDependencies({ source, workspaces: ['web'], reuse: true, env, install: false }) +}, 30000) + // A lock nobody refreshes is dead whatever it names: a killed run's pid now reused by another // user's process, another host's name (a container, a renamed Mac) or another PID namespace. test.each([