fix(compiler): various squashed fixes for the new ngc
introduce the option `allowEmptyCodegenFiles` to generate all generated files, even if they are empty. - also provides the original source files from which the file was generated in the write file callback - needed e.g. for G3 when copying over pinto mod names from the original component to all generated files use `importAs` from flat modules when writing summaries - i.e. prevents incorrect entries like @angular/common/common in the .ngsummary.json files. change interaction between ng and ts to prevent race conditions - before Angular would rely on TS to first read the file for which we generate files, and then the generated files. However, this can break easily when we reuse an old program. don’t generate files for sources that are outside of `rootDir` (see #19337)
This commit is contained in:

committed by
Victor Berchet

parent
13613d4acb
commit
a8a9660112
@ -67,6 +67,7 @@ def _ngc_tsconfig(ctx, files, srcs, **kwargs):
|
||||
return dict(tsc_wrapped_tsconfig(ctx, files, srcs, **kwargs), **{
|
||||
"angularCompilerOptions": {
|
||||
"generateCodeForLibraries": False,
|
||||
"allowEmptyCodegenFiles": True,
|
||||
# FIXME: wrong place to de-dupe
|
||||
"expectedOut": depset([o.path for o in expected_outs]).to_list()
|
||||
}
|
||||
|
@ -40,24 +40,21 @@ function runOneBuild(args: string[], inputs?: {[path: string]: string}): boolean
|
||||
if (args[0] === '-p') args.shift();
|
||||
// Strip leading at-signs, used to indicate a params file
|
||||
const project = args[0].replace(/^@+/, '');
|
||||
let fileLoader: FileLoader;
|
||||
if (inputs) {
|
||||
fileLoader = new CachedFileLoader(fileCache, ALLOW_NON_HERMETIC_READS);
|
||||
// Resolve the inputs to absolute paths to match TypeScript internals
|
||||
const resolvedInputs: {[path: string]: string} = {};
|
||||
for (const key of Object.keys(inputs)) {
|
||||
resolvedInputs[path.resolve(key)] = inputs[key];
|
||||
}
|
||||
fileCache.updateCache(resolvedInputs);
|
||||
} else {
|
||||
fileLoader = new UncachedFileLoader();
|
||||
}
|
||||
const [{options: tsOptions, bazelOpts, files, config}] = parseTsconfig(project);
|
||||
const expectedOuts = config['angularCompilerOptions']['expectedOut'];
|
||||
|
||||
const {basePath} = ng.calcProjectFileAndBasePath(project);
|
||||
const compilerOpts = ng.createNgCompilerOptions(basePath, config, tsOptions);
|
||||
const {diagnostics} = compile({fileLoader, compilerOpts, bazelOpts, files, expectedOuts});
|
||||
const tsHost = ts.createCompilerHost(compilerOpts, true);
|
||||
const {diagnostics} = compile({
|
||||
allowNonHermeticReads: ALLOW_NON_HERMETIC_READS,
|
||||
compilerOpts,
|
||||
tsHost,
|
||||
bazelOpts,
|
||||
files,
|
||||
inputs,
|
||||
expectedOuts
|
||||
});
|
||||
return diagnostics.every(d => d.category !== ts.DiagnosticCategory.Error);
|
||||
}
|
||||
|
||||
@ -71,14 +68,28 @@ export function relativeToRootDirs(filePath: string, rootDirs: string[]): string
|
||||
return filePath;
|
||||
}
|
||||
|
||||
export function compile(
|
||||
{fileLoader, compilerOpts, bazelOpts, files, expectedOuts, gatherDiagnostics}: {
|
||||
fileLoader: FileLoader,
|
||||
compilerOpts: ng.CompilerOptions,
|
||||
bazelOpts: BazelOptions,
|
||||
files: string[],
|
||||
expectedOuts: string[], gatherDiagnostics?: (program: ng.Program) => ng.Diagnostics
|
||||
}): {diagnostics: ng.Diagnostics, program: ng.Program} {
|
||||
export function compile({allowNonHermeticReads, compilerOpts, tsHost, bazelOpts, files, inputs,
|
||||
expectedOuts, gatherDiagnostics}: {
|
||||
allowNonHermeticReads: boolean,
|
||||
compilerOpts: ng.CompilerOptions,
|
||||
tsHost: ts.CompilerHost, inputs?: {[path: string]: string},
|
||||
bazelOpts: BazelOptions,
|
||||
files: string[],
|
||||
expectedOuts: string[], gatherDiagnostics?: (program: ng.Program) => ng.Diagnostics
|
||||
}): {diagnostics: ng.Diagnostics, program: ng.Program} {
|
||||
let fileLoader: FileLoader;
|
||||
if (inputs) {
|
||||
fileLoader = new CachedFileLoader(fileCache, ALLOW_NON_HERMETIC_READS);
|
||||
// Resolve the inputs to absolute paths to match TypeScript internals
|
||||
const resolvedInputs: {[path: string]: string} = {};
|
||||
for (const key of Object.keys(inputs)) {
|
||||
resolvedInputs[path.resolve(key)] = inputs[key];
|
||||
}
|
||||
fileCache.updateCache(resolvedInputs);
|
||||
} else {
|
||||
fileLoader = new UncachedFileLoader();
|
||||
}
|
||||
|
||||
if (!bazelOpts.es5Mode) {
|
||||
compilerOpts.annotateForClosureCompiler = true;
|
||||
compilerOpts.annotationsAs = 'static fields';
|
||||
@ -89,7 +100,6 @@ export function compile(
|
||||
}
|
||||
|
||||
const writtenExpectedOuts = [...expectedOuts];
|
||||
const tsHost = ts.createCompilerHost(compilerOpts, true);
|
||||
|
||||
const originalWriteFile = tsHost.writeFile.bind(tsHost);
|
||||
tsHost.writeFile =
|
||||
|
Reference in New Issue
Block a user