@@ -81,6 +81,7 @@ namespace ts {
81
81
let filesWithInvalidatedResolutions : Map < true > | undefined ;
82
82
let filesWithInvalidatedNonRelativeUnresolvedImports : Map < ReadonlyArray < string > > | undefined ;
83
83
let allFilesHaveInvalidatedResolution = false ;
84
+ const nonRelativeExternalModuleResolutions = createMultiMap < ResolutionWithFailedLookupLocations > ( ) ;
84
85
85
86
const getCurrentDirectory = memoize ( ( ) => resolutionHost . getCurrentDirectory ! ( ) ) ; // TODO: GH#18217
86
87
const cachedDirectoryStructureHost = resolutionHost . getCachedDirectoryStructureHost ( ) ;
@@ -154,6 +155,7 @@ namespace ts {
154
155
function clear ( ) {
155
156
clearMap ( directoryWatchesOfFailedLookups , closeFileWatcherOf ) ;
156
157
customFailedLookupPaths . clear ( ) ;
158
+ nonRelativeExternalModuleResolutions . clear ( ) ;
157
159
closeTypeRootsWatch ( ) ;
158
160
resolvedModuleNames . clear ( ) ;
159
161
resolvedTypeReferenceDirectives . clear ( ) ;
@@ -199,19 +201,20 @@ namespace ts {
199
201
perDirectoryResolvedModuleNames . clear ( ) ;
200
202
nonRelaticeModuleNameCache . clear ( ) ;
201
203
perDirectoryResolvedTypeReferenceDirectives . clear ( ) ;
204
+ nonRelativeExternalModuleResolutions . forEach ( watchFailedLookupLocationOfNonRelativeModuleResolutions ) ;
205
+ nonRelativeExternalModuleResolutions . clear ( ) ;
202
206
}
203
207
204
208
function finishCachingPerDirectoryResolution ( ) {
205
209
allFilesHaveInvalidatedResolution = false ;
206
210
filesWithInvalidatedNonRelativeUnresolvedImports = undefined ;
211
+ clearPerDirectoryResolutions ( ) ;
207
212
directoryWatchesOfFailedLookups . forEach ( ( watcher , path ) => {
208
213
if ( watcher . refCount === 0 ) {
209
214
directoryWatchesOfFailedLookups . delete ( path ) ;
210
215
watcher . watcher . close ( ) ;
211
216
}
212
217
} ) ;
213
-
214
- clearPerDirectoryResolutions ( ) ;
215
218
}
216
219
217
220
function resolveModuleName ( moduleName : string , containingFile : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost ) : CachedResolvedModuleWithFailedLookupLocations {
@@ -275,7 +278,7 @@ namespace ts {
275
278
perDirectoryResolution . set ( name , resolution ) ;
276
279
}
277
280
resolutionsInFile . set ( name , resolution ) ;
278
- watchFailedLookupLocationOfResolution ( resolution ) ;
281
+ watchFailedLookupLocationsOfExternalModuleResolutions ( name , resolution ) ;
279
282
if ( existingResolution ) {
280
283
stopWatchFailedLookupLocationOfResolution ( existingResolution ) ;
281
284
}
@@ -441,18 +444,27 @@ namespace ts {
441
444
return fileExtensionIsOneOf ( path , failedLookupDefaultExtensions ) ;
442
445
}
443
446
444
- function watchFailedLookupLocationOfResolution ( resolution : ResolutionWithFailedLookupLocations ) {
447
+ function watchFailedLookupLocationsOfExternalModuleResolutions ( name : string , resolution : ResolutionWithFailedLookupLocations ) {
445
448
// No need to set the resolution refCount
446
- if ( ! resolution . failedLookupLocations || ! resolution . failedLookupLocations . length ) {
447
- return ;
449
+ if ( resolution . failedLookupLocations && resolution . failedLookupLocations . length ) {
450
+ if ( resolution . refCount ) {
451
+ resolution . refCount ++ ;
452
+ }
453
+ else {
454
+ resolution . refCount = 1 ;
455
+ if ( isExternalModuleNameRelative ( name ) ) {
456
+ watchFailedLookupLocationOfResolution ( resolution ) ;
457
+ }
458
+ else {
459
+ nonRelativeExternalModuleResolutions . add ( name , resolution ) ;
460
+ }
461
+ }
448
462
}
463
+ }
449
464
450
- if ( resolution . refCount !== undefined ) {
451
- resolution . refCount ++ ;
452
- return ;
453
- }
465
+ function watchFailedLookupLocationOfResolution ( resolution : ResolutionWithFailedLookupLocations ) {
466
+ Debug . assert ( ! ! resolution . refCount ) ;
454
467
455
- resolution . refCount = 1 ;
456
468
const { failedLookupLocations } = resolution ;
457
469
let setAtRoot = false ;
458
470
for ( const failedLookupLocation of failedLookupLocations ) {
@@ -480,6 +492,16 @@ namespace ts {
480
492
}
481
493
}
482
494
495
+ function setRefCountToUndefined ( resolution : ResolutionWithFailedLookupLocations ) {
496
+ resolution . refCount = undefined ;
497
+ }
498
+
499
+ function watchFailedLookupLocationOfNonRelativeModuleResolutions ( resolutions : ResolutionWithFailedLookupLocations [ ] , name : string ) {
500
+ const updateResolution = resolutionHost . getCurrentProgram ( ) . getTypeChecker ( ) . tryFindAmbientModuleWithoutAugmentations ( name ) ?
501
+ setRefCountToUndefined : watchFailedLookupLocationOfResolution ;
502
+ resolutions . forEach ( updateResolution ) ;
503
+ }
504
+
483
505
function setDirectoryWatcher ( dir : string , dirPath : Path , nonRecursive ?: boolean ) {
484
506
const dirWatcher = directoryWatchesOfFailedLookups . get ( dirPath ) ;
485
507
if ( dirWatcher ) {
@@ -492,11 +514,11 @@ namespace ts {
492
514
}
493
515
494
516
function stopWatchFailedLookupLocationOfResolution ( resolution : ResolutionWithFailedLookupLocations ) {
495
- if ( ! resolution . failedLookupLocations || ! resolution . failedLookupLocations . length ) {
517
+ if ( ! resolution . refCount ) {
496
518
return ;
497
519
}
498
520
499
- resolution . refCount ! -- ;
521
+ resolution . refCount -- ;
500
522
if ( resolution . refCount ) {
501
523
return ;
502
524
}
0 commit comments