Fix: stop bun run watch infinite rebuild loop (codegen self-trigger)#240
Merged
Conversation
`build.ts --watch` watches `src/` recursively. Both codegen scripts (`build-site-data`, `build-injection-patterns`) `writeFileSync` their output into `src/rules/*.generated.ts` unconditionally on every build, so each build's write re-triggers the watcher → rebuild → write → loop. The watcher pegs CPU indefinitely, and because builds overlap with no mutex, a newer build's `rm(DIST)` deletes files from under an in-flight one — surfacing as transient `dist/background.js not found` (purity check) and `icons/icon-16.png` ENOENT (cp race). Guard each codegen write with a content comparison so an unchanged output no longer re-arms the watcher. A settled build now stops triggering itself. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
bun run watch(build.ts --watch) spins into an infinite rebuild loop, pegging CPU indefinitely. The symptoms reported were transient build failures:dist/background.js not found — run the extension build first.(background-purity check)ENOENT … copyfile 'icons/icon-16.png' -> 'dist/icons/icon-16.png'(icon copy)These ENOENT errors are symptoms, not real missing files.
Root cause
build.ts --watchwatchessrc/recursively (build.ts:256). Every build runsgenerateSiteData()/generateInjectionPatterns(), whichwriteFileSyncunconditionally intosrc/rules/site-data.generated.tsandsrc/rules/injection-patterns.generated.ts— inside the watched tree. So each build's own write re-triggers the watcher → rebuild → write → loop forever.The watcher also has no build mutex (just a 50ms debounce,
build.ts:251), so builds overlap. Each build starts withawait rm(DIST, …)(build.ts:122), so a newer build deletesdist/out from under an in-flight one — producing the transient "background.js not found" at the purity check and the icon-copy race.Fix
Guard each codegen write with a content comparison (
scripts/file-has-content.ts) so an unchanged output no longer re-arms the watcher. A settled build stops triggering itself, breaking the loop.Verification
bun run builds are clean.bun run checkpasses.bun test scripts/— 37 pass.Note (out of scope)
The missing build mutex is a latent issue: rapid saves during a slow build could still overlap and race on
rm(DIST). This PR removes the self-triggering case, which is what was actually biting. An in-flight guard to serialize concurrent builds could be a follow-up.🤖 Generated with Claude Code