@@ -631,6 +631,7 @@ namespace ts.codefix {
631631 let filteredCount = 0 ;
632632 const packageJson = filterByPackageJson && createAutoImportFilter ( from , program , host ) ;
633633 const allSourceFiles = program . getSourceFiles ( ) ;
634+ const globalTypingsCache = host . getGlobalTypingsCacheLocation && host . getGlobalTypingsCacheLocation ( ) ;
634635 forEachExternalModule ( program . getTypeChecker ( ) , allSourceFiles , ( module , sourceFile ) => {
635636 if ( sourceFile === undefined ) {
636637 if ( ! packageJson || packageJson . allowsImportingAmbientModule ( module , allSourceFiles ) ) {
@@ -640,7 +641,10 @@ namespace ts.codefix {
640641 filteredCount ++ ;
641642 }
642643 }
643- else if ( sourceFile && sourceFile !== from && isImportablePath ( from . fileName , sourceFile . fileName ) ) {
644+ else if ( sourceFile &&
645+ sourceFile !== from &&
646+ isImportablePath ( from . fileName , sourceFile . fileName , hostGetCanonicalFileName ( host ) , globalTypingsCache )
647+ ) {
644648 if ( ! packageJson || packageJson . allowsImportingSourceFile ( sourceFile , allSourceFiles ) ) {
645649 cb ( module ) ;
646650 }
@@ -669,10 +673,13 @@ namespace ts.codefix {
669673 * Don't include something from a `node_modules` that isn't actually reachable by a global import.
670674 * A relative import to node_modules is usually a bad idea.
671675 */
672- function isImportablePath ( fromPath : string , toPath : string ) : boolean {
676+ function isImportablePath ( fromPath : string , toPath : string , getCanonicalFileName : GetCanonicalFileName , globalCachePath ?: string ) : boolean {
673677 // If it's in a `node_modules` but is not reachable from here via a global import, don't bother.
674678 const toNodeModules = forEachAncestorDirectory ( toPath , ancestor => getBaseFileName ( ancestor ) === "node_modules" ? ancestor : undefined ) ;
675- return toNodeModules === undefined || startsWith ( fromPath , getDirectoryPath ( toNodeModules ) ) ;
679+ const toNodeModulesParent = toNodeModules && getDirectoryPath ( getCanonicalFileName ( toNodeModules ) ) ;
680+ return toNodeModulesParent === undefined
681+ || startsWith ( getCanonicalFileName ( fromPath ) , toNodeModulesParent )
682+ || ( ! ! globalCachePath && startsWith ( getCanonicalFileName ( globalCachePath ) , toNodeModulesParent ) ) ;
676683 }
677684
678685 export function moduleSymbolToValidIdentifier ( moduleSymbol : Symbol , target : ScriptTarget ) : string {
@@ -718,6 +725,7 @@ namespace ts.codefix {
718725 readFile : maybeBind ( host , host . readFile ) ,
719726 useCaseSensitiveFileNames : maybeBind ( host , host . useCaseSensitiveFileNames ) ,
720727 getProbableSymlinks : maybeBind ( host , host . getProbableSymlinks ) || program . getProbableSymlinks ,
728+ getGlobalTypingsCacheLocation : maybeBind ( host , host . getGlobalTypingsCacheLocation ) ,
721729 } ;
722730
723731 let usesNodeCoreModules : boolean | undefined ;
0 commit comments