@@ -151,7 +151,8 @@ namespace ts {
151151 const toImport = oldFromNew !== undefined
152152 // If we're at the new location (file was already renamed), need to redo module resolution starting from the old location.
153153 // TODO:GH#18217
154- ? getSourceFileToImportFromResolved ( resolveModuleName ( importLiteral . text , oldImportFromPath , program . getCompilerOptions ( ) , host as ModuleResolutionHost ) , oldToNew )
154+ ? getSourceFileToImportFromResolved ( resolveModuleName ( importLiteral . text , oldImportFromPath , program . getCompilerOptions ( ) , host as ModuleResolutionHost ) ,
155+ oldToNew , allFiles )
155156 : getSourceFileToImport ( importedModuleSymbol , importLiteral , sourceFile , program , host , oldToNew ) ;
156157
157158 // Need an update if the imported file moved, or the importing file moved and was using a relative path.
@@ -192,11 +193,11 @@ namespace ts {
192193 const resolved = host . resolveModuleNames
193194 ? host . getResolvedModuleWithFailedLookupLocationsFromCache && host . getResolvedModuleWithFailedLookupLocationsFromCache ( importLiteral . text , importingSourceFile . fileName )
194195 : program . getResolvedModuleWithFailedLookupLocationsFromCache ( importLiteral . text , importingSourceFile . fileName ) ;
195- return getSourceFileToImportFromResolved ( resolved , oldToNew ) ;
196+ return getSourceFileToImportFromResolved ( resolved , oldToNew , program . getSourceFiles ( ) ) ;
196197 }
197198 }
198199
199- function getSourceFileToImportFromResolved ( resolved : ResolvedModuleWithFailedLookupLocations | undefined , oldToNew : PathUpdater ) : ToImport | undefined {
200+ function getSourceFileToImportFromResolved ( resolved : ResolvedModuleWithFailedLookupLocations | undefined , oldToNew : PathUpdater , sourceFiles : readonly SourceFile [ ] ) : ToImport | undefined {
200201 // Search through all locations looking for a moved file, and only then test already existing files.
201202 // This is because if `a.ts` is compiled to `a.js` and `a.ts` is moved, we don't want to resolve anything to `a.js`, but to `a.ts`'s new location.
202203 if ( ! resolved ) return undefined ;
@@ -207,13 +208,21 @@ namespace ts {
207208 if ( result ) return result ;
208209 }
209210
210- // Then failed lookups except package.json since we dont want to touch them (only included ts/js files)
211- const result = forEach ( resolved . failedLookupLocations , tryChangeWithIgnoringPackageJson ) ;
211+ // Then failed lookups that are in the list of sources
212+ const result = forEach ( resolved . failedLookupLocations , tryChangeWithIgnoringPackageJsonExisting )
213+ // Then failed lookups except package.json since we dont want to touch them (only included ts/js files)
214+ || forEach ( resolved . failedLookupLocations , tryChangeWithIgnoringPackageJson ) ;
212215 if ( result ) return result ;
213216
214217 // If nothing changed, then result is resolved module file thats not updated
215218 return resolved . resolvedModule && { newFileName : resolved . resolvedModule . resolvedFileName , updated : false } ;
216219
220+ function tryChangeWithIgnoringPackageJsonExisting ( oldFileName : string ) {
221+ const newFileName = oldToNew ( oldFileName ) ;
222+ return newFileName && find ( sourceFiles , src => src . fileName === newFileName )
223+ ? tryChangeWithIgnoringPackageJson ( oldFileName ) : undefined ;
224+ }
225+
217226 function tryChangeWithIgnoringPackageJson ( oldFileName : string ) {
218227 return ! endsWith ( oldFileName , "/package.json" ) ? tryChange ( oldFileName ) : undefined ;
219228 }
0 commit comments