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.
This commit is contained in:
Maciej Lisiewski
2026-11-09 18:31:43 +01:00
parent 16236f2166
commit dc2c16efdb
2 changed files with 53 additions and 17 deletions

View File

@@ -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)) : '<missing>').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 })) {

View File

@@ -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([