Skip to content

Commit 7c202bd

Browse files
committed
Save names of auto type directive names so that we can update program correctly
1 parent 4b0e9bb commit 7c202bd

20 files changed

+2736
-496
lines changed

src/compiler/builder.ts

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ namespace ts {
8787
sourceFileToPackageName: ESMap<Path, string>;
8888
projectReferences: readonly ProjectReference[] | undefined;
8989
resolvedProjectReferences: readonly (ResolvedProjectReferenceOfProgramFromBuildInfo | undefined)[] | undefined;
90+
automaticTypeDirectiveNames: string[] | undefined;
9091
resolvedTypeReferenceDirectives: ESMap<string, ResolvedTypeReferenceDirectiveWithFailedLookupLocations>;
9192
fileProcessingDiagnostics: FilePreprocessingDiagnostic[] | undefined;
9293
}
@@ -351,6 +352,7 @@ namespace ts {
351352
projectReferences: state.program.getProjectReferences(),
352353
resolvedProjectReferences: state.program.getResolvedProjectReferences()?.map(toResolvedProjectReferenceOfProgramFromBuildInfo),
353354
resolvedTypeReferenceDirectives: state.program.getResolvedTypeReferenceDirectives(),
355+
automaticTypeDirectiveNames: state.program.getAutomaticTypeDirectiveNames(),
354356
fileProcessingDiagnostics: state.program.getFileProcessingDiagnostics(),
355357
};
356358

