Repository navigation
Fix stale file reads during a CLI rebuild - #20636
hadrienblanc wants to merge 1 commit into
Conversation
1eabdd7 to
a7bfb2c
Compare
A chain link was appended for each event that got past pendingRebuilds. A raw rename read the file and then awaited that chain, so the pending slot stayed taken and later renames of the same file were dropped. The follow-up rebuild kept the first read. Keep one pending change per file and one in-flight rebuild. Resolve recordChangedFile immediately so a later rename replaces the queued read.
a7bfb2c to
0e98fda
Compare
[Medium impact] The change affects the CLI watch rebuild workflow. Fix the saved-snapshot replacement before merging; it can drop unrelated file updates. Reviews (1) · Last reviewed commit: "Fix stale file reads during a CLI rebuil..." · Reviewed by Greptile |
| changedFiles.set(file, { | ||
| file, | ||
| content: content ?? (() => fs.promises.readFile(file, 'utf8')), | ||
| extension: path.extname(file).slice(1), |
There was a problem hiding this comment.
Later events discard saved text
During a long rebuild, an atomic save can queue captured text, clear its pending flag, then emit change. This changedFiles.set() replaces the captured text with a deferred fs.promises.readFile call. If the user moves or deletes the file before the next rebuild, that read fails and parseChanges() rejects the entire batch, dropping updates from other files too. Previously, the pending flag blocked that replacement. Keep a safe snapshot when later events update the queued entry.
Summary
A rename that lands while a CLI rebuild is running keeps the first read of that file. Later renames were dropped until the follow-up rebuild started, so that rebuild compiled the first snapshot.
The raw handler marks the file pending, reads it, then awaits
recordChangedFile. While the rebuild was still running, a later rename saw the pending slot and returned. The slot was cleared when the follow-up rebuild started.Store one pending change per file and run one rebuild at a time.
recordChangedFileresolves immediately, so a later rename replaces the queued read. The value passed torebuild()is unchanged:{ file, content, extension }.Test plan
npx jest tests/watching.test.js --runInBand— 2 passed, on Node 22.page 0andview 0instead ofpage 7andview 7.npx eslint src/cli/build/watching.js tests/watching.test.js— clean.Repeated
changeevents were already collapsed bypendingRebuilds. The new assertion is the rename case.