@@ -189,7 +189,7 @@ namespace ts {
189189 }
190190
191191 const failedLookupLocations : string [ ] = [ ] ;
192- const resolvedFileName = loadModuleFromFile ( supportedExtensions , moduleName , failedLookupLocations , host , traceEnabled ) ;
192+ const resolvedFileName = loadModuleFromFile ( supportedExtensions , moduleName , failedLookupLocations , ! directoryProbablyExists ( getDirectoryPath ( moduleName ) , host ) , host , traceEnabled ) ;
193193 return {
194194 resolvedModule : resolvedFileName ? { resolvedFileName } : undefined ,
195195 failedLookupLocations
@@ -265,7 +265,7 @@ namespace ts {
265265 }
266266 }
267267
268- const resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , host , traceEnabled ) ;
268+ const resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , ! directoryProbablyExists ( getDirectoryPath ( candidate ) , host ) , host , traceEnabled ) ;
269269 return {
270270 resolvedModule : resolvedFileName ? { resolvedFileName } : undefined ,
271271 failedLookupLocations
@@ -323,7 +323,7 @@ namespace ts {
323323 trace ( host , Diagnostics . Trying_substitution_0_candidate_module_location_Colon_1 , subst , path ) ;
324324 }
325325
326- const resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , host , traceEnabled ) ;
326+ const resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , ! directoryProbablyExists ( getDirectoryPath ( candidate ) , host ) , host , traceEnabled ) ;
327327 if ( resolvedFileName ) {
328328 return { resolvedModule : { resolvedFileName } , failedLookupLocations } ;
329329 }
@@ -338,7 +338,7 @@ namespace ts {
338338 trace ( host , Diagnostics . Resolving_module_name_0_relative_to_base_url_1_2 , moduleName , baseUrl , candidate ) ;
339339 }
340340
341- const resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , host , traceEnabled ) ;
341+ const resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , ! directoryProbablyExists ( getDirectoryPath ( candidate ) , host ) , host , traceEnabled ) ;
342342 return {
343343 resolvedModule : resolvedFileName ? { resolvedFileName } : undefined ,
344344 failedLookupLocations
@@ -363,18 +363,16 @@ namespace ts {
363363 if ( isRootedDiskPath ( moduleName ) || nameStartsWithDotSlashOrDotDotSlash ( moduleName ) ) {
364364 const failedLookupLocations : string [ ] = [ ] ;
365365 const candidate = normalizePath ( combinePaths ( containingDirectory , moduleName ) ) ;
366-
367366 if ( traceEnabled ) {
368367 trace ( host , Diagnostics . Loading_module_0_as_file_Slash_folder_candidate_module_location_1 , moduleName , candidate ) ;
369368 }
370369
371- let resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , host , traceEnabled ) ;
372-
370+ let resolvedFileName = loadModuleFromFile ( supportedExtensions , candidate , failedLookupLocations , /*onlyRecordFailures*/ false , host , traceEnabled ) ;
373371 if ( resolvedFileName ) {
374372 return { resolvedModule : { resolvedFileName } , failedLookupLocations } ;
375373 }
376374
377- resolvedFileName = loadNodeModuleFromDirectory ( supportedExtensions , candidate , failedLookupLocations , host , traceEnabled ) ;
375+ resolvedFileName = loadNodeModuleFromDirectory ( supportedExtensions , candidate , failedLookupLocations , /*onlyRecordFailures*/ false , host , traceEnabled ) ;
378376 return resolvedFileName
379377 ? { resolvedModule : { resolvedFileName } , failedLookupLocations }
380378 : { resolvedModule : undefined , failedLookupLocations } ;
@@ -388,13 +386,22 @@ namespace ts {
388386 }
389387 }
390388
391- function loadModuleFromFile ( extensions : string [ ] , candidate : string , failedLookupLocation : string [ ] , host : ModuleResolutionHost , traceEnabled : boolean ) : string {
389+ /* @internal */
390+ export function directoryProbablyExists ( directoryName : string , host : { directoryExists ?: ( directoryName : string ) => boolean } ) : boolean {
391+ // if host does not support 'directoryExists' assume that directory will exist
392+ return ! host . directoryExists || host . directoryExists ( directoryName ) ;
393+ }
394+
395+ /**
396+ * @param {boolean } onlyRecordFailures - if true then function won't try to actually load files but instead record all attempts as failures. This flag is necessary
397+ * in cases when we know upfront that all load attempts will fail (because containing folder does not exists) however we still need to record all failed lookup locations.
398+ */
399+ function loadModuleFromFile ( extensions : string [ ] , candidate : string , failedLookupLocation : string [ ] , onlyRecordFailures : boolean , host : ModuleResolutionHost , traceEnabled : boolean ) : string {
392400 return forEach ( extensions , tryLoad ) ;
393401
394402 function tryLoad ( ext : string ) : string {
395403 const fileName = fileExtensionIs ( candidate , ext ) ? candidate : candidate + ext ;
396-
397- if ( host . fileExists ( fileName ) ) {
404+ if ( ! onlyRecordFailures && host . fileExists ( fileName ) ) {
398405 if ( traceEnabled ) {
399406 trace ( host , Diagnostics . File_0_exist_use_it_as_a_module_resolution_result , fileName ) ;
400407 }
@@ -410,9 +417,10 @@ namespace ts {
410417 }
411418 }
412419
413- function loadNodeModuleFromDirectory ( extensions : string [ ] , candidate : string , failedLookupLocation : string [ ] , host : ModuleResolutionHost , traceEnabled : boolean ) : string {
420+ function loadNodeModuleFromDirectory ( extensions : string [ ] , candidate : string , failedLookupLocation : string [ ] , onlyRecordFailures : boolean , host : ModuleResolutionHost , traceEnabled : boolean ) : string {
414421 const packageJsonPath = combinePaths ( candidate , "package.json" ) ;
415- if ( host . fileExists ( packageJsonPath ) ) {
422+ const directoryExists = ! onlyRecordFailures && directoryProbablyExists ( candidate , host ) ;
423+ if ( directoryExists && host . fileExists ( packageJsonPath ) ) {
416424 if ( traceEnabled ) {
417425 trace ( host , Diagnostics . Found_package_json_at_0 , packageJsonPath ) ;
418426 }
@@ -434,7 +442,7 @@ namespace ts {
434442 if ( traceEnabled ) {
435443 trace ( host , Diagnostics . package_json_has_typings_field_0_that_references_1 , jsonContent . typings , typingsFile ) ;
436444 }
437- const result = loadModuleFromFile ( extensions , typingsFile , failedLookupLocation , host , traceEnabled ) ;
445+ const result = loadModuleFromFile ( extensions , typingsFile , failedLookupLocation , ! directoryProbablyExists ( getDirectoryPath ( typingsFile ) , host ) , host , traceEnabled ) ;
438446 if ( result ) {
439447 return result ;
440448 }
@@ -457,7 +465,7 @@ namespace ts {
457465 failedLookupLocation . push ( packageJsonPath ) ;
458466 }
459467
460- return loadModuleFromFile ( extensions , combinePaths ( candidate , "index" ) , failedLookupLocation , host , traceEnabled ) ;
468+ return loadModuleFromFile ( extensions , combinePaths ( candidate , "index" ) , failedLookupLocation , ! directoryExists , host , traceEnabled ) ;
461469 }
462470
463471 function loadModuleFromNodeModules ( moduleName : string , directory : string , host : ModuleResolutionHost , traceEnabled : boolean ) : ResolvedModuleWithFailedLookupLocations {
@@ -467,14 +475,14 @@ namespace ts {
467475 const baseName = getBaseFileName ( directory ) ;
468476 if ( baseName !== "node_modules" ) {
469477 const nodeModulesFolder = combinePaths ( directory , "node_modules" ) ;
478+ const nodeModulesFolderExists = directoryProbablyExists ( nodeModulesFolder , host ) ;
470479 const candidate = normalizePath ( combinePaths ( nodeModulesFolder , moduleName ) ) ;
471480 // Load only typescript files irrespective of allowJs option if loading from node modules
472- let result = loadModuleFromFile ( supportedTypeScriptExtensions , candidate , failedLookupLocations , host , traceEnabled ) ;
481+ let result = loadModuleFromFile ( supportedTypeScriptExtensions , candidate , failedLookupLocations , ! nodeModulesFolderExists , host , traceEnabled ) ;
473482 if ( result ) {
474483 return { resolvedModule : { resolvedFileName : result , isExternalLibraryImport : true } , failedLookupLocations } ;
475484 }
476-
477- result = loadNodeModuleFromDirectory ( supportedTypeScriptExtensions , candidate , failedLookupLocations , host , traceEnabled ) ;
485+ result = loadNodeModuleFromDirectory ( supportedTypeScriptExtensions , candidate , failedLookupLocations , ! nodeModulesFolderExists , host , traceEnabled ) ;
478486 if ( result ) {
479487 return { resolvedModule : { resolvedFileName : result , isExternalLibraryImport : true } , failedLookupLocations } ;
480488 }
@@ -655,7 +663,8 @@ namespace ts {
655663 getNewLine : ( ) => newLine ,
656664 fileExists : fileName => sys . fileExists ( fileName ) ,
657665 readFile : fileName => sys . readFile ( fileName ) ,
658- trace : ( s : string ) => sys . write ( s + newLine )
666+ trace : ( s : string ) => sys . write ( s + newLine ) ,
667+ directoryExists : directoryName => sys . directoryExists ( directoryName )
659668 } ;
660669 }
661670
0 commit comments