Skip to content

Fix stale file reads during a CLI rebuild - #20636

Open
hadrienblanc wants to merge 1 commit into
tailwindlabs:v3from
hadrienblanc:fix/v3-cli-watcher-rebuild-backlog
Open

hadrienblanc wants to merge 1 commit into
tailwindlabs:v3from
hadrienblanc:fix/v3-cli-watcher-rebuild-backlog

Conversation

@hadrienblanc

@hadrienblanc hadrienblanc commented Oct 31, 2026 •

Copy link
Copy Markdown

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. recordChangedFile resolves immediately, so a later rename replaces the queued read. The value passed to rebuild() is unchanged: { file, content, extension }.

Test plan

  • npx jest tests/watching.test.js --runInBand — 2 passed, on Node 22.
  • The same file against the unpatched watcher fails the rename test. The follow-up keeps page 0 and view 0 instead of page 7 and view 7.
  • npx eslint src/cli/build/watching.js tests/watching.test.js — clean.

Repeated change events were already collapsed by pendingRebuilds. The new assertion is the rename case.

@hadrienblanc hadrienblanc changed the title Fix CLI watcher queueing one rebuild per file event Fix stale file reads during a CLI rebuild Oct 31, 2026
hadrienblanc

This comment was marked as outdated.

hadrienblanc

This comment was marked as outdated.

@hadrienblanc
hadrienblanc force-pushed the fix/v3-cli-watcher-rebuild-backlog branch 2 times, most recently from 1eabdd7 to a7bfb2c Compare October 31, 2026 22:57
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.
@hadrienblanc
hadrienblanc force-pushed the fix/v3-cli-watcher-rebuild-backlog branch from a7bfb2c to 0e98fda Compare November 2, 2026 11:05
@hadrienblanc
hadrienblanc marked this pull request as ready for review November 2, 2026 13:15
@hadrienblanc
hadrienblanc requested a review from a team as a code owner November 2, 2026 13:15
@greptile-apps

greptile-apps Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[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

Comment thread src/cli/build/watching.js
Comment on lines +132 to 135
changedFiles.set(file, {
file,
content: content ?? (() => fs.promises.readFile(file, 'utf8')),
extension: path.extname(file).slice(1),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

@RobinMalfait RobinMalfait added the v3 label Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants