Skip to content

Commit deacc74

Browse files
alxhubzarend
authored andcommitted
fix(compiler-cli): ensure the compiler tracks ts.Programs correctly (#41291)
`NgCompiler` previously had a notion of the "next" `ts.Program`, which served two purposes: * it allowed a client using the `ts.createProgram` API to query for the latest program produced by the previous `NgCompiler`, as a starting point for building the _next_ program that incorporated any new user changes. * it allowed the old `NgCompiler` to be queried for the `ts.Program` on which all prior state is based, which is needed to compute the delta from the new program to ultimately determine how much of the prior state can be reused. This system contained a flaw: it relied on the `NgCompiler` knowing when the `ts.Program` would be changed. This works fine for changes that originate in `NgCompiler` APIs, but a client of the `TemplateTypeChecker` may use that API in ways that create new `ts.Program`s without the `NgCompiler`'s knowledge. This caused the `NgCompiler`'s concept of the "next" program to get out of sync, causing incorrectness in future incremental analysis. This refactoring cleans up the compiler's `ts.Program` management in several ways: * `TypeCheckingProgramStrategy`, the API which controls `ts.Program` updating, is renamed to the `ProgramDriver` and extracted to a separate ngtsc package. * It loses its responsibility of determining component shim filenames. That functionality now lives exclusively in the template type-checking package. * The "next" `ts.Program` concept is renamed to the "current" program, as the "next" name was misleading in several ways. * `NgCompiler` now wraps the `ProgramDriver` used in the `TemplateTypeChecker` to know when a new `ts.Program` is created, regardless of which API drove the creation, which actually fixes the bug. PR Close #41291
1 parent 10a7c87 commit deacc74

30 files changed

+366
-324
lines changed

packages/compiler-cli/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ts_library(
3333
"//packages/compiler-cli/src/ngtsc/incremental",
3434
"//packages/compiler-cli/src/ngtsc/indexer",
3535
"//packages/compiler-cli/src/ngtsc/perf",
36+
"//packages/compiler-cli/src/ngtsc/program_driver",
3637
"//packages/compiler-cli/src/ngtsc/reflection",
3738
"//packages/compiler-cli/src/ngtsc/shims",
3839
"//packages/compiler-cli/src/ngtsc/translator",

packages/compiler-cli/src/ngtsc/core/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ts_library(
2626
"//packages/compiler-cli/src/ngtsc/modulewithproviders",
2727
"//packages/compiler-cli/src/ngtsc/partial_evaluator",
2828
"//packages/compiler-cli/src/ngtsc/perf",
29+
"//packages/compiler-cli/src/ngtsc/program_driver",
2930
"//packages/compiler-cli/src/ngtsc/reflection",
3031
"//packages/compiler-cli/src/ngtsc/resource",
3132
"//packages/compiler-cli/src/ngtsc/routing",

packages/compiler-cli/src/ngtsc/core/src/compiler.ts

Lines changed: 88 additions & 59 deletions
Large diffs are not rendered by default.

packages/compiler-cli/src/ngtsc/core/test/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ts_library(
1515
"//packages/compiler-cli/src/ngtsc/file_system",
1616
"//packages/compiler-cli/src/ngtsc/file_system/testing",
1717
"//packages/compiler-cli/src/ngtsc/incremental",
18+
"//packages/compiler-cli/src/ngtsc/program_driver",
1819
"//packages/compiler-cli/src/ngtsc/reflection",
1920
"//packages/compiler-cli/src/ngtsc/typecheck",
2021
"//packages/compiler-cli/src/ngtsc/typecheck/api",

packages/compiler-cli/src/ngtsc/core/test/compiler_test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import * as ts from 'typescript';
1111
import {absoluteFrom as _, FileSystem, getFileSystem, getSourceFileOrError, NgtscCompilerHost, setFileSystem} from '../../file_system';
1212
import {runInEachFileSystem} from '../../file_system/testing';
1313
import {IncrementalBuildStrategy, NoopIncrementalBuildStrategy} from '../../incremental';
14+
import {ProgramDriver, TsCreateProgramDriver} from '../../program_driver';
1415
import {ClassDeclaration, isNamedClassDeclaration} from '../../reflection';
15-
import {ReusedProgramStrategy} from '../../typecheck';
16-
import {OptimizeFor, TypeCheckingProgramStrategy} from '../../typecheck/api';
16+
import {OptimizeFor} from '../../typecheck/api';
1717

1818
import {NgCompilerOptions} from '../api';
1919

@@ -22,7 +22,7 @@ import {NgCompilerHost} from '../src/host';
2222

2323
function makeFreshCompiler(
2424
host: NgCompilerHost, options: NgCompilerOptions, program: ts.Program,
25-
programStrategy: TypeCheckingProgramStrategy, incrementalStrategy: IncrementalBuildStrategy,
25+
programStrategy: ProgramDriver, incrementalStrategy: IncrementalBuildStrategy,
2626
enableTemplateTypeChecker: boolean, usePoisonedData: boolean): NgCompiler {
2727
const ticket = freshCompilationTicket(
2828
program, options, incrementalStrategy, programStrategy, /* perfRecorder */ null,
@@ -61,7 +61,7 @@ runInEachFileSystem(() => {
6161
const host = NgCompilerHost.wrap(baseHost, [COMPONENT], options, /* oldProgram */ null);
6262
const program = ts.createProgram({host, options, rootNames: host.inputFiles});
6363
const compiler = makeFreshCompiler(
64-
host, options, program, new ReusedProgramStrategy(program, host, options, []),
64+
host, options, program, new TsCreateProgramDriver(program, host, options, []),
6565
new NoopIncrementalBuildStrategy(), /** enableTemplateTypeChecker */ false,
6666
/* usePoisonedData */ false);
6767

@@ -113,7 +113,7 @@ runInEachFileSystem(() => {
113113
const CmpC = getClass(getSourceFileOrError(program, cmpCFile), 'CmpC');
114114

115115
const compiler = makeFreshCompiler(
116-
host, options, program, new ReusedProgramStrategy(program, host, options, []),
116+
host, options, program, new TsCreateProgramDriver(program, host, options, []),
117117
new NoopIncrementalBuildStrategy(), /** enableTemplateTypeChecker */ false,
118118
/* usePoisonedData */ false);
119119
const components = compiler.getComponentsWithTemplateFile(templateFile);
@@ -165,7 +165,7 @@ runInEachFileSystem(() => {
165165
const CmpA = getClass(getSourceFileOrError(program, cmpAFile), 'CmpA');
166166
const CmpC = getClass(getSourceFileOrError(program, cmpCFile), 'CmpC');
167167
const compiler = makeFreshCompiler(
168-
host, options, program, new ReusedProgramStrategy(program, host, options, []),
168+
host, options, program, new TsCreateProgramDriver(program, host, options, []),
169169
new NoopIncrementalBuildStrategy(), /** enableTemplateTypeChecker */ false,
170170
/* usePoisonedData */ false);
171171
const components = compiler.getComponentsWithStyleFile(styleFile);
@@ -199,7 +199,7 @@ runInEachFileSystem(() => {
199199
const program = ts.createProgram({host, options, rootNames: host.inputFiles});
200200
const CmpA = getClass(getSourceFileOrError(program, cmpAFile), 'CmpA');
201201
const compiler = makeFreshCompiler(
202-
host, options, program, new ReusedProgramStrategy(program, host, options, []),
202+
host, options, program, new TsCreateProgramDriver(program, host, options, []),
203203
new NoopIncrementalBuildStrategy(), /** enableTemplateTypeChecker */ false,
204204
/* usePoisonedData */ false);
205205
const resources = compiler.getComponentResources(CmpA);
@@ -235,7 +235,7 @@ runInEachFileSystem(() => {
235235
const program = ts.createProgram({host, options, rootNames: host.inputFiles});
236236
const CmpA = getClass(getSourceFileOrError(program, cmpAFile), 'CmpA');
237237
const compiler = makeFreshCompiler(
238-
host, options, program, new ReusedProgramStrategy(program, host, options, []),
238+
host, options, program, new TsCreateProgramDriver(program, host, options, []),
239239
new NoopIncrementalBuildStrategy(), /** enableTemplateTypeChecker */ false,
240240
/* usePoisonedData */ false);
241241
const resources = compiler.getComponentResources(CmpA);
@@ -267,7 +267,7 @@ runInEachFileSystem(() => {
267267
const host = NgCompilerHost.wrap(baseHost, [COMPONENT], options, /* oldProgram */ null);
268268
const program = ts.createProgram({host, options, rootNames: host.inputFiles});
269269
const compiler = makeFreshCompiler(
270-
host, options, program, new ReusedProgramStrategy(program, host, options, []),
270+
host, options, program, new TsCreateProgramDriver(program, host, options, []),
271271
new NoopIncrementalBuildStrategy(), /** enableTemplateTypeChecker */ false,
272272
/* usePoisonedData */ false);
273273

@@ -301,7 +301,7 @@ runInEachFileSystem(() => {
301301
const host = NgCompilerHost.wrap(baseHost, [COMPONENT], options, /* oldProgram */ null);
302302
const program = ts.createProgram({host, options, rootNames: host.inputFiles});
303303
const compilerA = makeFreshCompiler(
304-
host, options, program, new ReusedProgramStrategy(program, host, options, []),
304+
host, options, program, new TsCreateProgramDriver(program, host, options, []),
305305
new NoopIncrementalBuildStrategy(), /** enableTemplateTypeChecker */ false,
306306
/* usePoisonedData */ false);
307307

packages/compiler-cli/src/ngtsc/program.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import {absoluteFrom, AbsoluteFsPath, getFileSystem} from './file_system';
1818
import {TrackedIncrementalBuildStrategy} from './incremental';
1919
import {IndexedComponent} from './indexer';
2020
import {ActivePerfRecorder, PerfCheckpoint as PerfCheckpoint, PerfEvent, PerfPhase} from './perf';
21+
import {TsCreateProgramDriver} from './program_driver';
2122
import {DeclarationNode} from './reflection';
2223
import {retagAllTsFiles, untagAllTsFiles} from './shims';
23-
import {ReusedProgramStrategy} from './typecheck';
2424
import {OptimizeFor} from './typecheck/api';
2525

2626

@@ -95,7 +95,7 @@ export class NgtscProgram implements api.Program {
9595
// the program.
9696
untagAllTsFiles(this.tsProgram);
9797

98-
const reusedProgramStrategy = new ReusedProgramStrategy(
98+
const programDriver = new TsCreateProgramDriver(
9999
this.tsProgram, this.host, this.options, this.host.shimExtensionPrefixes);
100100

101101
this.incrementalStrategy = oldProgram !== undefined ?
@@ -114,14 +114,14 @@ export class NgtscProgram implements api.Program {
114114
let ticket: CompilationTicket;
115115
if (oldProgram === undefined) {
116116
ticket = freshCompilationTicket(
117-
this.tsProgram, options, this.incrementalStrategy, reusedProgramStrategy, perfRecorder,
117+
this.tsProgram, options, this.incrementalStrategy, programDriver, perfRecorder,
118118
/* enableTemplateTypeChecker */ false, /* usePoisonedData */ false);
119119
} else {
120120
ticket = incrementalFromCompilerTicket(
121121
oldProgram.compiler,
122122
this.tsProgram,
123123
this.incrementalStrategy,
124-
reusedProgramStrategy,
124+
programDriver,
125125
modifiedResourceFiles,
126126
perfRecorder,
127127
);
@@ -223,7 +223,7 @@ export class NgtscProgram implements api.Program {
223223
const diagnostics = sf === undefined ?
224224
this.compiler.getDiagnostics() :
225225
this.compiler.getDiagnosticsForFile(sf, OptimizeFor.WholeProgram);
226-
this.reuseTsProgram = this.compiler.getNextProgram();
226+
this.reuseTsProgram = this.compiler.getCurrentProgram();
227227
return diagnostics;
228228
}
229229

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load("//tools:defaults.bzl", "ts_library")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
ts_library(
6+
name = "program_driver",
7+
srcs = ["index.ts"] + glob([
8+
"src/*.ts",
9+
]),
10+
module_name = "@angular/compiler-cli/src/ngtsc/program_driver",
11+
deps = [
12+
"//packages/compiler-cli/src/ngtsc/file_system",
13+
"//packages/compiler-cli/src/ngtsc/shims",
14+
"//packages/compiler-cli/src/ngtsc/util",
15+
"@npm//typescript",
16+
],
17+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# The Program Driver interface
2+
3+
`ProgramDriver` is a small but important interface which allows the template type-checking machinery to request changes to the current `ts.Program`, and to receive a new `ts.Program` with those changes applied. This is used to add template type-checking code to the current `ts.Program`, eventually allowing for diagnostics to be produced within that code. This operation is abstracted behind this interface because different clients create `ts.Program`s differently. The Language Service, for example, creates `ts.Program`s from the current editor state on request, while the TS compiler API creates them explicitly.
4+
5+
When running using the TS APIs, it's important that each new `ts.Program` be created incrementally from the previous `ts.Program`. Under a normal compilation, this means that programs alternate between template type checking programs and user programs:
6+
7+
* `ts.Program#1` is created from user input (the user's source files).
8+
* `ts.Program#2` is created incrementally on top of #1 for template type-checking, and adds private TCB code.
9+
* `ts.Program#3` is created incrementally on top of #2 when the user makes changes to files on disk (incremental build).
10+
* `ts.Program#4` is created incrementally on top of #3 to adjust template type-checking code according to the user's changes.
11+
12+
The `TsCreateProgramDriver` performs this operation for template type-checking `ts.Program`s built by the command-line compiler or by the CLI. The latest template type-checking program is then exposed via the `NgCompiler`'s `getCurrentProgram()` operation, and new user programs are expected to be created incrementally on top of the previous template type-checking program.
13+
14+
## Programs and the compiler as a service
15+
16+
Not all clients of the compiler follow the incremental tick-tock scenario above. When the compiler is used as a service, new `ts.Program`s may be generated in response to various queries, either directly to `NgCompiler` or via the `TemplateTypeChecker`. Internally, the compiler will use the current `ProgramDriver` to create these additional `ts.Program`s as needed.
17+
18+
Incremental builds (new user code changes) may also require changing the `ts.Program`, using the compiler's incremental ticket process. If the `TsCreateProgramDriver` is used, the client is responsible for ensuring that any new incremental `ts.Program`s are created on top of the current program from the previous compilation, which can be obtained via `NgCompiler`'s `getCurrentProgram()`.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './src/api';
10+
export {TsCreateProgramDriver} from './src/ts_create_program_driver';
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import * as ts from 'typescript';
10+
import {AbsoluteFsPath} from '../../file_system';
11+
12+
export interface ProgramDriver {
13+
/**
14+
* Whether this strategy supports modifying user files (inline modifications) in addition to
15+
* modifying type-checking shims.
16+
*/
17+
readonly supportsInlineOperations: boolean;
18+
19+
/**
20+
* Retrieve the latest version of the program, containing all the updates made thus far.
21+
*/
22+
getProgram(): ts.Program;
23+
24+
/**
25+
* Incorporate a set of changes to either augment or completely replace the type-checking code
26+
* included in the type-checking program.
27+
*/
28+
updateFiles(contents: Map<AbsoluteFsPath, string>, updateMode: UpdateMode): void;
29+
}
30+
31+
export enum UpdateMode {
32+
/**
33+
* A complete update creates a completely new overlay of type-checking code on top of the user's
34+
* original program, which doesn't include type-checking code from previous calls to
35+
* `updateFiles`.
36+
*/
37+
Complete,
38+
39+
/**
40+
* An incremental update changes the contents of some files in the type-checking program without
41+
* reverting any prior changes.
42+
*/
43+
Incremental,
44+
}

0 commit comments

Comments
 (0)