Skip to content

Commit 01bf971

Browse files
committed
If typing installer is disabled invalidate all the resolutions from typings cache
This change finally makes all tests pass incremental tests for matching resolutions and program structuture
1 parent e1de706 commit 01bf971

21 files changed

Lines changed: 2210 additions & 2674 deletions

src/compiler/resolutionCache.ts

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ export interface ResolutionCache {
9999
fileWatchesOfAffectingLocations: Map<string, FileWatcherOfAffectingLocation>;
100100
packageDirWatchers: Map<Path, PackageDirWatcher>;
101101
dirPathToSymlinkPackageRefCount: Map<Path, number>;
102+
countResolutionsResolvedWithGlobalCache(): number;
103+
countResolutionsResolvedWithoutGlobalCache(): number;
102104
startRecordingFilesWithChangedResolutions(): void;
103105
finishRecordingFilesWithChangedResolutions(): Path[] | undefined;
104106

@@ -138,6 +140,8 @@ export interface ResolutionCache {
138140
): ResolvedModuleWithFailedLookupLocations;
139141

140142
invalidateResolutionsOfFailedLookupLocations(): boolean;
143+
invalidateResolutionsWithGlobalCachePass(): void;
144+
invalidateResolutionsWithoutGlobalCachePass(): void;
141145
invalidateResolutionOfFile(filePath: Path): void;
142146
removeResolutionsOfFile(filePath: Path): void;
143147
removeResolutionsFromProjectReferenceRedirects(filePath: Path): void;
@@ -169,6 +173,7 @@ export interface ResolutionWithFailedLookupLocations {
169173
// Files that have this resolution using
170174
files?: Set<Path>;
171175
alternateResult?: string;
176+
globalCacheResolution?: ResolvedModuleWithFailedLookupLocations | false;
172177
}
173178

174179
/** @internal */
@@ -537,29 +542,33 @@ function resolveModuleNameUsingGlobalCache(
537542
const host = getModuleResolutionHost(resolutionHost);
538543
const primaryResult = ts_resolveModuleName(moduleName, containingFile, compilerOptions, host, moduleResolutionCache, redirectedReference, mode);
539544
// return result immediately only if global cache support is not enabled or if it is .ts, .tsx or .d.ts
540-
if (!resolutionHost.getGlobalTypingsCacheLocation) {
545+
if (!resolutionHost.getGlobalTypingsCacheLocation || primaryResult.globalCacheResolution !== undefined) {
541546
return primaryResult;
542547
}
543548

544549
// otherwise try to load typings from @types
545550
const globalCache = resolutionHost.getGlobalTypingsCacheLocation();
546-
if (globalCache !== undefined && !isExternalModuleNameRelative(moduleName) && !(primaryResult.resolvedModule && extensionIsTS(primaryResult.resolvedModule.extension))) {
551+
if (!isExternalModuleNameRelative(moduleName) && !(primaryResult.resolvedModule && extensionIsTS(primaryResult.resolvedModule.extension))) {
552+
if (globalCache === undefined) {
553+
primaryResult.globalCacheResolution = false;
554+
return primaryResult;
555+
}
547556
// create different collection of failed lookup locations for second pass
548557
// if it will fail and we've already found something during the first pass - we don't want to pollute its results
549-
const { resolvedModule, failedLookupLocations, affectingLocations, resolutionDiagnostics } = loadModuleFromGlobalCache(
558+
primaryResult.globalCacheResolution = loadModuleFromGlobalCache(
550559
Debug.checkDefined(resolutionHost.globalCacheResolutionModuleName)(moduleName),
551560
resolutionHost.projectName,
552561
compilerOptions,
553562
host,
554563
globalCache,
555564
moduleResolutionCache,
556565
);
557-
if (resolvedModule) {
566+
if (primaryResult.globalCacheResolution.resolvedModule) {
558567
// Modify existing resolution so its saved in the directory cache as well
559-
(primaryResult.resolvedModule as any) = resolvedModule;
560-
primaryResult.failedLookupLocations = updateResolutionField(primaryResult.failedLookupLocations, failedLookupLocations);
561-
primaryResult.affectingLocations = updateResolutionField(primaryResult.affectingLocations, affectingLocations);
562-
primaryResult.resolutionDiagnostics = updateResolutionField(primaryResult.resolutionDiagnostics, resolutionDiagnostics);
568+
(primaryResult.resolvedModule as any) = primaryResult.globalCacheResolution.resolvedModule;
569+
primaryResult.failedLookupLocations = updateResolutionField(primaryResult.failedLookupLocations, primaryResult.globalCacheResolution.failedLookupLocations);
570+
primaryResult.affectingLocations = updateResolutionField(primaryResult.affectingLocations, primaryResult.globalCacheResolution.affectingLocations);
571+
primaryResult.resolutionDiagnostics = updateResolutionField(primaryResult.resolutionDiagnostics, primaryResult.globalCacheResolution.resolutionDiagnostics);
563572
return primaryResult;
564573
}
565574
}
@@ -572,7 +581,10 @@ function resolveModuleNameUsingGlobalCache(
572581
export type GetResolutionWithResolvedFileName<T extends ResolutionWithFailedLookupLocations = ResolutionWithFailedLookupLocations, R extends ResolutionWithResolvedFileName = ResolutionWithResolvedFileName> = (resolution: T) => R | undefined;
573582

574583
/** @internal */
575-
export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootDirForResolution: string, logChangesWhenResolvingModule: boolean): ResolutionCache {
584+
export function createResolutionCache(
585+
resolutionHost: ResolutionCacheHost,
586+
rootDirForResolution: string,
587+
): ResolutionCache {
576588
let filesWithChangedSetOfUnresolvedImports: Path[] | undefined;
577589
let filesWithInvalidatedResolutions: Set<Path> | undefined;
578590
let filesWithInvalidatedNonRelativeUnresolvedImports: ReadonlyMap<Path, readonly string[]> | undefined;
@@ -590,6 +602,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
590602
let startsWithPathChecks: Set<Path> | undefined;
591603
let isInDirectoryChecks: Set<Path> | undefined;
592604
let allModuleAndTypeResolutionsAreInvalidated = false;
605+
let resolutionsWithGlobalCachePassAreInvalidated = false;
606+
let resolutionsWithoutGlobalCachePassAreInvalidated = false;
593607

594608
const getCurrentDirectory = memoize(() => resolutionHost.getCurrentDirectory!());
595609
const cachedDirectoryStructureHost = resolutionHost.getCachedDirectoryStructureHost();
@@ -621,6 +635,9 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
621635
moduleResolutionCache.getPackageJsonInfoCache(),
622636
);
623637

638+
let resolutionsResolvedWithGlobalCache = 0;
639+
let resolutionsResolvedWithoutGlobalCache = 0;
640+
624641
const directoryWatchesOfFailedLookups = new Map<Path, DirectoryWatchesOfFailedLookup>();
625642
const fileWatchesOfAffectingLocations = new Map<string, FileWatcherOfAffectingLocation>();
626643
const rootDir = getRootDirectoryOfResolutionCache(rootDirForResolution, getCurrentDirectory);
@@ -647,6 +664,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
647664
fileWatchesOfAffectingLocations,
648665
packageDirWatchers,
649666
dirPathToSymlinkPackageRefCount,
667+
countResolutionsResolvedWithGlobalCache: () => resolutionsResolvedWithGlobalCache,
668+
countResolutionsResolvedWithoutGlobalCache: () => resolutionsResolvedWithoutGlobalCache,
650669
watchFailedLookupLocationsOfExternalModuleResolutions,
651670
getModuleResolutionCache: () => moduleResolutionCache,
652671
startRecordingFilesWithChangedResolutions,
@@ -664,6 +683,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
664683
hasChangedAutomaticTypeDirectiveNames: () => hasChangedAutomaticTypeDirectiveNames,
665684
invalidateResolutionOfFile,
666685
invalidateResolutionsOfFailedLookupLocations,
686+
invalidateResolutionsWithGlobalCachePass,
687+
invalidateResolutionsWithoutGlobalCachePass,
667688
setFilesWithInvalidatedNonRelativeUnresolvedImports,
668689
createHasInvalidatedResolutions,
669690
isFileWithInvalidatedNonRelativeUnresolvedImports,
@@ -686,12 +707,16 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
686707
resolvedFileToResolution.clear();
687708
resolutionsWithFailedLookups.clear();
688709
resolutionsWithOnlyAffectingLocations.clear();
710+
resolutionsResolvedWithGlobalCache = 0;
711+
resolutionsResolvedWithoutGlobalCache = 0;
689712
failedLookupChecks = undefined;
690713
startsWithPathChecks = undefined;
691714
isInDirectoryChecks = undefined;
692715
affectingPathChecks = undefined;
693716
affectingPathChecksForFile = undefined;
694717
allModuleAndTypeResolutionsAreInvalidated = false;
718+
resolutionsWithGlobalCachePassAreInvalidated = false;
719+
resolutionsWithoutGlobalCachePassAreInvalidated = false;
695720
moduleResolutionCache.clear();
696721
typeReferenceDirectiveResolutionCache.clear();
697722
moduleResolutionCache.update(resolutionHost.getCompilationSettings());
@@ -1027,7 +1052,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
10271052
),
10281053
getResolutionWithResolvedFileName: getResolvedModuleFromResolution,
10291054
shouldRetryResolution: resolution => !resolution.resolvedModule || !resolutionExtensionIsTSOrJson(resolution.resolvedModule.extension),
1030-
logChanges: logChangesWhenResolvingModule,
1055+
logChanges: !!resolutionHost.getGlobalTypingsCacheLocation,
10311056
deferWatchingNonRelativeResolution: true, // Defer non relative resolution watch because we could be using ambient modules
10321057
});
10331058
}
@@ -1102,6 +1127,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
11021127
) {
11031128
(resolution.files ??= new Set()).add(filePath);
11041129
if (resolution.files.size !== 1) return;
1130+
if (resolution.globalCacheResolution) resolutionsResolvedWithGlobalCache++;
1131+
else if (resolution.globalCacheResolution === false) resolutionsResolvedWithoutGlobalCache++;
11051132
if (!deferWatchingNonRelativeResolution || isExternalModuleNameRelative(name)) {
11061133
watchFailedLookupLocationOfResolution(resolution);
11071134
}
@@ -1378,6 +1405,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
13781405
Debug.checkDefined(resolution.files).delete(filePath);
13791406
if (resolution.files!.size) return;
13801407
resolution.files = undefined;
1408+
if (resolution.globalCacheResolution) resolutionsResolvedWithGlobalCache--;
1409+
if (resolution.globalCacheResolution === false) resolutionsResolvedWithoutGlobalCache--;
13811410
const resolved = getResolutionWithResolvedFileName(resolution);
13821411
if (resolved && resolved.resolvedFileName) {
13831412
const key = resolutionHost.toPath(resolved.resolvedFileName);
@@ -1558,6 +1587,13 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
15581587
}
15591588
}
15601589

1590+
function invalidateResolutionsWithGlobalCachePass() {
1591+
if (resolutionsResolvedWithGlobalCache) resolutionsWithGlobalCachePassAreInvalidated = true;
1592+
}
1593+
function invalidateResolutionsWithoutGlobalCachePass() {
1594+
if (resolutionsResolvedWithoutGlobalCache) resolutionsWithoutGlobalCachePassAreInvalidated = true;
1595+
}
1596+
15611597
function invalidateResolutionsOfFailedLookupLocations() {
15621598
if (allModuleAndTypeResolutionsAreInvalidated) {
15631599
affectingPathChecksForFile = undefined;
@@ -1569,6 +1605,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
15691605
startsWithPathChecks = undefined;
15701606
isInDirectoryChecks = undefined;
15711607
affectingPathChecks = undefined;
1608+
resolutionsWithGlobalCachePassAreInvalidated = false;
1609+
resolutionsWithoutGlobalCachePassAreInvalidated = false;
15721610
return true;
15731611
}
15741612
let invalidated = false;
@@ -1582,7 +1620,10 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
15821620
affectingPathChecksForFile = undefined;
15831621
}
15841622

1585-
if (!failedLookupChecks && !startsWithPathChecks && !isInDirectoryChecks && !affectingPathChecks) {
1623+
if (
1624+
!failedLookupChecks && !startsWithPathChecks && !isInDirectoryChecks && !affectingPathChecks &&
1625+
!resolutionsWithGlobalCachePassAreInvalidated && !resolutionsWithoutGlobalCachePassAreInvalidated
1626+
) {
15861627
return invalidated;
15871628
}
15881629

@@ -1593,6 +1634,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
15931634
isInDirectoryChecks = undefined;
15941635
invalidated = invalidateResolutions(resolutionsWithOnlyAffectingLocations, canInvalidatedFailedLookupResolutionWithAffectingLocation) || invalidated;
15951636
affectingPathChecks = undefined;
1637+
resolutionsWithGlobalCachePassAreInvalidated = false;
1638+
resolutionsWithoutGlobalCachePassAreInvalidated = false;
15961639
return invalidated;
15971640
}
15981641

@@ -1612,6 +1655,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
16121655
}
16131656

16141657
function canInvalidatedFailedLookupResolutionWithAffectingLocation(resolution: ResolutionWithFailedLookupLocations) {
1658+
if (resolutionsWithGlobalCachePassAreInvalidated && resolution.globalCacheResolution) return true;
1659+
if (resolutionsWithoutGlobalCachePassAreInvalidated && resolution.globalCacheResolution === false) return true;
16151660
return !!affectingPathChecks && resolution.affectingLocations?.some(location => affectingPathChecks!.has(location));
16161661
}
16171662

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8006,6 +8006,8 @@ export interface ResolvedModuleWithFailedLookupLocations {
80068006
* have been resolvable under different module resolution settings.
80078007
*/
80088008
alternateResult?: string;
8009+
/** @internal */
8010+
globalCacheResolution?: ResolvedModuleWithFailedLookupLocations | false;
80098011
}
80108012

80118013
export interface ResolvedTypeReferenceDirective {

src/compiler/watchPublic.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,6 @@ export function createWatchProgram<T extends BuilderProgram>(host: WatchCompiler
523523
configFileName ?
524524
getDirectoryPath(getNormalizedAbsolutePath(configFileName, currentDirectory)) :
525525
currentDirectory,
526-
/*logChangesWhenResolvingModule*/ false,
527526
);
528527
// Resolve module using host module resolution strategy if provided otherwise use resolution cache to resolve module names
529528
compilerHost.resolveModuleNameLiterals = maybeBind(host, host.resolveModuleNameLiterals);

src/harness/incrementalUtils.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export function verifyResolutionCache(
206206
projectName: string,
207207
): void {
208208
const currentDirectory = resolutionHostCacheHost.getCurrentDirectory!();
209-
const expected = ts.createResolutionCache(resolutionHostCacheHost, actual.rootDirForResolution, /*logChangesWhenResolvingModule*/ false);
209+
const expected = ts.createResolutionCache(resolutionHostCacheHost, actual.rootDirForResolution);
210210
expected.startCachingPerDirectoryResolution();
211211

212212
type ExpectedResolution = ts.CachedResolvedModuleWithFailedLookupLocations & ts.CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations;
@@ -269,6 +269,14 @@ export function verifyResolutionCache(
269269
verifyFileWatchesOfAffectingLocations(expected.fileWatchesOfAffectingLocations, actual.fileWatchesOfAffectingLocations);
270270
verifyPackageDirWatchers(expected.packageDirWatchers, actual.packageDirWatchers);
271271
verifyDirPathToSymlinkPackageRefCount(expected.dirPathToSymlinkPackageRefCount, actual.dirPathToSymlinkPackageRefCount);
272+
ts.Debug.assert(
273+
expected.countResolutionsResolvedWithGlobalCache() === actual.countResolutionsResolvedWithGlobalCache(),
274+
`${projectName}:: Expected ResolutionsResolvedWithGlobalCache count ${expected.countResolutionsResolvedWithGlobalCache()} but got ${actual.countResolutionsResolvedWithGlobalCache()}`,
275+
);
276+
ts.Debug.assert(
277+
expected.countResolutionsResolvedWithoutGlobalCache() === actual.countResolutionsResolvedWithoutGlobalCache(),
278+
`${projectName}:: Expected ResolutionsResolvedWithoutGlobalCache count ${expected.countResolutionsResolvedWithoutGlobalCache()} but got ${actual.countResolutionsResolvedWithoutGlobalCache()}`,
279+
);
272280

273281
// Stop watching resolutions to verify everything gets closed.
274282
expected.startCachingPerDirectoryResolution();
@@ -284,6 +292,8 @@ export function verifyResolutionCache(
284292
ts.Debug.assert(expected.resolutionsWithOnlyAffectingLocations.size === 0, `${projectName}:: resolutionsWithOnlyAffectingLocations should be released`);
285293
ts.Debug.assert(expected.directoryWatchesOfFailedLookups.size === 0, `${projectName}:: directoryWatchesOfFailedLookups should be released`);
286294
ts.Debug.assert(expected.fileWatchesOfAffectingLocations.size === 0, `${projectName}:: fileWatchesOfAffectingLocations should be released`);
295+
ts.Debug.assert(expected.countResolutionsResolvedWithGlobalCache() === 0, `${projectName}:: ResolutionsResolvedWithGlobalCache should be cleared`);
296+
ts.Debug.assert(expected.countResolutionsResolvedWithoutGlobalCache() === 0, `${projectName}:: ResolutionsResolvedWithoutGlobalCache should be cleared`);
287297

288298
function collectResolutionToRefFromCache<T extends ts.ResolutionWithFailedLookupLocations>(
289299
cacheType: string,
@@ -329,6 +339,7 @@ export function verifyResolutionCache(
329339
failedLookupLocations: resolved.failedLookupLocations,
330340
affectingLocations: resolved.affectingLocations,
331341
alternateResult: resolved.alternateResult,
342+
globalCacheResolution: resolved.globalCacheResolution,
332343
};
333344
expectedToResolution.set(expectedResolution, resolved);
334345
resolutionToExpected.set(resolved, expectedResolution);

src/server/editorServices.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,11 +1595,7 @@ export class ProjectService {
15951595
switch (response.kind) {
15961596
case ActionSet:
15971597
// Update the typing files and update the project
1598-
project.updateTypingFiles(
1599-
response,
1600-
response.typings,
1601-
/*scheduleUpdate*/ true,
1602-
);
1598+
project.updateTypingFiles(response);
16031599
return;
16041600
case ActionInvalidate:
16051601
// Do not clear resolution cache, there was changes detected in typings, so enque typing request and let it get us correct results

0 commit comments

Comments
 (0)