Skip to content

Commit

Permalink
chore(project-sync): Fix infinite looping due to package.json changes (
Browse files Browse the repository at this point in the history
…#11341)

Recently more of our build commands have adopted temporarily modifying
the package.json file in order to generate the correct types. This has
resulted in `yarn rwfw project:sync` going into an infinite loop as it
detects changes as result of a build and triggers another build in
response.

Chokidar is debouncing events so we only receive them once the watcher
function has completed. Here I simply add a small 8s period of time
after a build completes where any events it registers for package.json
files (which should include those gathered during the build itself) are
ignored.

The false negative case here is that someone changes a package.json soon
after a build completes or during when a build is running. In that case
it won't be picked up. People will likely see that no sync happened and
just hit save again triggering a build since the 8s will have elapsed.
  • Loading branch information
Josh-Walker-GM committed Aug 22, 2024
1 parent 6a83750 commit dc8e847
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tasks/framework-tools/frameworkSyncToProject.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,19 @@ async function main() {
process.on('SIGINT', closeWatcher)
process.on('exit', closeWatcher)

let lastSyncEndedAt = 0
watcher.on('all', async (event, filePath) => {
// We ignore changes that happen to package.json files that could have occurred
// as a result of the project syncing process. We do this by ignoring changes to
// those files that are registered within a short period after the sync process
// has ended - because events are being emitted only after the process has ended.
if (
Date.now() - lastSyncEndedAt < 8_000 &&
filePath.endsWith('package.json')
) {
return
}

logStatus(`${event}: ${filePath}`)

if (filePath.endsWith('package.json')) {
Expand Down Expand Up @@ -285,6 +297,7 @@ async function main() {

logStatus(`Done, and waiting for changes...`)
console.log(separator)
lastSyncEndedAt = Date.now()
})
}

Expand Down

0 comments on commit dc8e847

Please sign in to comment.