[rush] rush-lib build cache: editing a source file while its operation's build is in flight writes a cache entry keyed by the pre-edit inputs that contains post-edit outputs (cache poisoning)
Summary
An operation's build-cache key is computed from the iteration's input snapshot, taken when the iteration is scheduled. After the runner finishes, CacheableOperationPlugin packs whatever is in the output folders and writes it under that key. Nothing re-verifies that the operation's own inputs are unchanged. If a source file of the operation is edited while the build is in flight (after the snapshot, before or while the operation runs), the cache entry for the pre-edit inputs contains post-edit outputs. Any later build with the original inputs, on this machine or from a shared/remote cache, then restores wrong outputs silently. Native rush build and the daemon share the same pipeline and behave the same way.
This is not theoretical: during our analysis, an entry for @microsoft/rush-lib keyed by main's inputs in a shared user build cache (~/.rush-user/settings.json buildCacheFolder) contained dist/ output from an unmerged branch. A fresh clone of main restored it and silently got a different rush-lib. Editors, watch mode and multi-agent workflows (AI agents editing while builds run) make the race window routine.
Repro steps
- Take a synthetic 8-project workspace (p06 depends on p05) with the local build cache on. Commit a unique p06 source X, build
--to p05, then dirty p05 so it re-runs first (500 ms). - Start
rush build --to p06in the background. As soon as p05 starts executing (the snapshot has been taken and p06 has not started), append// EDITED-DURING-BUILDto p06's source (content Y). - The build finishes with exit 0, and p06's
lib/contains Y. git checkoutp06's source back to X,rm -rfp06'slib, and runrush build --to p06: p06 "was restored from the build cache" and itslibcontains the edit. This reproduces identically withrush-client(daemon) and nativerush.
Expected result: The cache entry for inputs X only ever contains outputs built from X. If an operation's inputs change during execution, the cache write is skipped (and the operation is marked as needing a re-run).
Actual result: The entry keyed by X holds outputs of Y, so the cache is poisoned.
Details
Root cause (main @ 60007c9a8c): state hashes for all records are computed up front in OperationGraph #scheduleIterationAsync (record.getStateHash()), and OperationBuildCache.trySetCacheEntryAsync later writes the packed outputs under that key without re-validating inputs.
Suggested fix: after execution and before trySetCacheEntryAsync, verify that the operation's own inputs (the project's tracked files plus dependsOnAdditionalFiles) are unchanged since the snapshot. A cheap stat-identity signature recorded when the hash was computed is enough; alternatively, re-hash. On a mismatch, skip the write and log "inputs changed during execution; not caching". A prototype of the stat-signature check fixed both the native and daemon repros without affecting normal caching (rush-lib tests passed).
This was found during an automated performance/behavior analysis of rush-client/rushd on Linux.
Standard questions
| Question | Answer |
|---|---|
@microsoft/rush globally installed version? |
built from main @ 60007c9a8c (5.179.0) |
rushVersion from rush.json? |
5.179.0 |
pnpmVersion, npmVersion, or yarnVersion from rush.json? |
[email protected] |
(if pnpm) useWorkspaces from pnpm-config.json? |
true |
| Operating system? | Linux (WSL2 Ubuntu 24.04) |
| Would you consider contributing a PR? | Yes |
Node.js version (node -v)? |
22.23.2 |
Source: microsoft/rushstack