Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-cli): prevent prior compilations from being retained in watch builds #42537

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -259,7 +259,7 @@ export class IncrementalCompilation implements IncrementalBuild<ClassRecord, Fil
versions: this.versions,
depGraph: this.depGraph,
semanticDepGraph: newGraph,
traitCompiler,
priorAnalysis: traitCompiler.getAnalyzedRecords(),
typeCheckResults: null,
emitted,
};
Expand Down Expand Up @@ -303,7 +303,11 @@ export class IncrementalCompilation implements IncrementalBuild<ClassRecord, Fil
return null;
}

return this.step.priorState.traitCompiler.recordsFor(sf);
const priorAnalysis = this.step.priorState.priorAnalysis;
if (!priorAnalysis.has(sf)) {
return null;
}
return priorAnalysis.get(sf)!;
}

priorTypeCheckingResultsFor(sf: ts.SourceFile): FileTypeCheckingData|null {
Expand Down
9 changes: 6 additions & 3 deletions packages/compiler-cli/src/ngtsc/incremental/src/state.ts
Expand Up @@ -6,8 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import * as ts from 'typescript';

import {AbsoluteFsPath} from '../../file_system';
import {TraitCompiler} from '../../transform';
import {ClassRecord} from '../../transform';
import {FileTypeCheckingData} from '../../typecheck/src/checker';
import {SemanticDepGraph} from '../semantic_graph';

Expand Down Expand Up @@ -51,9 +53,10 @@ export interface AnalyzedIncrementalState {
semanticDepGraph: SemanticDepGraph;

/**
* `TraitCompiler` which contains records of all analyzed classes within the build.
* The analysis data from a prior compilation. This stores the trait information for all source
* files that was present in a prior compilation.
*/
traitCompiler: TraitCompiler;
priorAnalysis: Map<ts.SourceFile, ClassRecord[]>;

/**
* All generated template type-checking files produced as part of this compilation, or `null` if
Expand Down
Expand Up @@ -16,6 +16,7 @@ ts_library(
"//packages/compiler-cli/src/ngtsc/incremental",
"//packages/compiler-cli/src/ngtsc/perf",
"//packages/compiler-cli/src/ngtsc/testing",
"//packages/compiler-cli/src/ngtsc/transform",
"@npm//typescript",
],
)
Expand Down
Expand Up @@ -10,6 +10,7 @@ import {absoluteFrom, getSourceFileOrError} from '../../file_system';
import {runInEachFileSystem} from '../../file_system/testing';
import {NOOP_PERF_RECORDER} from '../../perf';
import {makeProgram} from '../../testing';
import {TraitCompiler} from '../../transform';
import {IncrementalCompilation} from '../src/incremental';

runInEachFileSystem(() => {
Expand All @@ -20,21 +21,22 @@ runInEachFileSystem(() => {
{name: FOO_PATH, contents: `export const FOO = true;`},
]);
const fooSf = getSourceFileOrError(program, FOO_PATH);
const traitCompiler = {getAnalyzedRecords: () => new Map()} as TraitCompiler;

const versionMapFirst = new Map([[FOO_PATH, 'version.1']]);
const firstCompilation = IncrementalCompilation.fresh(
program,
versionMapFirst,
);
firstCompilation.recordSuccessfulAnalysis(null!);
firstCompilation.recordSuccessfulAnalysis(traitCompiler);
firstCompilation.recordSuccessfulEmit(fooSf);

const versionMapSecond = new Map([[FOO_PATH, 'version.2']]);
const secondCompilation = IncrementalCompilation.incremental(
program, versionMapSecond, program, firstCompilation.state, new Set(),
NOOP_PERF_RECORDER);

secondCompilation.recordSuccessfulAnalysis(null!);
secondCompilation.recordSuccessfulAnalysis(traitCompiler);
expect(secondCompilation.safeToSkipEmit(fooSf)).toBeFalse();
});
});
Expand Down
12 changes: 12 additions & 0 deletions packages/compiler-cli/src/ngtsc/transform/src/compilation.ts
Expand Up @@ -166,6 +166,18 @@ export class TraitCompiler implements ProgramTypeCheckAdapter {
return records;
}

getAnalyzedRecords(): Map<ts.SourceFile, ClassRecord[]> {
const result = new Map<ts.SourceFile, ClassRecord[]>();
for (const [sf, classes] of this.fileToClasses) {
const records: ClassRecord[] = [];
for (const clazz of classes) {
records.push(this.classes.get(clazz)!);
}
result.set(sf, records);
}
return result;
}

/**
* Import a `ClassRecord` from a previous compilation.
*
Expand Down