@@ -406,7 +406,7 @@ namespace ts {
406406 }
407407 }
408408
409- function loadWithLocalCache < T > ( names : string [ ] , containingFile : string , loader : ( name : string , containingFile : string ) => T ) : T [ ] {
409+ function loadWithLocalCache < T > ( names : string [ ] , containingFile : string , redirectedReference : ResolvedProjectReference | undefined , loader : ( name : string , containingFile : string , redirectedReference : ResolvedProjectReference | undefined ) => T ) : T [ ] {
410410 if ( names . length === 0 ) {
411411 return [ ] ;
412412 }
@@ -418,7 +418,7 @@ namespace ts {
418418 result = cache . get ( name ) ! ;
419419 }
420420 else {
421- cache . set ( name , result = loader ( name , containingFile ) ) ;
421+ cache . set ( name , result = loader ( name , containingFile , redirectedReference ) ) ;
422422 }
423423 resolutions . push ( result ) ;
424424 }
@@ -638,10 +638,10 @@ namespace ts {
638638 let _referencesArrayLiteralSyntax : ArrayLiteralExpression | null | undefined ;
639639
640640 let moduleResolutionCache : ModuleResolutionCache | undefined ;
641- let resolveModuleNamesWorker : ( moduleNames : string [ ] , containingFile : string , reusedNames ?: string [ ] ) => ResolvedModuleFull [ ] ;
641+ let resolveModuleNamesWorker : ( moduleNames : string [ ] , containingFile : string , reusedNames ?: string [ ] , redirectedReference ?: ResolvedProjectReference ) => ResolvedModuleFull [ ] ;
642642 const hasInvalidatedResolution = host . hasInvalidatedResolution || returnFalse ;
643643 if ( host . resolveModuleNames ) {
644- resolveModuleNamesWorker = ( moduleNames , containingFile , reusedNames ) => host . resolveModuleNames ! ( Debug . assertEachDefined ( moduleNames ) , containingFile , reusedNames ) . map ( resolved => {
644+ resolveModuleNamesWorker = ( moduleNames , containingFile , reusedNames , redirectedReference ) => host . resolveModuleNames ! ( Debug . assertEachDefined ( moduleNames ) , containingFile , reusedNames , redirectedReference ) . map ( resolved => {
645645 // An older host may have omitted extension, in which case we should infer it from the file extension of resolvedFileName.
646646 if ( ! resolved || ( resolved as ResolvedModuleFull ) . extension !== undefined ) {
647647 return resolved as ResolvedModuleFull ;
@@ -653,17 +653,17 @@ namespace ts {
653653 }
654654 else {
655655 moduleResolutionCache = createModuleResolutionCache ( currentDirectory , x => host . getCanonicalFileName ( x ) ) ;
656- const loader = ( moduleName : string , containingFile : string ) => resolveModuleName ( moduleName , containingFile , options , host , moduleResolutionCache ) . resolvedModule ! ; // TODO: GH#18217
657- resolveModuleNamesWorker = ( moduleNames , containingFile ) => loadWithLocalCache < ResolvedModuleFull > ( Debug . assertEachDefined ( moduleNames ) , containingFile , loader ) ;
656+ const loader = ( moduleName : string , containingFile : string , redirectedReference : ResolvedProjectReference | undefined ) => resolveModuleName ( moduleName , containingFile , options , host , moduleResolutionCache , redirectedReference ) . resolvedModule ! ; // TODO: GH#18217
657+ resolveModuleNamesWorker = ( moduleNames , containingFile , _reusedNames , redirectedReference ) => loadWithLocalCache < ResolvedModuleFull > ( Debug . assertEachDefined ( moduleNames ) , containingFile , redirectedReference , loader ) ;
658658 }
659659
660- let resolveTypeReferenceDirectiveNamesWorker : ( typeDirectiveNames : string [ ] , containingFile : string ) => ResolvedTypeReferenceDirective [ ] ;
660+ let resolveTypeReferenceDirectiveNamesWorker : ( typeDirectiveNames : string [ ] , containingFile : string , redirectedReference ?: ResolvedProjectReference ) => ResolvedTypeReferenceDirective [ ] ;
661661 if ( host . resolveTypeReferenceDirectives ) {
662- resolveTypeReferenceDirectiveNamesWorker = ( typeDirectiveNames , containingFile ) => host . resolveTypeReferenceDirectives ! ( Debug . assertEachDefined ( typeDirectiveNames ) , containingFile ) ;
662+ resolveTypeReferenceDirectiveNamesWorker = ( typeDirectiveNames , containingFile , redirectedReference ) => host . resolveTypeReferenceDirectives ! ( Debug . assertEachDefined ( typeDirectiveNames ) , containingFile , redirectedReference ) ;
663663 }
664664 else {
665- const loader = ( typesRef : string , containingFile : string ) => resolveTypeReferenceDirective ( typesRef , containingFile , options , host ) . resolvedTypeReferenceDirective ! ; // TODO: GH#18217
666- resolveTypeReferenceDirectiveNamesWorker = ( typeReferenceDirectiveNames , containingFile ) => loadWithLocalCache < ResolvedTypeReferenceDirective > ( Debug . assertEachDefined ( typeReferenceDirectiveNames ) , containingFile , loader ) ;
665+ const loader = ( typesRef : string , containingFile : string , redirectedReference : ResolvedProjectReference | undefined ) => resolveTypeReferenceDirective ( typesRef , containingFile , options , host , redirectedReference ) . resolvedTypeReferenceDirective ! ; // TODO: GH#18217
666+ resolveTypeReferenceDirectiveNamesWorker = ( typeReferenceDirectiveNames , containingFile , redirectedReference ) => loadWithLocalCache < ResolvedTypeReferenceDirective > ( Debug . assertEachDefined ( typeReferenceDirectiveNames ) , containingFile , redirectedReference , loader ) ;
667667 }
668668
669669 // Map from a stringified PackageId to the source file with that id.
@@ -760,12 +760,6 @@ namespace ts {
760760
761761 // unconditionally set oldProgram to undefined to prevent it from being captured in closure
762762 oldProgram = undefined ;
763- // Do not use our own command line for projectReferenceRedirects
764- if ( projectReferenceRedirects ) {
765- Debug . assert ( ! ! options . configFilePath ) ;
766- const path = toPath ( options . configFilePath ! ) ;
767- projectReferenceRedirects . delete ( path ) ;
768- }
769763
770764 program = {
771765 getRootFileNames : ( ) => rootNames ,
@@ -887,7 +881,7 @@ namespace ts {
887881 if ( structuralIsReused === StructureIsReused . Not && ! file . ambientModuleNames . length ) {
888882 // If the old program state does not permit reusing resolutions and `file` does not contain locally defined ambient modules,
889883 // the best we can do is fallback to the default logic.
890- return resolveModuleNamesWorker ( moduleNames , containingFile ) ;
884+ return resolveModuleNamesWorker ( moduleNames , containingFile , /*reusedNames*/ undefined , getProjectReferenceRedirectProject ( file . originalFileName ) ) ;
891885 }
892886
893887 const oldSourceFile = oldProgramState . program && oldProgramState . program . getSourceFile ( containingFile ) ;
@@ -967,7 +961,7 @@ namespace ts {
967961 }
968962
969963 const resolutions = unknownModuleNames && unknownModuleNames . length
970- ? resolveModuleNamesWorker ( unknownModuleNames , containingFile , reusedNames )
964+ ? resolveModuleNamesWorker ( unknownModuleNames , containingFile , reusedNames , getProjectReferenceRedirectProject ( file . originalFileName ) )
971965 : emptyArray ;
972966
973967 // Combine results of resolutions and predicted results
@@ -1248,7 +1242,7 @@ namespace ts {
12481242 if ( resolveTypeReferenceDirectiveNamesWorker ) {
12491243 // We lower-case all type references because npm automatically lowercases all packages. See GH#9824.
12501244 const typesReferenceDirectives = map ( newSourceFile . typeReferenceDirectives , ref => ref . fileName . toLocaleLowerCase ( ) ) ;
1251- const resolutions = resolveTypeReferenceDirectiveNamesWorker ( typesReferenceDirectives , newSourceFilePath ) ;
1245+ const resolutions = resolveTypeReferenceDirectiveNamesWorker ( typesReferenceDirectives , newSourceFilePath , getProjectReferenceRedirectProject ( newSourceFile . originalFileName ) ) ;
12521246 // ensure that types resolutions are still correct
12531247 const resolutionsChanged = hasChangesInResolutions ( typesReferenceDirectives , resolutions , oldSourceFile . resolvedTypeReferenceDirectiveNames , typeDirectiveIsEqualTo ) ;
12541248 if ( resolutionsChanged ) {
@@ -2219,16 +2213,35 @@ namespace ts {
22192213
22202214 // If this file is produced by a referenced project, we need to rewrite it to
22212215 // look in the output folder of the referenced project rather than the input
2216+ const referencedProject = getProjectReferenceRedirectProject ( fileName ) ;
2217+ if ( ! referencedProject ) {
2218+ return undefined ;
2219+ }
2220+
2221+ const out = referencedProject . commandLine . options . outFile || referencedProject . commandLine . options . out ;
2222+ return out ?
2223+ changeExtension ( out , Extension . Dts ) :
2224+ getOutputDeclarationFileName ( fileName , referencedProject . commandLine ) ;
2225+ }
2226+
2227+ /**
2228+ * Get the referenced project if the file is input file from that reference project
2229+ */
2230+ function getProjectReferenceRedirectProject ( fileName : string ) {
2231+ if ( ! resolvedProjectReferences || ! resolvedProjectReferences . length ) {
2232+ return undefined ;
2233+ }
2234+
2235+ // If this file is input file of the referenced projec
22222236 return forEachEntry ( projectReferenceRedirects ! , referencedProject => {
22232237 // not input file from the referenced project, ignore
2224- if ( ! referencedProject || ! contains ( referencedProject . commandLine . fileNames , fileName , isSameFile ) ) {
2238+ if ( ! referencedProject ||
2239+ options . configFilePath === referencedProject . commandLine . options . configFilePath ||
2240+ ! contains ( referencedProject . commandLine . fileNames , fileName , isSameFile ) ) {
22252241 return undefined ;
22262242 }
22272243
2228- const out = referencedProject . commandLine . options . outFile || referencedProject . commandLine . options . out ;
2229- return out ?
2230- changeExtension ( out , Extension . Dts ) :
2231- getOutputDeclarationFileName ( fileName , referencedProject . commandLine ) ;
2244+ return referencedProject ;
22322245 } ) ;
22332246 }
22342247
@@ -2246,7 +2259,7 @@ namespace ts {
22462259 return ;
22472260 }
22482261
2249- const resolutions = resolveTypeReferenceDirectiveNamesWorker ( typeDirectives , file . originalFileName ) ;
2262+ const resolutions = resolveTypeReferenceDirectiveNamesWorker ( typeDirectives , file . originalFileName , getProjectReferenceRedirectProject ( file . originalFileName ) ) ;
22502263
22512264 for ( let i = 0 ; i < typeDirectives . length ; i ++ ) {
22522265 const ref = file . typeReferenceDirectives [ i ] ;
0 commit comments