@@ -918,6 +920,7 @@ namespace ts {
918920
filesByName: readonly PersistedProgramFileByNameEntry[] | undefined;
919921
projectReferences: readonly PersistedProgramProjectReference[] | undefined;
920922
resolvedProjectReferences: readonly (PersistedProgramResolvedProjectReference | undefined)[] | undefined;
923+
automaticTypeDirectiveNames: string[] | undefined;
921924
resolvedTypeReferenceDirectives: readonly PersistedProgramResolutionEntry[] | undefined;
922925
fileProcessingDiagnostics: readonly PersistedProgramFilePreprocessingDiagnostic[] | undefined;
923926
resolutions: readonly PersistedProgramResolution[] | undefined;
@@ -1030,6 +1033,7 @@ namespace ts {
10301033
projectReferences: program.getProjectReferences()?.map(toPersistedProgramProjectReference),
10311034
resolvedProjectReferences: program.getResolvedProjectReferences()?.map(toPersistedProgramResolvedProjectReference),
10321035
resolvedTypeReferenceDirectives: toPersistedProgramResolutionMap(program.getResolvedTypeReferenceDirectives()),
1036+
automaticTypeDirectiveNames: program.getAutomaticTypeDirectiveNames()?.length ? program.getAutomaticTypeDirectiveNames() : undefined,
10331037
fileProcessingDiagnostics: mapToReadonlyArrayOrUndefined(program.getFileProcessingDiagnostics(), toPersistedProgramFilePreprocessingDiagnostic),
10341038
resolutions: mapToReadonlyArrayOrUndefined(resolutions, toPersistedProgramResolution),
10351039
};
@@ -1745,6 +1749,7 @@ namespace ts {
17451749
sourceFileToPackageName,
17461750
projectReferences: program.peristedProgram.projectReferences?.map(toProjectReference),
17471751
resolvedProjectReferences: program.peristedProgram.resolvedProjectReferences?.map(toResolvedProjectReference),
1752+
automaticTypeDirectiveNames: program.peristedProgram.automaticTypeDirectiveNames,
17481753
resolvedTypeReferenceDirectives: toResolutionMap(program.peristedProgram.resolvedTypeReferenceDirectives) || new Map(),
17491754
fileProcessingDiagnostics: map(program.peristedProgram.fileProcessingDiagnostics, toFileProcessingDiagnostic),
17501755
};
@@ -1863,6 +1868,7 @@ namespace ts {
18631868
},
18641869
getProjectReferences: () => persistedProgramState.projectReferences,
18651870
getResolvedProjectReferences: () => persistedProgramState.resolvedProjectReferences,
1871+
getAutomaticTypeDirectiveNames: () => persistedProgramState.automaticTypeDirectiveNames,
18661872
getMissingFilePaths: () => missingFilePaths ||= getMissingFilePaths(persistedProgramState.filesByName),
18671873
getFileIncludeReasons: () => persistedProgramState.fileIncludeReasons,
18681874
getResolvedTypeReferenceDirectives: () => persistedProgramState.resolvedTypeReferenceDirectives,

src/compiler/program.ts

+21-7
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ namespace ts {
847847
const cachedBindAndCheckDiagnosticsForFile: DiagnosticCache<Diagnostic> = {};
848848
const cachedDeclarationDiagnosticsForFile: DiagnosticCache<DiagnosticWithLocation> = {};
849849

850+
let automaticTypeDirectiveNames: string[] | undefined;
850851
let resolvedTypeReferenceDirectives = new Map<string, ResolvedTypeReferenceDirectiveWithFailedLookupLocations>();
851852
let fileProcessingDiagnostics: FilePreprocessingDiagnostic[] | undefined;
852853

@@ -1023,16 +1024,16 @@ namespace ts {
10231024
tracing?.pop();
10241025

10251026
// load type declarations specified via 'types' argument or implicitly from types/ and node_modules/@types folders
1026-
const typeReferences: string[] = rootNames.length ? getAutomaticTypeDirectiveNames(options, host) : emptyArray;
1027+
automaticTypeDirectiveNames ||= rootNames.length ? getAutomaticTypeDirectiveNames(options, host) : emptyArray;
10271028

1028-
if (typeReferences.length) {
1029-
tracing?.push(tracing.Phase.Program, "processTypeReferences", { count: typeReferences.length });
1029+
if (automaticTypeDirectiveNames.length) {
1030+
tracing?.push(tracing.Phase.Program, "processTypeReferences", { count: automaticTypeDirectiveNames.length });
10301031
// This containingFilename needs to match with the one used in managed-side
10311032
const containingDirectory = options.configFilePath ? getDirectoryPath(options.configFilePath) : host.getCurrentDirectory();
10321033
const containingFilename = combinePaths(containingDirectory, inferredTypesContainingFile);
1033-
const resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, containingFilename);
1034-
for (let i = 0; i < typeReferences.length; i++) {
1035-
processTypeReferenceDirective(typeReferences[i], resolutions[i], { kind: FileIncludeKind.AutomaticTypeDirectiveFile, typeReference: typeReferences[i], packageId: resolutions[i]?.resolvedTypeReferenceDirective?.packageId });
1034+
const resolutions = resolveTypeReferenceDirectiveNamesWorker(automaticTypeDirectiveNames, containingFilename);
1035+
for (let i = 0; i < automaticTypeDirectiveNames.length; i++) {
1036+
processTypeReferenceDirective(automaticTypeDirectiveNames[i], resolutions[i], { kind: FileIncludeKind.AutomaticTypeDirectiveFile, typeReference: automaticTypeDirectiveNames[i], packageId: resolutions[i]?.resolvedTypeReferenceDirective?.packageId });
10361037
}
10371038
tracing?.pop();
10381039
}
@@ -1136,6 +1137,7 @@ namespace ts {
11361137
getRelationCacheSizes: () => getDiagnosticsProducingTypeChecker().getRelationCacheSizes(),
11371138
getFileProcessingDiagnostics: () => fileProcessingDiagnostics,
11381139
getResolvedTypeReferenceDirectives: () => resolvedTypeReferenceDirectives,
1140+
getAutomaticTypeDirectiveNames: () => automaticTypeDirectiveNames!,
11391141
isSourceFileFromExternalLibrary,
11401142
isSourceFileFromExternalLibraryPath,
11411143
isSourceFileDefaultLibrary,
@@ -1680,10 +1682,21 @@ namespace ts {
16801682
return structureIsReused;
16811683
}
16821684

1683-
if (changesAffectingProgramStructure(oldOptions, options) || host.hasChangedAutomaticTypeDirectiveNames?.()) {
1685+
if (changesAffectingProgramStructure(oldOptions, options)) {
16841686
return StructureIsReused.SafeModules;
16851687
}
16861688

1689+
if (host.hasChangedAutomaticTypeDirectiveNames) {
1690+
if (host.hasChangedAutomaticTypeDirectiveNames()) return StructureIsReused.SafeModules;
1691+
}
1692+
else {
1693+
// See if auto type reference directives have changed
1694+
automaticTypeDirectiveNames = rootNames.length ? getAutomaticTypeDirectiveNames(options, host) : emptyArray;
1695+
if (!arrayIsEqualTo(automaticTypeDirectiveNames, oldProgram.getAutomaticTypeDirectiveNames() || emptyArray)) {
1696+
return StructureIsReused.SafeModules;
1697+
}
1698+
}
1699+
16871700
// update fileName -> file mapping
16881701
for (let index = 0; index < newSourceFiles.length; index++) {
16891702
const newSourceFile = newSourceFiles[index];
@@ -1726,6 +1739,7 @@ namespace ts {
17261739
fileReasons = oldProgram.getFileIncludeReasons();
17271740
fileProcessingDiagnostics = oldProgram.getFileProcessingDiagnostics();
17281741
resolvedTypeReferenceDirectives = oldProgram.getResolvedTypeReferenceDirectives();
1742+
automaticTypeDirectiveNames = oldProgram.getAutomaticTypeDirectiveNames() || emptyArray;
17291743

17301744
sourceFileToPackageName = oldProgram.sourceFileToPackageName;
17311745
redirectTargetsMap = oldProgram.redirectTargetsMap;

src/compiler/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3937,6 +3937,7 @@ namespace ts {
39373937

39383938
/* @internal */ getFileProcessingDiagnostics(): FilePreprocessingDiagnostic[] | undefined;
39393939
/* @internal */ getResolvedTypeReferenceDirectives(): ESMap<string, ResolvedTypeReferenceDirectiveWithFailedLookupLocations>;
3940+
/* @internal */ getAutomaticTypeDirectiveNames(): string[];
39403941
isSourceFileFromExternalLibrary(file: SourceFile): boolean;
39413942
/* @internal */ isSourceFileFromExternalLibraryPath(path: Path): boolean;
39423943
isSourceFileDefaultLibrary(file: SourceFile): boolean;
@@ -4040,6 +4041,7 @@ namespace ts {
40404041
getMissingFilePaths(): readonly Path[];
40414042
getFileIncludeReasons(): MultiMap<Path, FileIncludeReason>;
40424043
getResolvedTypeReferenceDirectives(): ESMap<string, ResolvedTypeReferenceDirectiveWithFailedLookupLocations>;
4044+
getAutomaticTypeDirectiveNames(): string[] | undefined;
40434045
getFilesByNameMap(): ESMap<Path, SourceFileOfProgramFromBuildInfo | Path | typeof missingSourceOfProjectReferenceRedirect | typeof missingFile>;
40444046
isSourceFileFromExternalLibraryPath(path: Path): boolean;
40454047
getFileProcessingDiagnostics(): FilePreprocessingDiagnostic[] | undefined;

src/testRunner/unittests/tsc/persistResolutions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ namespace ts.PersistentResolutionsTests {
22
describe("unittests:: tsc:: persistResolutions::", () => {
33
const cleanResolutionsWithProject = cleanResolutions("--p");
44
const cleanResolutionsAgainWithProject = cleanResolutionsAgain("--p");
5-
const installNewTypeWithProject = installNewType("Install another type and it is not picked by program");
6-
const deleteExistingTypeWithProject = deleteExistingType("Delete existing type and this will trigger new program so above new type becomes part of program");
5+
const installNewTypeWithProject = installNewType("Install another type");
6+
const deleteExistingTypeWithProject = deleteExistingType("Delete existing type");
77

88
verifyTscSerializedIncrementalEdits({
99
scenario: "persistResolutions",

tests/baselines/reference/tsbuild/persistResolutions/initial-build/saves-resolution-and-uses-it-for-new-program-with-outFile.js

+72-30
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)