Skip to content

Commit 7219639

Browse files
alexeaglematsko
authored andcommitted
fix(compiler-cli): base synthetic filepaths on input filepath (angular#28453)
This change is needed to work in google3, where file paths in the ts.Program must always be absolute. PR Close angular#28453
1 parent f2621db commit 7219639

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class SyntheticFilesCompilerHost implements PluginCompilerHost {
3030
private rootFiles: string[], private delegate: ts.CompilerHost,
3131
generatedFiles: (rootFiles: string[]) => {
3232
[fileName: string]: (host: ts.CompilerHost) => ts.SourceFile | undefined
33-
}, ) {
33+
}) {
3434
// Allow ngtsc to contribute in-memory synthetic files, which will be loaded
3535
// as if they existed on disk as action inputs.
3636
const angularGeneratedFiles = generatedFiles !(rootFiles);

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import * as ts from 'typescript';
1111

1212
import {SyntheticFilesCompilerHost} from './synthetic_files_compiler_host';
1313

14-
// Copied from tsc_wrapped/plugin_api.ts to avoid a runtime dependency on that package
14+
// Copied from tsc_wrapped/plugin_api.ts to avoid a runtime dependency on the
15+
// @bazel/typescript package - it would be strange for non-Bazel users of
16+
// Angular to fetch that package.
1517
function createProxy<T>(delegate: T): T {
1618
const proxy = Object.create(null);
1719
for (const k of Object.keys(delegate)) {
@@ -23,6 +25,20 @@ function createProxy<T>(delegate: T): T {
2325
export class NgTscPlugin implements TscPlugin {
2426
constructor(private angularCompilerOptions: unknown) {}
2527

28+
wrapHost(inputFiles: string[], compilerHost: ts.CompilerHost) {
29+
return new SyntheticFilesCompilerHost(inputFiles, compilerHost, (rootFiles: string[]) => {
30+
// For demo purposes, assume that the first .ts rootFile is the only
31+
// one that needs ngfactory.js/d.ts back-compat files produced.
32+
const tsInputs = rootFiles.filter(f => f.endsWith('.ts') && !f.endsWith('.d.ts'));
33+
const factoryPath: string = tsInputs[0].replace(/\.ts/, '.ngfactory.ts');
34+
35+
return {
36+
factoryPath: (host: ts.CompilerHost) =>
37+
ts.createSourceFile(factoryPath, 'contents', ts.ScriptTarget.ES5),
38+
};
39+
});
40+
}
41+
2642
wrap(program: ts.Program, config: {}, host: ts.CompilerHost) {
2743
const proxy = createProxy(program);
2844
proxy.getSemanticDiagnostics = (sourceFile: ts.SourceFile) => {
@@ -38,7 +54,7 @@ export class NgTscPlugin implements TscPlugin {
3854
category: ts.DiagnosticCategory.Error,
3955
code: 12345,
4056
// source is the name of the plugin.
41-
source: 'Angular',
57+
source: 'ngtsc',
4258
};
4359
result.push(fake);
4460
}
@@ -64,15 +80,4 @@ export class NgTscPlugin implements TscPlugin {
6480
}];
6581
return {afterDeclarations};
6682
}
67-
68-
wrapHost(inputFiles: string[], compilerHost: ts.CompilerHost) {
69-
return new SyntheticFilesCompilerHost(inputFiles, compilerHost, this.generatedFiles);
70-
}
71-
72-
generatedFiles(rootFiles: string[]) {
73-
return {
74-
'file-1.ts': (host: ts.CompilerHost) =>
75-
ts.createSourceFile('file-1.ts', 'contents', ts.ScriptTarget.ES5),
76-
};
77-
}
7883
}

0 commit comments

Comments
 (0)