Skip to content

Commit 00dbe24

Browse files
committed
If typing installer is disable invalidate all the resolutions from typings cache
1 parent c3b7b4b commit 00dbe24

11 files changed

+420
-635
lines changed

src/compiler/resolutionCache.ts

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ export interface ResolutionCache {
9999
resolutionsWithOnlyAffectingLocations: Set<ResolutionWithFailedLookupLocations>;
100100
directoryWatchesOfFailedLookups: Map<string, DirectoryWatchesOfFailedLookup>;
101101
fileWatchesOfAffectingLocations: Map<string, FileWatcherOfAffectingLocation>;
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, syncDirWatcherRemove?: boolean): void;
143147
removeResolutionsFromProjectReferenceRedirects(filePath: Path): void;
@@ -170,6 +174,7 @@ export interface ResolutionWithFailedLookupLocations {
170174
// Files that have this resolution using
171175
files?: Set<Path>;
172176
node10Result?: string;
177+
globalCacheResolution?: boolean;
173178
}
174179

175180
/** @internal */
@@ -490,13 +495,17 @@ function resolveModuleNameUsingGlobalCache(
490495
const host = getModuleResolutionHost(resolutionHost);
491496
const primaryResult = ts_resolveModuleName(moduleName, containingFile, compilerOptions, host, moduleResolutionCache, redirectedReference, mode);
492497
// return result immediately only if global cache support is not enabled or if it is .ts, .tsx or .d.ts
493-
if (!resolutionHost.getGlobalCache) {
498+
if (!resolutionHost.getGlobalCache || primaryResult.globalCacheResolution !== undefined) {
494499
return primaryResult;
495500
}
496501

497502
// otherwise try to load typings from @types
498503
const globalCache = resolutionHost.getGlobalCache();
499-
if (globalCache !== undefined && !isExternalModuleNameRelative(moduleName) && !(primaryResult.resolvedModule && extensionIsTS(primaryResult.resolvedModule.extension))) {
504+
if (!isExternalModuleNameRelative(moduleName) && !(primaryResult.resolvedModule && extensionIsTS(primaryResult.resolvedModule.extension))) {
505+
if (globalCache === undefined) {
506+
primaryResult.globalCacheResolution = false;
507+
return primaryResult;
508+
}
500509
// create different collection of failed lookup locations for second pass
501510
// if it will fail and we've already found something during the first pass - we don't want to pollute its results
502511
const { resolvedModule, failedLookupLocations, affectingLocations, resolutionDiagnostics } = loadModuleFromGlobalCache(
@@ -507,6 +516,7 @@ function resolveModuleNameUsingGlobalCache(
507516
globalCache,
508517
moduleResolutionCache,
509518
);
519+
primaryResult.globalCacheResolution = true;
510520
if (resolvedModule) {
511521
// Modify existing resolution so its saved in the directory cache as well
512522
(primaryResult.resolvedModule as any) = resolvedModule;
@@ -525,7 +535,10 @@ function resolveModuleNameUsingGlobalCache(
525535
export type GetResolutionWithResolvedFileName<T extends ResolutionWithFailedLookupLocations = ResolutionWithFailedLookupLocations, R extends ResolutionWithResolvedFileName = ResolutionWithResolvedFileName> = (resolution: T) => R | undefined;
526536

527537
/** @internal */
528-
export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootDirForResolution: string, logChangesWhenResolvingModule: boolean): ResolutionCache {
538+
export function createResolutionCache(
539+
resolutionHost: ResolutionCacheHost,
540+
rootDirForResolution: string,
541+
): ResolutionCache {
529542
let filesWithChangedSetOfUnresolvedImports: Path[] | undefined;
530543
let filesWithInvalidatedResolutions: Set<Path> | undefined;
531544
let filesWithInvalidatedNonRelativeUnresolvedImports: ReadonlyMap<Path, readonly string[]> | undefined;
@@ -543,6 +556,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
543556
let startsWithPathChecks: Set<Path> | undefined;
544557
let isInDirectoryChecks: Set<Path> | undefined;
545558
let allModuleAndTypeResolutionsAreInvalidated = false;
559+
let resolutionsWithGlobalCachePassAreInvalidated = false;
560+
let resolutionsWithoutGlobalCachePassAreInvalidated = false;
546561

547562
const getCurrentDirectory = memoize(() => resolutionHost.getCurrentDirectory!()); // TODO: GH#18217
548563
const cachedDirectoryStructureHost = resolutionHost.getCachedDirectoryStructureHost();
@@ -574,6 +589,9 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
574589
moduleResolutionCache.getPackageJsonInfoCache(),
575590
);
576591

592+
let resolutionsResolvedWithGlobalCache = 0;
593+
let resolutionsResolvedWithoutGlobalCache = 0;
594+
577595
const directoryWatchesOfFailedLookups = new Map<Path, DirectoryWatchesOfFailedLookup>();
578596
const fileWatchesOfAffectingLocations = new Map<string, FileWatcherOfAffectingLocation>();
579597
const rootDir = getRootDirectoryOfResolutionCache(rootDirForResolution, getCurrentDirectory);
@@ -593,6 +611,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
593611
resolutionsWithOnlyAffectingLocations,
594612
directoryWatchesOfFailedLookups,
595613
fileWatchesOfAffectingLocations,
614+
countResolutionsResolvedWithGlobalCache: () => resolutionsResolvedWithGlobalCache,
615+
countResolutionsResolvedWithoutGlobalCache: () => resolutionsResolvedWithoutGlobalCache,
596616
watchFailedLookupLocationsOfExternalModuleResolutions,
597617
getModuleResolutionCache: () => moduleResolutionCache,
598618
startRecordingFilesWithChangedResolutions,
@@ -610,6 +630,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
610630
hasChangedAutomaticTypeDirectiveNames: () => hasChangedAutomaticTypeDirectiveNames,
611631
invalidateResolutionOfFile,
612632
invalidateResolutionsOfFailedLookupLocations,
633+
invalidateResolutionsWithGlobalCachePass,
634+
invalidateResolutionsWithoutGlobalCachePass,
613635
setFilesWithInvalidatedNonRelativeUnresolvedImports,
614636
createHasInvalidatedResolutions,
615637
isFileWithInvalidatedNonRelativeUnresolvedImports,
@@ -637,12 +659,16 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
637659
resolvedFileToResolution.clear();
638660
resolutionsWithFailedLookups.clear();
639661
resolutionsWithOnlyAffectingLocations.clear();
662+
resolutionsResolvedWithGlobalCache = 0;
663+
resolutionsResolvedWithoutGlobalCache = 0;
640664
failedLookupChecks = undefined;
641665
startsWithPathChecks = undefined;
642666
isInDirectoryChecks = undefined;
643667
affectingPathChecks = undefined;
644668
affectingPathChecksForFile = undefined;
645669
allModuleAndTypeResolutionsAreInvalidated = false;
670+
resolutionsWithGlobalCachePassAreInvalidated = false;
671+
resolutionsWithoutGlobalCachePassAreInvalidated = false;
646672
moduleResolutionCache.clear();
647673
typeReferenceDirectiveResolutionCache.clear();
648674
moduleResolutionCache.update(resolutionHost.getCompilationSettings());
@@ -970,7 +996,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
970996
),
971997
getResolutionWithResolvedFileName: getResolvedModule,
972998
shouldRetryResolution: resolution => !resolution.resolvedModule || !resolutionExtensionIsTSOrJson(resolution.resolvedModule.extension),
973-
logChanges: logChangesWhenResolvingModule,
999+
logChanges: !!resolutionHost.getGlobalCache,
9741000
deferWatchingNonRelativeResolution: true, // Defer non relative resolution watch because we could be using ambient modules
9751001
});
9761002
}
@@ -1049,6 +1075,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
10491075
}
10501076
else {
10511077
resolution.refCount = 1;
1078+
if (resolution.globalCacheResolution) resolutionsResolvedWithGlobalCache++;
1079+
else if (resolution.globalCacheResolution === false) resolutionsResolvedWithoutGlobalCache++;
10521080
Debug.assert(!resolution.files?.size); // This resolution shouldnt be referenced by any file yet
10531081
if (!deferWatchingNonRelativeResolution || isExternalModuleNameRelative(name)) {
10541082
watchFailedLookupLocationOfResolution(resolution);
@@ -1241,6 +1269,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
12411269
if (resolution.refCount) {
12421270
return;
12431271
}
1272+
if (resolution.globalCacheResolution) resolutionsResolvedWithGlobalCache--;
1273+
if (resolution.globalCacheResolution === false) resolutionsResolvedWithoutGlobalCache--;
12441274
const resolved = getResolutionWithResolvedFileName(resolution);
12451275
if (resolved && resolved.resolvedFileName) {
12461276
const key = resolutionHost.toPath(resolved.resolvedFileName);
@@ -1421,6 +1451,13 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
14211451
}
14221452
}
14231453

1454+
function invalidateResolutionsWithGlobalCachePass() {
1455+
if (resolutionsResolvedWithGlobalCache) resolutionsWithGlobalCachePassAreInvalidated = true;
1456+
}
1457+
function invalidateResolutionsWithoutGlobalCachePass() {
1458+
if (resolutionsResolvedWithoutGlobalCache) resolutionsWithoutGlobalCachePassAreInvalidated = true;
1459+
}
1460+
14241461
function invalidateResolutionsOfFailedLookupLocations() {
14251462
if (allModuleAndTypeResolutionsAreInvalidated) {
14261463
affectingPathChecksForFile = undefined;
@@ -1432,6 +1469,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
14321469
startsWithPathChecks = undefined;
14331470
isInDirectoryChecks = undefined;
14341471
affectingPathChecks = undefined;
1472+
resolutionsWithGlobalCachePassAreInvalidated = false;
1473+
resolutionsWithoutGlobalCachePassAreInvalidated = false;
14351474
return true;
14361475
}
14371476
let invalidated = false;
@@ -1445,7 +1484,10 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
14451484
affectingPathChecksForFile = undefined;
14461485
}
14471486

1448-
if (!failedLookupChecks && !startsWithPathChecks && !isInDirectoryChecks && !affectingPathChecks) {
1487+
if (
1488+
!failedLookupChecks && !startsWithPathChecks && !isInDirectoryChecks && !affectingPathChecks &&
1489+
!resolutionsWithGlobalCachePassAreInvalidated && !resolutionsWithoutGlobalCachePassAreInvalidated
1490+
) {
14491491
return invalidated;
14501492
}
14511493

@@ -1456,6 +1498,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
14561498
isInDirectoryChecks = undefined;
14571499
invalidated = invalidateResolutions(resolutionsWithOnlyAffectingLocations, canInvalidatedFailedLookupResolutionWithAffectingLocation) || invalidated;
14581500
affectingPathChecks = undefined;
1501+
resolutionsWithGlobalCachePassAreInvalidated = false;
1502+
resolutionsWithoutGlobalCachePassAreInvalidated = false;
14591503
return invalidated;
14601504
}
14611505

@@ -1475,6 +1519,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
14751519
}
14761520

14771521
function canInvalidatedFailedLookupResolutionWithAffectingLocation(resolution: ResolutionWithFailedLookupLocations) {
1522+
if (resolutionsWithGlobalCachePassAreInvalidated && resolution.globalCacheResolution) return true;
1523+
if (resolutionsWithoutGlobalCachePassAreInvalidated && resolution.globalCacheResolution === false) return true;
14781524
return !!affectingPathChecks && resolution.affectingLocations?.some(location => affectingPathChecks!.has(location));
14791525
}
14801526

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7714,6 +7714,8 @@ export interface ResolvedModuleWithFailedLookupLocations {
77147714
* while respecting package.json `exports`, but were found when disabling `exports`.
77157715
*/
77167716
node10Result?: string;
7717+
/** @internal */
7718+
globalCacheResolution?: boolean;
77177719
}
77187720

77197721
export interface ResolvedTypeReferenceDirective {

src/compiler/watchPublic.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,6 @@ export function createWatchProgram<T extends BuilderProgram>(host: WatchCompiler
516516
configFileName ?
517517
getDirectoryPath(getNormalizedAbsolutePath(configFileName, currentDirectory)) :
518518
currentDirectory,
519-
/*logChangesWhenResolvingModule*/ false,
520519
);
521520
// Resolve module using host module resolution strategy if provided otherwise use resolution cache to resolve module names
522521
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
) {
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;
@@ -271,6 +271,14 @@ export function verifyResolutionCache(
271271
verifyResolutionSet(expected.resolutionsWithOnlyAffectingLocations, actual.resolutionsWithOnlyAffectingLocations, `resolutionsWithOnlyAffectingLocations`);
272272
verifyDirectoryWatchesOfFailedLookups(expected.directoryWatchesOfFailedLookups, actual.directoryWatchesOfFailedLookups);
273273
verifyFileWatchesOfAffectingLocations(expected.fileWatchesOfAffectingLocations, actual.fileWatchesOfAffectingLocations);
274+
ts.Debug.assert(
275+
expected.countResolutionsResolvedWithGlobalCache() === actual.countResolutionsResolvedWithGlobalCache(),
276+
`${projectName}:: Expected ResolutionsResolvedWithGlobalCache count ${expected.countResolutionsResolvedWithGlobalCache()} but got ${actual.countResolutionsResolvedWithGlobalCache()}`,
277+
);
278+
ts.Debug.assert(
279+
expected.countResolutionsResolvedWithoutGlobalCache() === actual.countResolutionsResolvedWithoutGlobalCache(),
280+
`${projectName}:: Expected ResolutionsResolvedWithoutGlobalCache count ${expected.countResolutionsResolvedWithoutGlobalCache()} but got ${actual.countResolutionsResolvedWithoutGlobalCache()}`,
281+
);
274282

275283
// Stop watching resolutions to verify everything gets closed.
276284
expected.startCachingPerDirectoryResolution();
@@ -287,6 +295,8 @@ export function verifyResolutionCache(
287295
ts.Debug.assert(expected.resolutionsWithOnlyAffectingLocations.size === 0, `${projectName}:: resolutionsWithOnlyAffectingLocations should be released`);
288296
ts.Debug.assert(expected.directoryWatchesOfFailedLookups.size === 0, `${projectName}:: directoryWatchesOfFailedLookups should be released`);
289297
ts.Debug.assert(expected.fileWatchesOfAffectingLocations.size === 0, `${projectName}:: fileWatchesOfAffectingLocations should be released`);
298+
ts.Debug.assert(expected.countResolutionsResolvedWithGlobalCache() === 0, `${projectName}:: ResolutionsResolvedWithGlobalCache should be cleared`);
299+
ts.Debug.assert(expected.countResolutionsResolvedWithoutGlobalCache() === 0, `${projectName}:: ResolutionsResolvedWithoutGlobalCache should be cleared`);
290300

291301
function collectResolutionToRefFromCache<T extends ts.ResolutionWithFailedLookupLocations>(
292302
cacheType: string,
@@ -332,6 +342,7 @@ export function verifyResolutionCache(
332342
failedLookupLocations: resolved.failedLookupLocations,
333343
affectingLocations: resolved.affectingLocations,
334344
node10Result: resolved.node10Result,
345+
globalCacheResolution: resolved.globalCacheResolution,
335346
};
336347
expectedToResolution.set(expectedResolution, resolved);
337348
resolutionToExpected.set(resolved, expectedResolution);

src/server/editorServices.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,12 +1297,7 @@ export class ProjectService {
12971297
switch (response.kind) {
12981298
case ActionSet:
12991299
// Update the typing files and update the project
1300-
project.updateTypingsForProject(
1301-
response.compilerOptions,
1302-
response.typeAcquisition,
1303-
response.unresolvedImports,
1304-
response.typings,
1305-
);
1300+
project.updateTypingsForProject(response);
13061301
return;
13071302
case ActionInvalidate:
13081303
// 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)