Skip to content

Commit d900a52

Browse files
committed
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 fd2d72a)
1 parent 596f763 commit d900a52

File tree

1 file changed

+18
-7
lines changed
  • packages/angular_devkit/build_angular/src/builders/application

1 file changed

+18
-7
lines changed

packages/angular_devkit/build_angular/src/builders/application/build-action.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export async function* runEsBuildBuildAction(
138138
}
139139

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

156156
// Update watched locations provided by the new build result.
157-
// Add any new locations
158-
watcher.add(result.watchFiles.filter((watchFile) => !previousWatchFiles.has(watchFile)));
159-
const newWatchFiles = new Set(result.watchFiles);
160-
// Remove any old locations
161-
watcher.remove([...previousWatchFiles].filter((watchFile) => !newWatchFiles.has(watchFile)));
162-
previousWatchFiles = newWatchFiles;
157+
// Keep watching all previous files if there are any errors; otherwise consider all
158+
// files stale until confirmed present in the new result's watch files.
159+
const staleWatchFiles = result.errors.length > 0 ? undefined : new Set(currentWatchFiles);
160+
for (const watchFile of result.watchFiles) {
161+
if (!currentWatchFiles.has(watchFile)) {
162+
// Add new watch location
163+
watcher.add(watchFile);
164+
currentWatchFiles.add(watchFile);
165+
}
166+
167+
// Present so remove from stale locations
168+
staleWatchFiles?.delete(watchFile);
169+
}
170+
// Remove any stale locations if the build was successful
171+
if (staleWatchFiles?.size) {
172+
watcher.remove([...staleWatchFiles]);
173+
}
163174

164175
if (writeToFileSystem) {
165176
// Write output files

0 commit comments

Comments
 (0)