@@ -5,12 +5,13 @@ namespace ts {
55 startRecordingFilesWithChangedResolutions ( ) : void ;
66 finishRecordingFilesWithChangedResolutions ( ) : Path [ ] | undefined ;
77
8- resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined ) : ResolvedModuleFull [ ] ;
8+ resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined , redirectedReference ?: ResolvedProjectReference ) : ResolvedModuleFull [ ] ;
99 getResolvedModuleWithFailedLookupLocationsFromCache ( moduleName : string , containingFile : string ) : CachedResolvedModuleWithFailedLookupLocations | undefined ;
10- resolveTypeReferenceDirectives ( typeDirectiveNames : string [ ] , containingFile : string ) : ResolvedTypeReferenceDirective [ ] ;
10+ resolveTypeReferenceDirectives ( typeDirectiveNames : string [ ] , containingFile : string , redirectedReference ?: ResolvedProjectReference ) : ResolvedTypeReferenceDirective [ ] ;
1111
1212 invalidateResolutionOfFile ( filePath : Path ) : void ;
1313 removeResolutionsOfFile ( filePath : Path ) : void ;
14+ removeResolutionsFromProjectReferenceRedirects ( filePath : Path ) : void ;
1415 setFilesWithInvalidatedNonRelativeUnresolvedImports ( filesWithUnresolvedImports : Map < ReadonlyArray < string > > ) : void ;
1516 createHasInvalidatedResolution ( forceAllFilesAsInvalidated ?: boolean ) : HasInvalidatedResolution ;
1617
@@ -90,17 +91,17 @@ namespace ts {
9091 // The key in the map is source file's path.
9192 // The values are Map of resolutions with key being name lookedup.
9293 const resolvedModuleNames = createMap < Map < CachedResolvedModuleWithFailedLookupLocations > > ( ) ;
93- const perDirectoryResolvedModuleNames = createMap < Map < CachedResolvedModuleWithFailedLookupLocations > > ( ) ;
94- const nonRelaticeModuleNameCache = createMap < PerModuleNameCache > ( ) ;
94+ const perDirectoryResolvedModuleNames : CacheWithRedirects < Map < CachedResolvedModuleWithFailedLookupLocations > > = createCacheWithRedirects ( ) ;
95+ const nonRelativeModuleNameCache : CacheWithRedirects < PerModuleNameCache > = createCacheWithRedirects ( ) ;
9596 const moduleResolutionCache = createModuleResolutionCacheWithMaps (
9697 perDirectoryResolvedModuleNames ,
97- nonRelaticeModuleNameCache ,
98+ nonRelativeModuleNameCache ,
9899 getCurrentDirectory ( ) ,
99100 resolutionHost . getCanonicalFileName
100101 ) ;
101102
102103 const resolvedTypeReferenceDirectives = createMap < Map < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > ( ) ;
103- const perDirectoryResolvedTypeReferenceDirectives = createMap < Map < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > ( ) ;
104+ const perDirectoryResolvedTypeReferenceDirectives : CacheWithRedirects < Map < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > = createCacheWithRedirects ( ) ;
104105
105106 /**
106107 * These are the extensions that failed lookup files will have by default,
@@ -128,6 +129,7 @@ namespace ts {
128129 resolveModuleNames,
129130 getResolvedModuleWithFailedLookupLocationsFromCache,
130131 resolveTypeReferenceDirectives,
132+ removeResolutionsFromProjectReferenceRedirects,
131133 removeResolutionsOfFile,
132134 invalidateResolutionOfFile,
133135 setFilesWithInvalidatedNonRelativeUnresolvedImports,
@@ -199,7 +201,7 @@ namespace ts {
199201
200202 function clearPerDirectoryResolutions ( ) {
201203 perDirectoryResolvedModuleNames . clear ( ) ;
202- nonRelaticeModuleNameCache . clear ( ) ;
204+ nonRelativeModuleNameCache . clear ( ) ;
203205 perDirectoryResolvedTypeReferenceDirectives . clear ( ) ;
204206 nonRelativeExternalModuleResolutions . forEach ( watchFailedLookupLocationOfNonRelativeModuleResolutions ) ;
205207 nonRelativeExternalModuleResolutions . clear ( ) ;
@@ -217,8 +219,8 @@ namespace ts {
217219 } ) ;
218220 }
219221
220- function resolveModuleName ( moduleName : string , containingFile : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost ) : CachedResolvedModuleWithFailedLookupLocations {
221- const primaryResult = ts . resolveModuleName ( moduleName , containingFile , compilerOptions , host , moduleResolutionCache ) ;
222+ function resolveModuleName ( moduleName : string , containingFile : string , compilerOptions : CompilerOptions , host : ModuleResolutionHost , redirectedReference ?: ResolvedProjectReference ) : CachedResolvedModuleWithFailedLookupLocations {
223+ const primaryResult = ts . resolveModuleName ( moduleName , containingFile , compilerOptions , host , moduleResolutionCache , redirectedReference ) ;
222224 // return result immediately only if global cache support is not enabled or if it is .ts, .tsx or .d.ts
223225 if ( ! resolutionHost . getGlobalCache ) {
224226 return primaryResult ;
@@ -242,16 +244,18 @@ namespace ts {
242244 function resolveNamesWithLocalCache < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
243245 names : string [ ] ,
244246 containingFile : string ,
247+ redirectedReference : ResolvedProjectReference | undefined ,
245248 cache : Map < Map < T > > ,
246- perDirectoryCache : Map < Map < T > > ,
247- loader : ( name : string , containingFile : string , options : CompilerOptions , host : ModuleResolutionHost ) => T ,
249+ perDirectoryCacheWithRedirects : CacheWithRedirects < Map < T > > ,
250+ loader : ( name : string , containingFile : string , options : CompilerOptions , host : ModuleResolutionHost , redirectedReference ?: ResolvedProjectReference ) => T ,
248251 getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
249252 reusedNames : string [ ] | undefined ,
250253 logChanges : boolean ) : R [ ] {
251254
252255 const path = resolutionHost . toPath ( containingFile ) ;
253256 const resolutionsInFile = cache . get ( path ) || cache . set ( path , createMap ( ) ) . get ( path ) ! ;
254257 const dirPath = getDirectoryPath ( path ) ;
258+ const perDirectoryCache = perDirectoryCacheWithRedirects . getOrCreateMapOfCacheRedirects ( redirectedReference ) ;
255259 let perDirectoryResolution = perDirectoryCache . get ( dirPath ) ;
256260 if ( ! perDirectoryResolution ) {
257261 perDirectoryResolution = createMap ( ) ;
@@ -260,12 +264,20 @@ namespace ts {
260264 const resolvedModules : R [ ] = [ ] ;
261265 const compilerOptions = resolutionHost . getCompilationSettings ( ) ;
262266 const hasInvalidatedNonRelativeUnresolvedImport = logChanges && isFileWithInvalidatedNonRelativeUnresolvedImports ( path ) ;
267+
268+ // All the resolutions in this file are invalidated if this file wasnt resolved using same redirect
269+ const program = resolutionHost . getCurrentProgram ( ) ;
270+ const oldRedirect = program && program . getResolvedProjectReferenceToRedirect ( containingFile ) ;
271+ const unmatchedRedirects = oldRedirect ?
272+ ! redirectedReference || redirectedReference . sourceFile . path !== oldRedirect . sourceFile . path :
273+ ! ! redirectedReference ;
274+
263275 const seenNamesInFile = createMap < true > ( ) ;
264276 for ( const name of names ) {
265277 let resolution = resolutionsInFile . get ( name ) ;
266278 // Resolution is valid if it is present and not invalidated
267279 if ( ! seenNamesInFile . has ( name ) &&
268- allFilesHaveInvalidatedResolution || ! resolution || resolution . isInvalidated ||
280+ allFilesHaveInvalidatedResolution || unmatchedRedirects || ! resolution || resolution . isInvalidated ||
269281 // If the name is unresolved import that was invalidated, recalculate
270282 ( hasInvalidatedNonRelativeUnresolvedImport && ! isExternalModuleNameRelative ( name ) && ! getResolutionWithResolvedFileName ( resolution ) ) ) {
271283 const existingResolution = resolution ;
@@ -274,7 +286,7 @@ namespace ts {
274286 resolution = resolutionInDirectory ;
275287 }
276288 else {
277- resolution = loader ( name , containingFile , compilerOptions , resolutionHost ) ;
289+ resolution = loader ( name , containingFile , compilerOptions , resolutionHost , redirectedReference ) ;
278290 perDirectoryResolution . set ( name , resolution ) ;
279291 }
280292 resolutionsInFile . set ( name , resolution ) ;
@@ -323,18 +335,18 @@ namespace ts {
323335 }
324336 }
325337
326- function resolveTypeReferenceDirectives ( typeDirectiveNames : string [ ] , containingFile : string ) : ResolvedTypeReferenceDirective [ ] {
338+ function resolveTypeReferenceDirectives ( typeDirectiveNames : string [ ] , containingFile : string , redirectedReference ?: ResolvedProjectReference ) : ResolvedTypeReferenceDirective [ ] {
327339 return resolveNamesWithLocalCache < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations , ResolvedTypeReferenceDirective > (
328- typeDirectiveNames , containingFile ,
340+ typeDirectiveNames , containingFile , redirectedReference ,
329341 resolvedTypeReferenceDirectives , perDirectoryResolvedTypeReferenceDirectives ,
330342 resolveTypeReferenceDirective , getResolvedTypeReferenceDirective ,
331343 /*reusedNames*/ undefined , /*logChanges*/ false
332344 ) ;
333345 }
334346
335- function resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined ) : ResolvedModuleFull [ ] {
347+ function resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined , redirectedReference ?: ResolvedProjectReference ) : ResolvedModuleFull [ ] {
336348 return resolveNamesWithLocalCache < CachedResolvedModuleWithFailedLookupLocations , ResolvedModuleFull > (
337- moduleNames , containingFile ,
349+ moduleNames , containingFile , redirectedReference ,
338350 resolvedModuleNames , perDirectoryResolvedModuleNames ,
339351 resolveModuleName , getResolvedModule ,
340352 reusedNames , logChangesWhenResolvingModule
@@ -594,6 +606,20 @@ namespace ts {
594606 }
595607 }
596608
609+ function removeResolutionsFromProjectReferenceRedirects ( filePath : Path ) {
610+ if ( ! fileExtensionIs ( filePath , Extension . Json ) ) { return ; }
611+
612+ const program = resolutionHost . getCurrentProgram ( ) ;
613+ if ( ! program ) { return ; }
614+
615+ // If this file is input file for the referenced project, get it
616+ const resolvedProjectReference = program . getResolvedProjectReferenceByPath ( filePath ) ;
617+ if ( ! resolvedProjectReference ) { return ; }
618+
619+ // filePath is for the projectReference and the containing file is from this project reference, invalidate the resolution
620+ resolvedProjectReference . commandLine . fileNames . forEach ( f => removeResolutionsOfFile ( resolutionHost . toPath ( f ) ) ) ;
621+ }
622+
597623 function removeResolutionsOfFile ( filePath : Path ) {
598624 removeResolutionsOfFileFromCache ( resolvedModuleNames , filePath ) ;
599625 removeResolutionsOfFileFromCache ( resolvedTypeReferenceDirectives , filePath ) ;
0 commit comments