8000 fix(compiler-cli): do not persist component analysis if template/styl… · angular/angular@04d8b6c · GitHub
[go: up one dir, main page]

Skip to content

Commit 04d8b6c

Browse files
devversionAndrewKushnir
authored andcommitted
fix(compiler-cli): do not persist component analysis if template/styles are missing (#49184)
Consider the following scenario: 1. A TS file with a component and templateUrl exists 2. The template file does not exist. 3. First build: ngtsc will properly report the error, via a FatalDiagnosticError 4. The template file is now created 5. Second build: ngtsc still reports the same errror. ngtsc persists the 8000 analysis data of the component and never invalidates it when the template/style file becomes available later. This breaks incremental builds and potentially common workflows where resource files are added later after the TS file is created. This did surface as an issue in the Angular CLI yet because Webpack requires users to re-start the process when a new file is added. With ESBuild this will change and this also breaks incremental builds with Bazel/Blaze workers. To fix this, we have a few options: * Invalidate the analysis when e.g. the template file is missing. Never caching it means that it will be re-analyzed on every build iteration. * Add the resource dependency to ngtsc's incremental file graph. ngtsc will then know via `host.getModifiedResources` when the file becomes available- and fresh analysis of component would occur. The first approach is straightforward to implement and was chosen here. The second approach would allow ngtsc to re-use more of the analysis when we know that e.g. the template file still not there, but it increases complexity unnecessarily because there is no **single** obvious resource path for e.g. a `templateUrl`. The URL is attempted to be resolved using multiple strategies, such as TS program root dirs, or there is support for a custom resolution through `host.resourceNameToFileName`. It would be possible to determine some candidate paths and add them to the dependency tracker, but it seems incomplete given possible external resolvers like `resourceNameToFileName` and also would likely not have a sufficient-enough impact given that a broken component decorator is not expected to remain for too long between N incremental build iterations. PR Close #49184
1 parent 92b0bda commit 04d8b6c

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

packages/compiler-cli/src/ngtsc/annotations/component/src/handler.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ export class ComponentDecoratorHandler implements
318318
template = preanalyzed;
319319
} else {
320320
const templateDecl = parseTemplateDeclaration(
321-
decorator, component, containingFile, this.evaluator, this.resourceLoader,
322-
this.defaultPreserveWhitespaces);
321+
node, decorator, component, containingFile, this.evaluator, this.depTracker,
322+
this.resourceLoader, this.defaultPreserveWhitespaces);
323323
template = extractTemplate(
324324
node, templateDecl, this.evaluator, this.depTracker, this.resourceLoader, {
325325
enableI18nLegacyMessageIdFormat: this.enableI18nLegacyMessageIdFormat,
@@ -353,6 +353,13 @@ export class ComponentDecoratorHandler implements
353353
this.depTracker.addResourceDependency(node.getSourceFile(), absoluteFrom(resourceUrl));
354354
}
355355
} catch {
356+
if (this.depTracker !== null) {
357+
// The analysis of this file cannot be re-used if one of the style URLs could
358+
// not be resolved or loaded. Future builds should re-analyze and re-attempt
359+
// resolution/loading.
360+
this.depTracker.recordDependencyAnalysisFailure(node.getSourceFile());
361+
}
362+
356363
if (diagnostics === undefined) {
357364
diagnostics = [];
358365
}

packages/compiler-cli/src/ngtsc/annotations/component/src/resources.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ function parseExtractedTemplate(
262262
}
263263

264264
export function parseTemplateDeclaration(
265-
decorator: Decorator, component: Map<string, ts.Expression>, containingFile: string,
266-
evaluator: PartialEvaluator, resourceLoader: ResourceLoader,
267-
defaultPreserveWhitespaces: boolean): TemplateDeclaration {
265+
node: ClassDeclaration, decorator: Decorator, component: Map<string, ts.Expression>,
266+
containingFile: string, evaluator: PartialEvaluator, depTracker: DependencyTracker|null,
267+
resourceLoader: ResourceLoader, defaultPreserveWhitespaces: boolean): TemplateDeclaration {
268268
let preserveWhitespaces: boolean = defaultPreserveWhitespaces;
269269
if (component.has('preserveWhitespaces')) {
270270
const expr = component.get('preserveWhitespaces')!;
@@ -305,6 +305,12 @@ export function parseTemplateDeclaration(
305305
resolvedTemplateUrl: resourceUrl,
306306
};
307307
} catch (e) {
308+
if (depTracker !== null) {
309+
// The analysis of this file cannot be re-used if the template URL could
310+
// not be resolved. Future builds should re-analyze and re-attempt resolution.
311+
depTracker.recordDependencyAnalysisFailure(node.getSourceFile());
312+
}
313+
308314
throw makeResourceNotFoundError(
309315
templateUrl, templateUrlExpr, ResourceTypeForDiagnostics.Template);
310316
}
@@ -348,7 +354,7 @@ export function preloadAndParseTemplate(
348354
if (templatePromise !== undefined) {
349355
return templatePromise.then(() => {
350356
const templateDecl = parseTemplateDeclaration(
351-
decorator, component, containingFile, evaluator, resourceLoader,
357+
node, decorator, component, containingFile, evaluator, depTracker, resourceLoader,
352358
defaultPreserveWhitespaces);
353359
const template =
354360
extractTemplate(node, templateDecl, evaluator, depTracker, resourceLoader, options);
@@ -359,12 +365,18 @@ export function preloadAndParseTemplate(
359365
return Promise.resolve(null);
360366
}
361367
} catch (e) {
368+
if (depTracker !== null) {
369+
// The analysis of this file cannot be re-used if the template URL could
370+
// not be resolved. Future builds should re-analyze and re-attempt resolution.
371+
depTracker.recordDependencyAnalysisFailure(node.getSourceFile());
372+
}
373+
362374
throw makeResourceNotFoundError(
363375
templateUrl, templateUrlExpr, ResourceTypeForDiagnostics.Template);
364376
}
365377
} else {
366378
const templateDecl = parseTemplateDeclaration(
367-
decorator, component, containingFile, evaluator, resourceLoader,
379+
node, decorator, component, containingFile, evaluator, depTracker, resourceLoader,
368380
defaultPreserveWhitespaces);
369381
const template =
370382
extractTemplate(node, templateDecl, evaluator, depTracker, resourceLoader, options);

packages/compiler-cli/test/ngtsc/incremental_spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {ErrorCode, ngErrorCode} from '../../src/ngtsc/diagnostics';
910
import {runInEachFileSystem} from '../../src/ngtsc/file_system/testing';
1011
import {loadStandardTestFiles} from '../../src/ngtsc/testing';
1112

@@ -945,6 +946,50 @@ runInEachFileSystem(() => {
945946
});
946947
});
947948
});
949+
950+
describe('missing resource files', () => {
951+
it('should re-analyze a component if a template file becomes available later', () => {
952+
env.write('app.ts', `
953+
import {Component} from '@angular/core';
954+
955+
@Component({
956+
selector: 'app',
957+
templateUrl: './some-template.html',
958+
})
959+
export class AppComponent {}
960+
`);
961+
962+
const firstDiagnostics = env.driveDiagnostics();
963+
expect(firstDiagnostics.length).toBe(1);
964+
expect(firstDiagnostics[0].code).toBe(ngErrorCode(ErrorCode.COMPONENT_RESOURCE_NOT_FOUND));
965+
966+
env.write('some-template.html', `
967+
<span>Test</span>
968+
`);
969+
970+
env.driveMain();
971+
});
972+
973+
it('should re-analyze if component style file becomes available later', () => {
974+
env.write('app.ts', `
975+
import {Component} from '@angular/core';
976+
977+
@Component({
978+
selector: 'app',
979+
template: 'Works',
980+
styleUrls: ['./some-style.css'],
981+
})
982+
export class AppComponent {}
983+
`);
984+
985+
const firstDiagnostics = env.driveDiagnostics();
986+
expect(firstDiagnostics.length).toBe(1);
987+
expect(firstDiagnostics[0].code).toBe(ngErrorCode(ErrorCode.COMPONENT_RESOURCE_NOT_FOUND));
988+
989+
env.write('some-style.css', `body {}`);
990+
env.driveMain();
991+
});
992+
});
948993
});
949994

950995
function setupFooBarProgram(env: NgtscTestEnvironment) {

0 commit comments

Comments
 (0)
0