@@ -32,7 +32,7 @@ namespace ts {
32
32
* Kinds of file that we are currently looking for.
33
33
* Typically there is one pass with Extensions.TypeScript, then a second pass with Extensions.JavaScript.
34
34
*/
35
- const enum Extensions {
35
+ enum Extensions {
36
36
TypeScript , /** '.ts', '.tsx', or '.d.ts' */
37
37
JavaScript , /** '.js' or '.jsx' */
38
38
DtsOnly /** Only '.d.ts' */
@@ -217,9 +217,13 @@ namespace ts {
217
217
return forEach ( typeRoots , typeRoot => {
218
218
const candidate = combinePaths ( typeRoot , typeReferenceDirectiveName ) ;
219
219
const candidateDirectory = getDirectoryPath ( candidate ) ;
220
+ const directoryExists = directoryProbablyExists ( candidateDirectory , host ) ;
221
+ if ( ! directoryExists && traceEnabled ) {
222
+ trace ( host , Diagnostics . Directory_0_does_not_exist_skipping_all_lookups_in_it , candidateDirectory ) ;
223
+ }
220
224
return resolvedTypeScriptOnly (
221
225
loadNodeModuleFromDirectory ( Extensions . DtsOnly , candidate , failedLookupLocations ,
222
- ! directoryProbablyExists ( candidateDirectory , host ) , moduleResolutionState ) ) ;
226
+ ! directoryExists , moduleResolutionState ) ) ;
223
227
} ) ;
224
228
}
225
229
else {
@@ -701,7 +705,7 @@ namespace ts {
701
705
702
706
if ( moduleHasNonRelativeName ( moduleName ) ) {
703
707
if ( traceEnabled ) {
704
- trace ( host , Diagnostics . Loading_module_0_from_node_modules_folder , moduleName ) ;
708
+ trace ( host , Diagnostics . Loading_module_0_from_node_modules_folder_target_file_type_1 , moduleName , Extensions [ extensions ] ) ;
705
709
}
706
710
const resolved = loadModuleFromNodeModules ( extensions , moduleName , containingDirectory , failedLookupLocations , state , cache ) ;
707
711
// For node_modules lookups, get the real path so that multiple accesses to an `npm link`-ed module do not create duplicate files.
@@ -729,11 +733,33 @@ namespace ts {
729
733
730
734
function nodeLoadModuleByRelativeName ( extensions : Extensions , candidate : string , failedLookupLocations : Push < string > , onlyRecordFailures : boolean , state : ModuleResolutionState ) : Resolved | undefined {
731
735
if ( state . traceEnabled ) {
732
- trace ( state . host , Diagnostics . Loading_module_as_file_Slash_folder_candidate_module_location_0 , candidate ) ;
736
+ trace ( state . host , Diagnostics . Loading_module_as_file_Slash_folder_candidate_module_location_0_target_file_type_1 , candidate , Extensions [ extensions ] ) ;
733
737
}
734
-
735
- const resolvedFromFile = ! pathEndsWithDirectorySeparator ( candidate ) && loadModuleFromFile ( extensions , candidate , failedLookupLocations , onlyRecordFailures , state ) ;
736
- return resolvedFromFile || loadNodeModuleFromDirectory ( extensions , candidate , failedLookupLocations , onlyRecordFailures , state ) ;
738
+ if ( ! pathEndsWithDirectorySeparator ( candidate ) ) {
739
+ if ( ! onlyRecordFailures ) {
740
+ const parentOfCandidate = getDirectoryPath ( candidate ) ;
741
+ if ( ! directoryProbablyExists ( parentOfCandidate , state . host ) ) {
742
+ if ( state . traceEnabled ) {
743
+ trace ( state . host , Diagnostics . Directory_0_does_not_exist_skipping_all_lookups_in_it , parentOfCandidate ) ;
744
+ }
745
+ onlyRecordFailures = true ;
746
+ }
747
+ }
748
+ const resolvedFromFile = loadModuleFromFile ( extensions , candidate , failedLookupLocations , onlyRecordFailures , state ) ;
749
+ if ( resolvedFromFile ) {
750
+ return resolvedFromFile ;
751
+ }
752
+ }
753
+ if ( ! onlyRecordFailures ) {
754
+ const candidateExists = directoryProbablyExists ( candidate , state . host ) ;
755
+ if ( ! candidateExists ) {
756
+ if ( state . traceEnabled ) {
757
+ trace ( state . host , Diagnostics . Directory_0_does_not_exist_skipping_all_lookups_in_it , candidate ) ;
758
+ }
759
+ onlyRecordFailures = true ;
760
+ }
761
+ }
762
+ return loadNodeModuleFromDirectory ( extensions , candidate , failedLookupLocations , onlyRecordFailures , state ) ;
737
763
}
738
764
739
765
/* @internal */
@@ -792,19 +818,21 @@ namespace ts {
792
818
793
819
/** Return the file if it exists. */
794
820
function tryFile ( fileName : string , failedLookupLocations : Push < string > , onlyRecordFailures : boolean , state : ModuleResolutionState ) : string | undefined {
795
- if ( ! onlyRecordFailures && state . host . fileExists ( fileName ) ) {
796
- if ( state . traceEnabled ) {
797
- trace ( state . host , Diagnostics . File_0_exist_use_it_as_a_name_resolution_result , fileName ) ;
821
+ if ( ! onlyRecordFailures ) {
822
+ if ( state . host . fileExists ( fileName ) ) {
823
+ if ( state . traceEnabled ) {
824
+ trace ( state . host , Diagnostics . File_0_exist_use_it_as_a_name_resolution_result , fileName ) ;
825
+ }
826
+ return fileName ;
798
827
}
799
- return fileName ;
800
- }
801
- else {
802
- if ( state . traceEnabled ) {
803
- trace ( state . host , Diagnostics . File_0_does_not_exist , fileName ) ;
828
+ else {
829
+ if ( state . traceEnabled ) {
830
+ trace ( state . host , Diagnostics . File_0_does_not_exist , fileName ) ;
831
+ }
804
832
}
805
- failedLookupLocations . push ( fileName ) ;
806
- return undefined ;
807
833
}
834
+ failedLookupLocations . push ( fileName ) ;
835
+ return undefined ;
808
836
}
809
837
810
838
function loadNodeModuleFromDirectory ( extensions : Extensions , candidate : string , failedLookupLocations : Push < string > , onlyRecordFailures : boolean , state : ModuleResolutionState ) : Resolved | undefined {
@@ -841,7 +869,7 @@ namespace ts {
841
869
}
842
870
}
843
871
else {
844
- if ( state . traceEnabled ) {
872
+ if ( directoryExists && state . traceEnabled ) {
845
873
trace ( state . host , Diagnostics . File_0_does_not_exist , packageJsonPath ) ;
846
874
}
847
875
// record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results
@@ -873,9 +901,7 @@ namespace ts {
873
901
return combinePaths ( directory , "package.json" ) ;
874
902
}
875
903
876
- function loadModuleFromNodeModulesFolder ( extensions : Extensions , moduleName : string , directory : string , failedLookupLocations : Push < string > , state : ModuleResolutionState ) : Resolved | undefined {
877
- const nodeModulesFolder = combinePaths ( directory , "node_modules" ) ;
878
- const nodeModulesFolderExists = directoryProbablyExists ( nodeModulesFolder , state . host ) ;
904
+ function loadModuleFromNodeModulesFolder ( extensions : Extensions , moduleName : string , nodeModulesFolder : string , nodeModulesFolderExists : boolean , failedLookupLocations : Push < string > , state : ModuleResolutionState ) : Resolved | undefined {
879
905
const candidate = normalizePath ( combinePaths ( nodeModulesFolder , moduleName ) ) ;
880
906
881
907
return loadModuleFromFile ( extensions , candidate , failedLookupLocations , ! nodeModulesFolderExists , state ) ||
@@ -905,12 +931,26 @@ namespace ts {
905
931
906
932
/** Load a module from a single node_modules directory, but not from any ancestors' node_modules directories. */
907
933
function loadModuleFromNodeModulesOneLevel ( extensions : Extensions , moduleName : string , directory : string , failedLookupLocations : Push < string > , state : ModuleResolutionState , typesOnly = false ) : Resolved | undefined {
908
- const packageResult = typesOnly ? undefined : loadModuleFromNodeModulesFolder ( extensions , moduleName , directory , failedLookupLocations , state ) ;
934
+ const nodeModulesFolder = combinePaths ( directory , "node_modules" ) ;
935
+ const nodeModulesFolderExists = directoryProbablyExists ( nodeModulesFolder , state . host ) ;
936
+ if ( ! nodeModulesFolderExists && state . traceEnabled ) {
937
+ trace ( state . host , Diagnostics . Directory_0_does_not_exist_skipping_all_lookups_in_it , nodeModulesFolder ) ;
938
+ }
939
+
940
+ const packageResult = typesOnly ? undefined : loadModuleFromNodeModulesFolder ( extensions , moduleName , nodeModulesFolder , nodeModulesFolderExists , failedLookupLocations , state ) ;
909
941
if ( packageResult ) {
910
942
return packageResult ;
911
943
}
912
944
if ( extensions !== Extensions . JavaScript ) {
913
- return loadModuleFromNodeModulesFolder ( Extensions . DtsOnly , combinePaths ( "@types" , moduleName ) , directory , failedLookupLocations , state ) ;
945
+ const nodeModulesAtTypes = combinePaths ( nodeModulesFolder , "@types" ) ;
946
+ let nodeModulesAtTypesExists = nodeModulesFolderExists ;
947
+ if ( nodeModulesFolderExists && ! directoryProbablyExists ( nodeModulesAtTypes , state . host ) ) {
948
+ if ( state . traceEnabled ) {
949
+ trace ( state . host , Diagnostics . Directory_0_does_not_exist_skipping_all_lookups_in_it , nodeModulesAtTypes ) ;
950
+ }
951
+ nodeModulesAtTypesExists = false ;
952
+ }
953
+ return loadModuleFromNodeModulesFolder ( Extensions . DtsOnly , moduleName , nodeModulesAtTypes , nodeModulesAtTypesExists , failedLookupLocations , state ) ;
914
954
}
915
955
}
916
956
0 commit comments