@@ -11,6 +11,7 @@ namespace ts {
1111
1212 invalidateResolutionsOfFailedLookupLocations ( ) : boolean ;
1313 invalidateResolutionOfFile ( filePath : Path ) : void ;
14+ removeRelativeNoResolveResolutionsOfFile ( filePath : Path ) : boolean ;
1415 removeResolutionsOfFile ( filePath : Path ) : void ;
1516 removeResolutionsFromProjectReferenceRedirects ( filePath : Path ) : void ;
1617 setFilesWithInvalidatedNonRelativeUnresolvedImports ( filesWithUnresolvedImports : ESMap < Path , readonly string [ ] > ) : void ;
@@ -141,7 +142,21 @@ namespace ts {
141142 type GetResolutionWithResolvedFileName < T extends ResolutionWithFailedLookupLocations = ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName = ResolutionWithResolvedFileName > =
142143 ( resolution : T ) => R | undefined ;
143144
144- export function createResolutionCache ( resolutionHost : ResolutionCacheHost , rootDirForResolution : string | undefined , logChangesWhenResolvingModule : boolean ) : ResolutionCache {
145+ export enum ResolutionKind {
146+ All ,
147+ RelativeReferencesInOpenFileOnly
148+ }
149+
150+ const noResolveResolvedModule : ResolvedModuleWithFailedLookupLocations = {
151+ resolvedModule : undefined ,
152+ failedLookupLocations : [ ]
153+ } ;
154+ const noResolveResolvedTypeReferenceDirective : ResolvedTypeReferenceDirectiveWithFailedLookupLocations = {
155+ resolvedTypeReferenceDirective : undefined ,
156+ failedLookupLocations : [ ]
157+ } ;
158+
159+ export function createResolutionCache ( resolutionHost : ResolutionCacheHost , rootDirForResolution : string | undefined , resolutionKind : ResolutionKind , logChangesWhenResolvingModule : boolean ) : ResolutionCache {
145160 let filesWithChangedSetOfUnresolvedImports : Path [ ] | undefined ;
146161 let filesWithInvalidatedResolutions : Set < Path > | undefined ;
147162 let filesWithInvalidatedNonRelativeUnresolvedImports : ReadonlyESMap < Path , readonly string [ ] > | undefined ;
@@ -206,6 +221,7 @@ namespace ts {
206221 hasChangedAutomaticTypeDirectiveNames : ( ) => hasChangedAutomaticTypeDirectiveNames ,
207222 invalidateResolutionOfFile,
208223 invalidateResolutionsOfFailedLookupLocations,
224+ removeRelativeNoResolveResolutionsOfFile,
209225 setFilesWithInvalidatedNonRelativeUnresolvedImports,
210226 createHasInvalidatedResolution,
211227 updateTypeRootsWatch,
@@ -341,11 +357,12 @@ namespace ts {
341357 shouldRetryResolution : ( t : T ) => boolean ;
342358 reusedNames ?: readonly string [ ] ;
343359 logChanges ?: boolean ;
360+ noResolveResolution : T ;
344361 }
345362 function resolveNamesWithLocalCache < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > ( {
346363 names, containingFile, redirectedReference,
347364 cache, perDirectoryCacheWithRedirects,
348- loader, getResolutionWithResolvedFileName,
365+ loader, getResolutionWithResolvedFileName, noResolveResolution ,
349366 shouldRetryResolution, reusedNames, logChanges
350367 } : ResolveNamesWithLocalCacheInput < T , R > ) : ( R | undefined ) [ ] {
351368 const path = resolutionHost . toPath ( containingFile ) ;
@@ -382,7 +399,10 @@ namespace ts {
382399 resolution = resolutionInDirectory ;
383400 }
384401 else {
385- resolution = loader ( name , containingFile , compilerOptions , resolutionHost . getCompilerHost ?.( ) || resolutionHost , redirectedReference ) ;
402+ resolution = resolutionKind === ResolutionKind . All ||
403+ ( isExternalModuleNameRelative ( name ) && resolutionHost . fileIsOpen ( path ) ) ?
404+ loader ( name , containingFile , compilerOptions , resolutionHost . getCompilerHost ?.( ) || resolutionHost , redirectedReference ) :
405+ noResolveResolution ;
386406 perDirectoryResolution . set ( name , resolution ) ;
387407 }
388408 resolutionsInFile . set ( name , resolution ) ;
@@ -441,6 +461,7 @@ namespace ts {
441461 loader : resolveTypeReferenceDirective ,
442462 getResolutionWithResolvedFileName : getResolvedTypeReferenceDirective ,
443463 shouldRetryResolution : resolution => resolution . resolvedTypeReferenceDirective === undefined ,
464+ noResolveResolution : noResolveResolvedTypeReferenceDirective ,
444465 } ) ;
445466 }
446467
@@ -455,7 +476,8 @@ namespace ts {
455476 getResolutionWithResolvedFileName : getResolvedModule ,
456477 shouldRetryResolution : resolution => ! resolution . resolvedModule || ! resolutionExtensionIsTSOrJson ( resolution . resolvedModule . extension ) ,
457478 reusedNames,
458- logChanges : logChangesWhenResolvingModule
479+ logChanges : logChangesWhenResolvingModule ,
480+ noResolveResolution : noResolveResolvedModule ,
459481 } ) ;
460482 }
461483
@@ -741,6 +763,31 @@ namespace ts {
741763 }
742764 }
743765
766+ function removeRelativeNoResolveResolutionsOfFileFromCache < T extends ResolutionWithFailedLookupLocations > (
767+ cache : ESMap < Path , ESMap < string , T > > ,
768+ filePath : Path ,
769+ noResolveResolution : T ,
770+ ) {
771+ Debug . assert ( resolutionKind === ResolutionKind . RelativeReferencesInOpenFileOnly ) ;
772+ // Deleted file, stop watching failed lookups for all the resolutions in the file
773+ const resolutions = cache . get ( filePath ) ;
774+ if ( ! resolutions ) return false ;
775+ let invalidated = false ;
776+ resolutions . forEach ( ( resolution , name ) => {
777+ if ( resolution === noResolveResolution && isExternalModuleNameRelative ( name ) ) {
778+ resolutions . delete ( name ) ;
779+ invalidated = true ;
780+ }
781+ } ) ;
782+ return invalidated ;
783+ }
784+
785+ function removeRelativeNoResolveResolutionsOfFile ( filePath : Path ) {
786+ let invalidated = removeRelativeNoResolveResolutionsOfFileFromCache ( resolvedModuleNames , filePath , noResolveResolvedModule ) ;
787+ invalidated = removeRelativeNoResolveResolutionsOfFileFromCache ( resolvedTypeReferenceDirectives , filePath , noResolveResolvedTypeReferenceDirective ) || invalidated ;
788+ return invalidated ;
789+ }
790+
744791 function setFilesWithInvalidatedNonRelativeUnresolvedImports ( filesMap : ReadonlyESMap < Path , readonly string [ ] > ) {
745792 Debug . assert ( filesWithInvalidatedNonRelativeUnresolvedImports === filesMap || filesWithInvalidatedNonRelativeUnresolvedImports === undefined ) ;
746793 filesWithInvalidatedNonRelativeUnresolvedImports = filesMap ;
0 commit comments