Skip to content

Commit 7e9d5f5

Browse files
petebacondarwinalxhub
authored andcommitted
fix(compiler-cli): use CompilerHost to ensure canonical file paths (#36968)
The type checking infrastrure uses file-paths that may come from the TS compiler. Such paths will have been canonicalized, and so the type checking classes must also canonicalize paths when matching. PR Close #36968
1 parent 654868f commit 7e9d5f5

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,8 @@ export class NgCompiler {
486486
// Execute the typeCheck phase of each decorator in the program.
487487
const prepSpan = this.perfRecorder.start('typeCheckPrep');
488488
const ctx = new TypeCheckContext(
489-
typeCheckingConfig, compilation.refEmitter!, compilation.reflector, host.typeCheckFile);
489+
typeCheckingConfig, host, compilation.refEmitter!, compilation.reflector,
490+
host.typeCheckFile);
490491
compilation.traitCompiler.typeCheck(ctx);
491492
this.perfRecorder.stop(prepSpan);
492493

packages/compiler-cli/src/ngtsc/typecheck/src/context.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ export class TypeCheckContext {
3838
private typeCheckFile: TypeCheckFile;
3939

4040
constructor(
41-
private config: TypeCheckingConfig, private refEmitter: ReferenceEmitter,
42-
private reflector: ReflectionHost, typeCheckFilePath: AbsoluteFsPath) {
43-
this.typeCheckFile = new TypeCheckFile(typeCheckFilePath, config, refEmitter, reflector);
41+
private config: TypeCheckingConfig, compilerHost: ts.CompilerHost,
42+
private refEmitter: ReferenceEmitter, private reflector: ReflectionHost,
43+
typeCheckFilePath: AbsoluteFsPath) {
44+
this.typeCheckFile =
45+
new TypeCheckFile(typeCheckFilePath, config, refEmitter, reflector, compilerHost);
4446
}
4547

4648
/**

packages/compiler-cli/src/ngtsc/typecheck/src/type_check_file.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ export class TypeCheckFile extends Environment {
3333
private tcbStatements: ts.Statement[] = [];
3434

3535
constructor(
36-
private fileName: string, config: TypeCheckingConfig, refEmitter: ReferenceEmitter,
37-
reflector: ReflectionHost) {
36+
readonly fileName: AbsoluteFsPath, config: TypeCheckingConfig, refEmitter: ReferenceEmitter,
37+
reflector: ReflectionHost, compilerHost: ts.CompilerHost) {
3838
super(
3939
config, new ImportManager(new NoopImportRewriter(), 'i'), refEmitter, reflector,
40-
ts.createSourceFile(fileName, '', ts.ScriptTarget.Latest, true));
40+
ts.createSourceFile(
41+
compilerHost.getCanonicalFileName(fileName), '', ts.ScriptTarget.Latest, true));
4142
}
4243

4344
addTypeCheckBlock(
@@ -49,7 +50,7 @@ export class TypeCheckFile extends Environment {
4950
}
5051

5152
render(): ts.SourceFile {
52-
let source: string = this.importManager.getAllImports(this.fileName)
53+
let source: string = this.importManager.getAllImports(this.contextFile.fileName)
5354
.map(i => `import * as ${i.qualifier} from '${i.specifier}';`)
5455
.join('\n') +
5556
'\n\n';
@@ -75,7 +76,7 @@ export class TypeCheckFile extends Environment {
7576
source += '\nexport const IS_A_MODULE = true;\n';
7677

7778
return ts.createSourceFile(
78-
this.fileName, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
79+
this.contextFile.fileName, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
7980
}
8081

8182
getPreludeStatements(): ts.Statement[] {

packages/compiler-cli/src/ngtsc/typecheck/test/test_utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export function typecheck(
262262
new LogicalProjectStrategy(reflectionHost, logicalFs),
263263
]);
264264
const ctx = new TypeCheckContext(
265-
{...ALL_ENABLED_CONFIG, ...config}, emitter, reflectionHost, typeCheckFilePath);
265+
{...ALL_ENABLED_CONFIG, ...config}, host, emitter, reflectionHost, typeCheckFilePath);
266266

267267
const templateUrl = 'synthetic.html';
268268
const templateFile = new ParseSourceFile(template, templateUrl);

packages/compiler-cli/src/ngtsc/typecheck/test/type_constructor_spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ runInEachFileSystem(() => {
4545
const host = new NgtscCompilerHost(getFileSystem());
4646
const file = new TypeCheckFile(
4747
_('/_typecheck_.ts'), ALL_ENABLED_CONFIG, new ReferenceEmitter([]),
48-
/* reflector */ null!);
48+
/* reflector */ null!, host);
4949
const sf = file.render();
5050
expect(sf.statements.length).toBe(1);
5151
});
@@ -75,8 +75,8 @@ TestClass.ngTypeCtor({value: 'test'});
7575
new AbsoluteModuleStrategy(program, checker, moduleResolver, reflectionHost),
7676
new LogicalProjectStrategy(reflectionHost, logicalFs),
7777
]);
78-
const ctx =
79-
new TypeCheckContext(ALL_ENABLED_CONFIG, emitter, reflectionHost, _('/_typecheck_.ts'));
78+
const ctx = new TypeCheckContext(
79+
ALL_ENABLED_CONFIG, host, emitter, reflectionHost, _('/_typecheck_.ts'));
8080
const TestClass =
8181
getDeclaration(program, _('/main.ts'), 'TestClass', isNamedClassDeclaration);
8282
ctx.addInlineTypeCtor(
@@ -111,8 +111,8 @@ TestClass.ngTypeCtor({value: 'test'});
111111
new AbsoluteModuleStrategy(program, checker, moduleResolver, reflectionHost),
112112
new LogicalProjectStrategy(reflectionHost, logicalFs),
113113
]);
114-
const ctx =
115-
new TypeCheckContext(ALL_ENABLED_CONFIG, emitter, reflectionHost, _('/_typecheck_.ts'));
114+
const ctx = new TypeCheckContext(
115+
ALL_ENABLED_CONFIG, host, emitter, reflectionHost, _('/_typecheck_.ts'));
116116
const TestClass =
117117
getDeclaration(program, _('/main.ts'), 'TestClass', isNamedClassDeclaration);
118118
ctx.addInlineTypeCtor(
@@ -153,8 +153,8 @@ TestClass.ngTypeCtor({value: 'test'});
153153
new AbsoluteModuleStrategy(program, checker, moduleResolver, reflectionHost),
154154
new LogicalProjectStrategy(reflectionHost, logicalFs),
155155
]);
156-
const ctx =
157-
new TypeCheckContext(ALL_ENABLED_CONFIG, emitter, reflectionHost, _('/_typecheck_.ts'));
156+
const ctx = new TypeCheckContext(
157+
ALL_ENABLED_CONFIG, host, emitter, reflectionHost, _('/_typecheck_.ts'));
158158
const TestClass =
159159
getDeclaration(program, _('/main.ts'), 'TestClass', isNamedClassDeclaration);
160160
ctx.addInlineTypeCtor(

0 commit comments

Comments
 (0)