@@ -5,12 +5,13 @@ namespace ts {
5
5
startRecordingFilesWithChangedResolutions ( ) : void ;
6
6
finishRecordingFilesWithChangedResolutions ( ) : Path [ ] | undefined ;
7
7
8
- resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined ) : ResolvedModuleFull [ ] ;
8
+ resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined , redirectedReference ?: ResolvedProjectReference ) : ResolvedModuleFull [ ] ;
9
9
getResolvedModuleWithFailedLookupLocationsFromCache ( moduleName : string , containingFile : string ) : CachedResolvedModuleWithFailedLookupLocations | undefined ;
10
- resolveTypeReferenceDirectives ( typeDirectiveNames : string [ ] , containingFile : string ) : ResolvedTypeReferenceDirective [ ] ;
10
+ resolveTypeReferenceDirectives ( typeDirectiveNames : string [ ] , containingFile : string , redirectedReference ?: ResolvedProjectReference ) : ResolvedTypeReferenceDirective [ ] ;
11
11
12
12
invalidateResolutionOfFile ( filePath : Path ) : void ;
13
13
removeResolutionsOfFile ( filePath : Path ) : void ;
14
+ removeResolutionsFromProjectReferenceRedirects ( filePath : Path ) : void ;
14
15
setFilesWithInvalidatedNonRelativeUnresolvedImports ( filesWithUnresolvedImports : Map < ReadonlyArray < string > > ) : void ;
15
16
createHasInvalidatedResolution ( forceAllFilesAsInvalidated ?: boolean ) : HasInvalidatedResolution ;
16
17
@@ -90,17 +91,17 @@ namespace ts {
90
91
// The key in the map is source file's path.
91
92
// The values are Map of resolutions with key being name lookedup.
92
93
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 ( ) ;
95
96
const moduleResolutionCache = createModuleResolutionCacheWithMaps (
96
97
perDirectoryResolvedModuleNames ,
97
- nonRelaticeModuleNameCache ,
98
+ nonRelativeModuleNameCache ,
98
99
getCurrentDirectory ( ) ,
99
100
resolutionHost . getCanonicalFileName
100
101
) ;
101
102
102
103
const resolvedTypeReferenceDirectives = createMap < Map < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > ( ) ;
103
- const perDirectoryResolvedTypeReferenceDirectives = createMap < Map < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > ( ) ;
104
+ const perDirectoryResolvedTypeReferenceDirectives : CacheWithRedirects < Map < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > = createCacheWithRedirects ( ) ;
104
105
105
106
/**
106
107
* These are the extensions that failed lookup files will have by default,
@@ -128,6 +129,7 @@ namespace ts {
128
129
resolveModuleNames,
129
130
getResolvedModuleWithFailedLookupLocationsFromCache,
130
131
resolveTypeReferenceDirectives,
132
+ removeResolutionsFromProjectReferenceRedirects,
131
133
removeResolutionsOfFile,
132
134
invalidateResolutionOfFile,
133
135
setFilesWithInvalidatedNonRelativeUnresolvedImports,
@@ -199,7 +201,7 @@ namespace ts {
199
201
200
202
function clearPerDirectoryResolutions ( ) {
201
203
perDirectoryResolvedModuleNames . clear ( ) ;
202
- nonRelaticeModuleNameCache . clear ( ) ;
204
+ nonRelativeModuleNameCache . clear ( ) ;
203
205
perDirectoryResolvedTypeReferenceDirectives . clear ( ) ;
204
206
nonRelativeExternalModuleResolutions . forEach ( watchFailedLookupLocationOfNonRelativeModuleResolutions ) ;
205
207
nonRelativeExternalModuleResolutions . clear ( ) ;
@@ -217,8 +219,8 @@ namespace ts {
217
219
} ) ;
218
220
}
219
221
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 ) ;
222
224
// return result immediately only if global cache support is not enabled or if it is .ts, .tsx or .d.ts
223
225
if ( ! resolutionHost . getGlobalCache ) {
224
226
return primaryResult ;
@@ -242,16 +244,18 @@ namespace ts {
242
244
function resolveNamesWithLocalCache < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
243
245
names : string [ ] ,
244
246
containingFile : string ,
247
+ redirectedReference : ResolvedProjectReference | undefined ,
245
248
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 ,
248
251
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
249
252
reusedNames : string [ ] | undefined ,
250
253
logChanges : boolean ) : R [ ] {
251
254
252
255
const path = resolutionHost . toPath ( containingFile ) ;
253
256
const resolutionsInFile = cache . get ( path ) || cache . set ( path , createMap ( ) ) . get ( path ) ! ;
254
257
const dirPath = getDirectoryPath ( path ) ;
258
+ const perDirectoryCache = perDirectoryCacheWithRedirects . getOrCreateMapOfCacheRedirects ( redirectedReference ) ;
255
259
let perDirectoryResolution = perDirectoryCache . get ( dirPath ) ;
256
260
if ( ! perDirectoryResolution ) {
257
261
perDirectoryResolution = createMap ( ) ;
@@ -260,12 +264,20 @@ namespace ts {
260
264
const resolvedModules : R [ ] = [ ] ;
261
265
const compilerOptions = resolutionHost . getCompilationSettings ( ) ;
262
266
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
+
263
275
const seenNamesInFile = createMap < true > ( ) ;
264
276
for ( const name of names ) {
265
277
let resolution = resolutionsInFile . get ( name ) ;
266
278
// Resolution is valid if it is present and not invalidated
267
279
if ( ! seenNamesInFile . has ( name ) &&
268
- allFilesHaveInvalidatedResolution || ! resolution || resolution . isInvalidated ||
280
+ allFilesHaveInvalidatedResolution || unmatchedRedirects || ! resolution || resolution . isInvalidated ||
269
281
// If the name is unresolved import that was invalidated, recalculate
270
282
( hasInvalidatedNonRelativeUnresolvedImport && ! isExternalModuleNameRelative ( name ) && ! getResolutionWithResolvedFileName ( resolution ) ) ) {
271
283
const existingResolution = resolution ;
@@ -274,7 +286,7 @@ namespace ts {
274
286
resolution = resolutionInDirectory ;
275
287
}
276
288
else {
277
- resolution = loader ( name , containingFile , compilerOptions , resolutionHost ) ;
289
+ resolution = loader ( name , containingFile , compilerOptions , resolutionHost , redirectedReference ) ;
278
290
perDirectoryResolution . set ( name , resolution ) ;
279
291
}
280
292
resolutionsInFile . set ( name , resolution ) ;
@@ -323,18 +335,18 @@ namespace ts {
323
335
}
324
336
}
325
337
326
- function resolveTypeReferenceDirectives ( typeDirectiveNames : string [ ] , containingFile : string ) : ResolvedTypeReferenceDirective [ ] {
338
+ function resolveTypeReferenceDirectives ( typeDirectiveNames : string [ ] , containingFile : string , redirectedReference ?: ResolvedProjectReference ) : ResolvedTypeReferenceDirective [ ] {
327
339
return resolveNamesWithLocalCache < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations , ResolvedTypeReferenceDirective > (
328
- typeDirectiveNames , containingFile ,
340
+ typeDirectiveNames , containingFile , redirectedReference ,
329
341
resolvedTypeReferenceDirectives , perDirectoryResolvedTypeReferenceDirectives ,
330
342
resolveTypeReferenceDirective , getResolvedTypeReferenceDirective ,
331
343
/*reusedNames*/ undefined , /*logChanges*/ false
332
344
) ;
333
345
}
334
346
335
- function resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined ) : ResolvedModuleFull [ ] {
347
+ function resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined , redirectedReference ?: ResolvedProjectReference ) : ResolvedModuleFull [ ] {
336
348
return resolveNamesWithLocalCache < CachedResolvedModuleWithFailedLookupLocations , ResolvedModuleFull > (
337
- moduleNames , containingFile ,
349
+ moduleNames , containingFile , redirectedReference ,
338
350
resolvedModuleNames , perDirectoryResolvedModuleNames ,
339
351
resolveModuleName , getResolvedModule ,
340
352
reusedNames , logChangesWhenResolvingModule
@@ -594,6 +606,20 @@ namespace ts {
594
606
}
595
607
}
596
608
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
+
597
623
function removeResolutionsOfFile ( filePath : Path ) {
598
624
removeResolutionsOfFileFromCache ( resolvedModuleNames , filePath ) ;
599
625
removeResolutionsOfFileFromCache ( resolvedTypeReferenceDirectives , filePath ) ;
0 commit comments