Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): maintain current watch files afte…
Browse files Browse the repository at this point in the history
…r build errors

When using the `application` and/or `browser-esbuild` builders, the current watch files
will be maintained in the event of a build error. This better ensures that files used
in the last build/rebuild that may have caused an error will continue to be watched and
refreshed when later changed. Only when a successful rebuild has occurred will any stale
watch files be removed from the watch list.

(cherry picked from commit fd2d72a)
  • Loading branch information
clydin committed Nov 15, 2023
1 parent 596f763 commit d900a52
Showing 1 changed file with 18 additions and 7 deletions.
Expand Up @@ -138,7 +138,7 @@ export async function* runEsBuildBuildAction(
}

// Wait for changes and rebuild as needed
let previousWatchFiles = new Set(result.watchFiles);
const currentWatchFiles = new Set(result.watchFiles);
try {
for await (const changes of watcher) {
if (options.signal?.aborted) {
Expand All @@ -154,12 +154,23 @@ export async function* runEsBuildBuildAction(
);

// Update watched locations provided by the new build result.
// Add any new locations
watcher.add(result.watchFiles.filter((watchFile) => !previousWatchFiles.has(watchFile)));
const newWatchFiles = new Set(result.watchFiles);
// Remove any old locations
watcher.remove([...previousWatchFiles].filter((watchFile) => !newWatchFiles.has(watchFile)));
previousWatchFiles = newWatchFiles;
// Keep watching all previous files if there are any errors; otherwise consider all
// files stale until confirmed present in the new result's watch files.
const staleWatchFiles = result.errors.length > 0 ? undefined : new Set(currentWatchFiles);
for (const watchFile of result.watchFiles) {
if (!currentWatchFiles.has(watchFile)) {
// Add new watch location
watcher.add(watchFile);
currentWatchFiles.add(watchFile);
}

// Present so remove from stale locations
staleWatchFiles?.delete(watchFile);
}
// Remove any stale locations if the build was successful
if (staleWatchFiles?.size) {
watcher.remove([...staleWatchFiles]);
}

if (writeToFileSystem) {
// Write output files
Expand Down

0 comments on commit d900a52

Please sign in to comment.