mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(compiler-cli): correct incremental behavior even with broken imports (#39923)
When the compiler is invoked via ngc or the Angular CLI, its APIs are used under the assumption that Angular analysis/diagnostics are only requested if the program has no TypeScript-level errors. A result of this assumption is that the incremental engine has not needed to resolve changes via its dependency graph when the program contained broken imports, since broken imports are a TypeScript error. The Angular Language Service for Ivy is using the compiler as a backend, and exercising its incremental compilation APIs without enforcing this assumption. As a result, the Language Service has run into issues where broken imports cause incremental compilation to fail and produce incorrect results. This commit introduces a mechanism within the compiler to keep track of files for which dependency analysis has failed, and to always treat such files as potentially affected by future incremental steps. This is tested via the Language Service infrastructure to ensure that the compiler is doing the right thing in the case of invalid imports. PR Close #39923
This commit is contained in:
committed by
Misko Hevery
parent
a6c8cc3215
commit
c7c5b2fc1e
@@ -64,4 +64,12 @@ export interface DependencyTracker<T extends {fileName: string} = ts.SourceFile>
|
||||
* `resourcesOf` they will not automatically be added to `from`.
|
||||
*/
|
||||
addTransitiveResources(from: T, resourcesOf: T): void;
|
||||
|
||||
/**
|
||||
* Record that the given file contains unresolvable dependencies.
|
||||
*
|
||||
* In practice, this means that the dependency graph cannot provide insight into the effects of
|
||||
* future changes on that file.
|
||||
*/
|
||||
recordDependencyAnalysisFailure(file: T): void;
|
||||
}
|
||||
|
||||
@@ -53,6 +53,10 @@ export class FileDependencyGraph<T extends {fileName: string} = ts.SourceFile> i
|
||||
}
|
||||
}
|
||||
|
||||
recordDependencyAnalysisFailure(file: T): void {
|
||||
this.nodeFor(file).failedAnalysis = true;
|
||||
}
|
||||
|
||||
getResourceDependencies(from: T): AbsoluteFsPath[] {
|
||||
const node = this.nodes.get(from);
|
||||
|
||||
@@ -97,6 +101,7 @@ export class FileDependencyGraph<T extends {fileName: string} = ts.SourceFile> i
|
||||
this.nodes.set(sf, {
|
||||
dependsOn: new Set(node.dependsOn),
|
||||
usesResources: new Set(node.usesResources),
|
||||
failedAnalysis: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -109,6 +114,7 @@ export class FileDependencyGraph<T extends {fileName: string} = ts.SourceFile> i
|
||||
this.nodes.set(sf, {
|
||||
dependsOn: new Set<string>(),
|
||||
usesResources: new Set<AbsoluteFsPath>(),
|
||||
failedAnalysis: false,
|
||||
});
|
||||
}
|
||||
return this.nodes.get(sf)!;
|
||||
@@ -122,6 +128,12 @@ export class FileDependencyGraph<T extends {fileName: string} = ts.SourceFile> i
|
||||
function isLogicallyChanged<T extends {fileName: string}>(
|
||||
sf: T, node: FileNode, changedTsPaths: ReadonlySet<string>, deletedTsPaths: ReadonlySet<string>,
|
||||
changedResources: ReadonlySet<AbsoluteFsPath>): boolean {
|
||||
// A file is assumed to have logically changed if its dependencies could not be determined
|
||||
// accurately.
|
||||
if (node.failedAnalysis) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// A file is logically changed if it has physically changed itself (including being deleted).
|
||||
if (changedTsPaths.has(sf.fileName) || deletedTsPaths.has(sf.fileName)) {
|
||||
return true;
|
||||
@@ -146,6 +158,7 @@ function isLogicallyChanged<T extends {fileName: string}>(
|
||||
interface FileNode {
|
||||
dependsOn: Set<string>;
|
||||
usesResources: Set<AbsoluteFsPath>;
|
||||
failedAnalysis: boolean;
|
||||
}
|
||||
|
||||
const EMPTY_SET: ReadonlySet<any> = new Set<any>();
|
||||
|
||||
@@ -220,6 +220,15 @@ export class StaticInterpreter {
|
||||
if (node.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword) {
|
||||
return undefined;
|
||||
} else {
|
||||
// Check if the symbol here is imported.
|
||||
if (this.dependencyTracker !== null && this.host.getImportOfIdentifier(node) !== null) {
|
||||
// It was, but no declaration for the node could be found. This means that the dependency
|
||||
// graph for the current file cannot be properly updated to account for this (broken)
|
||||
// import. Instead, the originating file is reported as failing dependency analysis,
|
||||
// ensuring that future compilations will always attempt to re-resolve the previously
|
||||
// broken identifier.
|
||||
this.dependencyTracker.recordDependencyAnalysisFailure(context.originatingFile);
|
||||
}
|
||||
return DynamicValue.fromUnknownIdentifier(node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -991,4 +991,5 @@ const fakeDepTracker: DependencyTracker = {
|
||||
addResourceDependency: () => undefined,
|
||||
addTransitiveDependency: () => undefined,
|
||||
addTransitiveResources: () => undefined,
|
||||
recordDependencyAnalysisFailure: () => undefined,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user