From d900a5217a75accf434a95ad90300ec5005a23a8 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Mon, 13 Nov 2023 11:58:26 -0500 Subject: [PATCH] fix(@angular-devkit/build-angular): maintain current watch files after 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 fd2d72a79d78ef33ee95571f9769d06a75018950) --- .../src/builders/application/build-action.ts | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/builders/application/build-action.ts b/packages/angular_devkit/build_angular/src/builders/application/build-action.ts index 4ce023c4a70f..08b478687ac5 100644 --- a/packages/angular_devkit/build_angular/src/builders/application/build-action.ts +++ b/packages/angular_devkit/build_angular/src/builders/application/build-action.ts @@ -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) { @@ -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