From fd15c9bd845eba30fb835e90231c52e3d5e6a39f Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:36:00 -0400 Subject: [PATCH] fix(@angular-devkit/build-angular): clear diagnostic cache when external templates change with esbuild builders To prevent stale Angular template diagnostics from persisting in watch mode (including `ng serve`), the template diagnostic cache will now be invalidated based on the set of changed external template files. This ensures that the Angular AOT compiler will analyze the template again during the rebuild and clear any fixed errors. --- .../angular/compilation/aot-compilation.ts | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/tools/esbuild/angular/compilation/aot-compilation.ts b/packages/angular_devkit/build_angular/src/tools/esbuild/angular/compilation/aot-compilation.ts index 3116784a2fbd..8df67c906e59 100644 --- a/packages/angular_devkit/build_angular/src/tools/esbuild/angular/compilation/aot-compilation.ts +++ b/packages/angular_devkit/build_angular/src/tools/esbuild/angular/compilation/aot-compilation.ts @@ -78,7 +78,7 @@ export class AotCompilation extends AngularCompilation { let usingBuildInfo = false; if (!oldProgram) { oldProgram = ts.readBuilderProgram(compilerOptions, host); - usingBuildInfo = true; + usingBuildInfo = !!oldProgram; } const typeScriptProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram( @@ -93,6 +93,25 @@ export class AotCompilation extends AngularCompilation { findAffectedFiles(typeScriptProgram, angularCompiler, usingBuildInfo), ); + // Get all files referenced in the TypeScript/Angular program including component resources + const referencedFiles = typeScriptProgram + .getSourceFiles() + .filter((sourceFile) => !angularCompiler.ignoreForEmit.has(sourceFile)) + .flatMap((sourceFile) => { + const resourceDependencies = angularCompiler.getResourceDependencies(sourceFile); + + // Also invalidate Angular diagnostics for a source file if component resources are modified + if (this.#state && hostOptions.modifiedFiles?.size) { + for (const resourceDependency of resourceDependencies) { + if (hostOptions.modifiedFiles.has(resourceDependency)) { + this.#state.diagnosticCache.delete(sourceFile); + } + } + } + + return [sourceFile.fileName, ...resourceDependencies]; + }); + this.#state = new AngularCompilationState( angularProgram, host, @@ -103,14 +122,6 @@ export class AotCompilation extends AngularCompilation { this.#state?.diagnosticCache, ); - const referencedFiles = typeScriptProgram - .getSourceFiles() - .filter((sourceFile) => !angularCompiler.ignoreForEmit.has(sourceFile)) - .flatMap((sourceFile) => [ - sourceFile.fileName, - ...angularCompiler.getResourceDependencies(sourceFile), - ]); - return { affectedFiles, compilerOptions, referencedFiles }; }