@@ -726,16 +726,23 @@ namespace ts.moduleSpecifiers {
726
726
}
727
727
728
728
function tryGetModuleNameFromRootDirs ( rootDirs : readonly string [ ] , moduleFileName : string , sourceDirectory : string , getCanonicalFileName : ( file : string ) => string , ending : Ending , compilerOptions : CompilerOptions ) : string | undefined {
729
- const normalizedTargetPath = getPathRelativeToRootDirs ( moduleFileName , rootDirs , getCanonicalFileName ) ;
730
- if ( normalizedTargetPath === undefined ) {
729
+ const normalizedTargetPaths = getPathsRelativeToRootDirs ( moduleFileName , rootDirs , getCanonicalFileName ) ;
730
+ if ( normalizedTargetPaths === undefined ) {
731
+ return undefined ;
732
+ }
733
+
734
+ const normalizedSourcePaths = getPathsRelativeToRootDirs ( sourceDirectory , rootDirs , getCanonicalFileName ) ;
735
+ const relativePaths = flatMap ( normalizedSourcePaths , sourcePath => {
736
+ return map ( normalizedTargetPaths , targetPath => ensurePathIsNonModuleName ( getRelativePathFromDirectory ( sourcePath , targetPath , getCanonicalFileName ) ) ) ;
737
+ } ) ;
738
+ const shortest = min ( relativePaths , compareNumberOfDirectorySeparators ) ;
739
+ if ( ! shortest ) {
731
740
return undefined ;
732
741
}
733
742
734
- const normalizedSourcePath = getPathRelativeToRootDirs ( sourceDirectory , rootDirs , getCanonicalFileName ) ;
735
- const relativePath = normalizedSourcePath !== undefined ? ensurePathIsNonModuleName ( getRelativePathFromDirectory ( normalizedSourcePath , normalizedTargetPath , getCanonicalFileName ) ) : normalizedTargetPath ;
736
743
return getEmitModuleResolutionKind ( compilerOptions ) === ModuleResolutionKind . NodeJs
737
- ? removeExtensionAndIndexPostFix ( relativePath , ending , compilerOptions )
738
- : removeFileExtension ( relativePath ) ;
744
+ ? removeExtensionAndIndexPostFix ( shortest , ending , compilerOptions )
745
+ : removeFileExtension ( shortest ) ;
739
746
}
740
747
741
748
function tryGetModuleNameAsNodeModule ( { path, isRedirect } : ModulePath , { getCanonicalFileName, sourceDirectory } : Info , importingSourceFile : SourceFile , host : ModuleSpecifierResolutionHost , options : CompilerOptions , userPreferences : UserPreferences , packageNameOnly ?: boolean , overrideMode ?: ModuleKind . ESNext | ModuleKind . CommonJS ) : string | undefined {
@@ -882,8 +889,8 @@ namespace ts.moduleSpecifiers {
882
889
}
883
890
}
884
891
885
- function getPathRelativeToRootDirs ( path : string , rootDirs : readonly string [ ] , getCanonicalFileName : GetCanonicalFileName ) : string | undefined {
886
- return firstDefined ( rootDirs , rootDir => {
892
+ function getPathsRelativeToRootDirs ( path : string , rootDirs : readonly string [ ] , getCanonicalFileName : GetCanonicalFileName ) : string [ ] | undefined {
893
+ return mapDefined ( rootDirs , rootDir => {
887
894
const relativePath = getRelativePathIfInDirectory ( path , rootDir , getCanonicalFileName ) ;
888
895
return relativePath !== undefined && isPathRelativeToParent ( relativePath ) ? undefined : relativePath ;
889
896
} ) ;
0 commit comments