Repository navigation
Fix stale file reads during a CLI rebuild #20636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hadrienblanc
wants to merge
1
commit into
tailwindlabs:v3
Choose a base branch
from
hadrienblanc:fix/v3-cli-watcher-rebuild-backlog
base: v3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import fs from 'fs' | ||
| import os from 'os' | ||
| import path from 'path' | ||
| import { createWatcher } from '../src/cli/build/watching' | ||
|
|
||
| let tmp | ||
| let current | ||
| let releaseGate = () => {} | ||
|
|
||
| beforeEach(() => { | ||
| tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'tw-watch-')) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| releaseGate() | ||
| releaseGate = () => {} | ||
| await current?.fswatcher.close() | ||
| current = undefined | ||
| fs.rmSync(tmp, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| function write(name, text) { | ||
| let file = path.join(tmp, name) | ||
| fs.writeFileSync(file, text) | ||
| return file | ||
| } | ||
|
|
||
| function delay(ms) { | ||
| return new Promise((resolve) => setTimeout(resolve, ms)) | ||
| } | ||
|
|
||
| async function waitFor(predicate) { | ||
| let started = Date.now() | ||
| while (!predicate()) { | ||
| if (Date.now() - started > 5000) { | ||
| throw new Error('timed out waiting for the watcher') | ||
| } | ||
| await delay(10) | ||
| } | ||
| } | ||
|
|
||
| function watch(rebuild) { | ||
| current = createWatcher( | ||
| {}, | ||
| { | ||
| state: { | ||
| contentPatterns: { | ||
| all: [path.join(tmp, '**/*')], | ||
| dynamic: [path.join(tmp, '**/*')], | ||
| }, | ||
| contextDependencies: new Set(), | ||
| configBag: { dependencies: new Set() }, | ||
| }, | ||
| rebuild, | ||
| } | ||
| ) | ||
|
|
||
| return current | ||
| } | ||
|
|
||
| async function readChanges(changes) { | ||
| let read = [] | ||
| for (let change of changes) { | ||
| read.push({ | ||
| file: change.file, | ||
| extension: change.extension, | ||
| content: await change.content(), | ||
| }) | ||
| } | ||
| return read | ||
| } | ||
|
|
||
| function holdFirstRebuild() { | ||
| let calls = [] | ||
| let markStarted | ||
| let started = new Promise((resolve) => { | ||
| markStarted = resolve | ||
| }) | ||
| let gate = new Promise((resolve) => { | ||
| releaseGate = resolve | ||
| }) | ||
|
|
||
| watch(async (changes) => { | ||
| calls.push(await readChanges(changes)) | ||
| if (calls.length === 1) { | ||
| markStarted() | ||
| await gate | ||
| } | ||
| }) | ||
|
|
||
| return { calls, started } | ||
| } | ||
|
|
||
| test('repeated change events during a rebuild still coalesce into one follow-up read', async () => { | ||
| let { calls, started } = holdFirstRebuild() | ||
| let page = write('page.html', 'initial') | ||
| let view = write('view.html', 'initial') | ||
|
|
||
| current.fswatcher.emit('change', page) | ||
| await started | ||
|
|
||
| for (let i = 0; i < 20; i++) { | ||
| fs.writeFileSync(page, `page ${i}`) | ||
| fs.writeFileSync(view, `view ${i}`) | ||
| current.fswatcher.emit('change', page) | ||
| current.fswatcher.emit('change', view) | ||
| } | ||
| releaseGate() | ||
|
|
||
| await waitFor(() => calls.length >= 2) | ||
| await delay(80) | ||
|
|
||
| expect(calls).toHaveLength(2) | ||
| expect(calls[1]).toEqual([ | ||
| { file: path.resolve(page), extension: 'html', content: 'page 19' }, | ||
| { file: path.resolve(view), extension: 'html', content: 'view 19' }, | ||
| ]) | ||
| }) | ||
|
|
||
| test('rename events during a rebuild keep each file once, with its latest text', async () => { | ||
| let { calls, started } = holdFirstRebuild() | ||
| let page = write('page.html', 'seed') | ||
| let view = write('view.html', 'seed') | ||
|
|
||
| current.fswatcher.emit('change', page) | ||
| await started | ||
|
|
||
| for (let i = 0; i < 8; i++) { | ||
| fs.writeFileSync(page, `page ${i}`) | ||
| fs.writeFileSync(view, `view ${i}`) | ||
| current.fswatcher.emit('raw', 'rename', page, { watchedPath: page }) | ||
| current.fswatcher.emit('raw', 'rename', view, { watchedPath: view }) | ||
| await delay(30) | ||
| } | ||
| releaseGate() | ||
|
|
||
| await waitFor(() => calls.length >= 2) | ||
| await delay(80) | ||
|
|
||
| expect(calls).toHaveLength(2) | ||
| expect(calls[1]).toEqual([ | ||
| { file: path.resolve(page), extension: 'html', content: 'page 7' }, | ||
| { file: path.resolve(view), extension: 'html', content: 'view 7' }, | ||
| ]) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
During a long rebuild, an atomic save can queue captured text, clear its pending flag, then emit
change. ThischangedFiles.set()replaces the captured text with a deferredfs.promises.readFilecall. If the user moves or deletes the file before the next rebuild, that read fails andparseChanges()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.