Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): clear diagnostic cache when exter…
Browse files Browse the repository at this point in the history
…nal 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.
  • Loading branch information
clydin committed Oct 4, 2023
1 parent 9c4a6be commit 83020fc
Showing 1 changed file with 20 additions and 9 deletions.
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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 };
}

Expand Down

0 comments on commit 83020fc

Please sign in to comment.