diff --git a/src/compiler/resolutionCache.ts b/src/compiler/resolutionCache.ts index cbfa95b27d537..8f2bc04a30ff0 100644 --- a/src/compiler/resolutionCache.ts +++ b/src/compiler/resolutionCache.ts @@ -167,7 +167,6 @@ export interface ResolutionWithFailedLookupLocations { failedLookupLocations?: string[]; affectingLocations?: string[]; isInvalidated?: boolean; - refCount?: number; // Files that have this resolution using files?: Set; alternateResult?: string; @@ -1107,28 +1106,21 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD getResolutionWithResolvedFileName: GetResolutionWithResolvedFileName, deferWatchingNonRelativeResolution: boolean, ) { - if (resolution.refCount) { - resolution.refCount++; - Debug.assertIsDefined(resolution.files); + (resolution.files ??= new Set()).add(filePath); + if (resolution.files.size !== 1) return; + if (!deferWatchingNonRelativeResolution || isExternalModuleNameRelative(name)) { + watchFailedLookupLocationOfResolution(resolution); } else { - resolution.refCount = 1; - Debug.assert(!resolution.files?.size); // This resolution shouldnt be referenced by any file yet - if (!deferWatchingNonRelativeResolution || isExternalModuleNameRelative(name)) { - watchFailedLookupLocationOfResolution(resolution); - } - else { - nonRelativeExternalModuleResolutions.add(name, resolution); - } - const resolved = getResolutionWithResolvedFileName(resolution); - if (resolved && resolved.resolvedFileName) { - const key = resolutionHost.toPath(resolved.resolvedFileName); - let resolutions = resolvedFileToResolution.get(key); - if (!resolutions) resolvedFileToResolution.set(key, resolutions = new Set()); - resolutions.add(resolution); - } + nonRelativeExternalModuleResolutions.add(name, resolution); + } + const resolved = getResolutionWithResolvedFileName(resolution); + if (resolved && resolved.resolvedFileName) { + const key = resolutionHost.toPath(resolved.resolvedFileName); + let resolutions = resolvedFileToResolution.get(key); + if (!resolutions) resolvedFileToResolution.set(key, resolutions = new Set()); + resolutions.add(resolution); } - (resolution.files ??= new Set()).add(filePath); } function watchFailedLookupLocation(failedLookupLocation: string, setAtRoot: boolean) { @@ -1156,7 +1148,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD } function watchFailedLookupLocationOfResolution(resolution: ResolutionWithFailedLookupLocations) { - Debug.assert(!!resolution.refCount); + Debug.assert(!!resolution.files?.size); const { failedLookupLocations, affectingLocations, alternateResult } = resolution; if (!failedLookupLocations?.length && !affectingLocations?.length && !alternateResult) return; @@ -1177,7 +1169,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD } function watchAffectingLocationsOfResolution(resolution: ResolutionWithFailedLookupLocations, addToResolutionsWithOnlyAffectingLocations: boolean) { - Debug.assert(!!resolution.refCount); + Debug.assert(!!resolution.files?.size); const { affectingLocations } = resolution; if (!affectingLocations?.length) return; if (addToResolutionsWithOnlyAffectingLocations) resolutionsWithOnlyAffectingLocations.add(resolution); @@ -1393,10 +1385,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD syncDirWatcherRemove?: boolean, ) { Debug.checkDefined(resolution.files).delete(filePath); - resolution.refCount!--; - if (resolution.refCount) { - return; - } + if (resolution.files!.size) return; + resolution.files = undefined; const resolved = getResolutionWithResolvedFileName(resolution); if (resolved && resolved.resolvedFileName) { const key = resolutionHost.toPath(resolved.resolvedFileName); diff --git a/src/harness/incrementalUtils.ts b/src/harness/incrementalUtils.ts index ae48d0bed6694..2232a2bb771f9 100644 --- a/src/harness/incrementalUtils.ts +++ b/src/harness/incrementalUtils.ts @@ -254,15 +254,11 @@ export function verifyResolutionCache( // Verify ref count resolutionToRefs.forEach((info, resolution) => { ts.Debug.assert( - resolution.refCount === info.length, - `${projectName}:: Expected Resolution ref count ${info.length} but got ${resolution.refCount}`, + resolution.files?.size === info.length, + `${projectName}:: Expected Resolution ref count ${info.length} but got ${resolution.files?.size}`, () => `Expected from:: ${JSON.stringify(info, undefined, " ")}` + - `Actual from: ${resolution.refCount}`, - ); - ts.Debug.assert( - resolutionToExpected.get(resolution)!.refCount === resolution.refCount, - `${projectName}:: Expected Resolution ref count ${resolutionToExpected.get(resolution)!.refCount} but got ${resolution.refCount}`, + `Actual from: ${resolution.files?.size}`, ); verifySet(resolutionToExpected.get(resolution)!.files, resolution.files, `${projectName}:: Resolution files`); }); @@ -280,10 +276,9 @@ export function verifyResolutionCache( actual.resolvedTypeReferenceDirectives.forEach((_resolutions, path) => expected.removeResolutionsOfFile(path)); expected.finishCachingPerDirectoryResolution(/*newProgram*/ undefined, actualProgram); - resolutionToExpected.forEach(expected => { - ts.Debug.assert(!expected.refCount, `${projectName}:: All the resolution should be released`); - ts.Debug.assert(!expected.files?.size, `${projectName}:: Shouldnt ref to any files`); - }); + resolutionToExpected.forEach( + expected => ts.Debug.assert(!expected.files?.size, `${projectName}:: Shouldnt ref to any files`), + ); ts.Debug.assert(expected.resolvedFileToResolution.size === 0, `${projectName}:: resolvedFileToResolution should be released`); ts.Debug.assert(expected.resolutionsWithFailedLookups.size === 0, `${projectName}:: resolutionsWithFailedLookups should be released`); ts.Debug.assert(expected.resolutionsWithOnlyAffectingLocations.size === 0, `${projectName}:: resolutionsWithOnlyAffectingLocations should be released`);