fix(ivy): ngcc should only index .d.ts exports within the package (#32129)
ngcc needs to solve a unique problem when compiling typings for an entrypoint: it must resolve a declaration within a .js file to its representation in a .d.ts file. Since such .d.ts files can be used in deep imports without ever being referenced from the "root" .d.ts, it's not enough to simply match exported types to the root .d.ts. ngcc must build an index of all .d.ts files. Previously, this operation had a bug: it scanned all .d.ts files in the .d.ts program, not only those within the package. Thus, if a class in the program happened to share a name with a class exported from a dependency's .d.ts, ngcc might accidentally modify the wrong .d.ts file, causing a variety of issues downstream. To fix this issue, ngcc's .d.ts scanner now limits the .d.ts files it indexes to only those declared in the current package. PR Close #32129
This commit is contained in:

committed by
Andrew Kushnir

parent
02bab8cf90
commit
964d72610f
@ -10,6 +10,7 @@ import * as ts from 'typescript';
|
||||
|
||||
import {AbsoluteFsPath} from '../../../src/ngtsc/file_system';
|
||||
import {ClassDeclaration, ClassMember, ClassMemberKind, ClassSymbol, ConcreteDeclaration, CtorParameter, Declaration, Decorator, TypeScriptReflectionHost, isDecoratorIdentifier, reflectObjectLiteral} from '../../../src/ngtsc/reflection';
|
||||
import {isWithinPackage} from '../analysis/util';
|
||||
import {Logger} from '../logging/logger';
|
||||
import {BundleProgram} from '../packages/bundle_program';
|
||||
import {findAll, getNameText, hasNameIdentifier, isDefined, stripDollarSuffix} from '../utils';
|
||||
@ -85,7 +86,8 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
|
||||
protected logger: Logger, protected isCore: boolean, checker: ts.TypeChecker,
|
||||
dts?: BundleProgram|null) {
|
||||
super(checker);
|
||||
this.dtsDeclarationMap = dts && this.computeDtsDeclarationMap(dts.path, dts.program) || null;
|
||||
this.dtsDeclarationMap =
|
||||
dts && this.computeDtsDeclarationMap(dts.path, dts.program, dts.package) || null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1347,8 +1349,9 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
|
||||
* @param dtsProgram The program containing all the typings files.
|
||||
* @returns a map of class names to class declarations.
|
||||
*/
|
||||
protected computeDtsDeclarationMap(dtsRootFileName: AbsoluteFsPath, dtsProgram: ts.Program):
|
||||
Map<string, ts.Declaration> {
|
||||
protected computeDtsDeclarationMap(
|
||||
dtsRootFileName: AbsoluteFsPath, dtsProgram: ts.Program,
|
||||
dtsPackage: AbsoluteFsPath): Map<string, ts.Declaration> {
|
||||
const dtsDeclarationMap = new Map<string, ts.Declaration>();
|
||||
const checker = dtsProgram.getTypeChecker();
|
||||
|
||||
@ -1361,8 +1364,12 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
|
||||
|
||||
// Now add any additional classes that are exported from individual dts files,
|
||||
// but are not publicly exported from the entry-point.
|
||||
dtsProgram.getSourceFiles().forEach(
|
||||
sourceFile => { collectExportedDeclarations(checker, dtsDeclarationMap, sourceFile); });
|
||||
dtsProgram.getSourceFiles().forEach(sourceFile => {
|
||||
if (!isWithinPackage(dtsPackage, sourceFile)) {
|
||||
return;
|
||||
}
|
||||
collectExportedDeclarations(checker, dtsDeclarationMap, sourceFile);
|
||||
});
|
||||
return dtsDeclarationMap;
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@ export interface BundleProgram {
|
||||
host: ts.CompilerHost;
|
||||
path: AbsoluteFsPath;
|
||||
file: ts.SourceFile;
|
||||
package: AbsoluteFsPath;
|
||||
r3SymbolsPath: AbsoluteFsPath|null;
|
||||
r3SymbolsFile: ts.SourceFile|null;
|
||||
}
|
||||
@ -31,7 +32,7 @@ export interface BundleProgram {
|
||||
* Create a bundle program.
|
||||
*/
|
||||
export function makeBundleProgram(
|
||||
fs: FileSystem, isCore: boolean, path: AbsoluteFsPath, r3FileName: string,
|
||||
fs: FileSystem, isCore: boolean, pkg: AbsoluteFsPath, path: AbsoluteFsPath, r3FileName: string,
|
||||
options: ts.CompilerOptions, host: ts.CompilerHost,
|
||||
additionalFiles: AbsoluteFsPath[] = []): BundleProgram {
|
||||
const r3SymbolsPath = isCore ? findR3SymbolsPath(fs, dirname(path), r3FileName) : null;
|
||||
@ -48,7 +49,7 @@ export function makeBundleProgram(
|
||||
const file = program.getSourceFile(path) !;
|
||||
const r3SymbolsFile = r3SymbolsPath && program.getSourceFile(r3SymbolsPath) || null;
|
||||
|
||||
return {program, options, host, path, file, r3SymbolsPath, r3SymbolsFile};
|
||||
return {program, options, host, package: pkg, path, file, r3SymbolsPath, r3SymbolsFile};
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -57,14 +57,15 @@ export function makeEntryPointBundle(
|
||||
// Create the bundle programs, as necessary.
|
||||
const absFormatPath = fs.resolve(entryPoint.path, formatPath);
|
||||
const typingsPath = fs.resolve(entryPoint.path, entryPoint.typings);
|
||||
const src = makeBundleProgram(fs, isCore, absFormatPath, 'r3_symbols.js', options, srcHost);
|
||||
const src = makeBundleProgram(
|
||||
fs, isCore, entryPoint.package, absFormatPath, 'r3_symbols.js', options, srcHost);
|
||||
const additionalDtsFiles = transformDts && mirrorDtsFromSrc ?
|
||||
computePotentialDtsFilesFromJsFiles(fs, src.program, absFormatPath, typingsPath) :
|
||||
[];
|
||||
const dts = transformDts ?
|
||||
makeBundleProgram(
|
||||
fs, isCore, typingsPath, 'r3_symbols.d.ts', options, dtsHost, additionalDtsFiles) :
|
||||
null;
|
||||
const dts = transformDts ? makeBundleProgram(
|
||||
fs, isCore, entryPoint.package, typingsPath, 'r3_symbols.d.ts',
|
||||
options, dtsHost, additionalDtsFiles) :
|
||||
null;
|
||||
const isFlatCore = isCore && src.r3SymbolsFile === null;
|
||||
|
||||
return {entryPoint, format, rootDirs, isCore, isFlatCore, src, dts};
|
||||
|
Reference in New Issue
Block a user