@@ -195,63 +195,66 @@ namespace ts {
195195
196196 const failedLookupLocations : string [ ] = [ ] ;
197197
198- // Check primary library paths
199- if ( typeRoots && typeRoots . length ) {
198+ let resolved = primaryLookup ( ) ;
199+ let primary = true ;
200+ if ( ! resolved ) {
201+ resolved = secondaryLookup ( ) ;
202+ primary = false ;
203+ }
204+
205+ let resolvedTypeReferenceDirective : ResolvedTypeReferenceDirective | undefined ;
206+ if ( resolved ) {
207+ resolved = realpath ( resolved , host , traceEnabled ) ;
200208 if ( traceEnabled ) {
201- trace ( host , Diagnostics . Resolving_with_primary_search_path_0 , typeRoots . join ( ", " ) ) ;
209+ trace ( host , Diagnostics . Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2 , typeReferenceDirectiveName , resolved , primary ) ;
202210 }
203- for ( const typeRoot of typeRoots ) {
204- const candidate = combinePaths ( typeRoot , typeReferenceDirectiveName ) ;
205- const candidateDirectory = getDirectoryPath ( candidate ) ;
211+ resolvedTypeReferenceDirective = { primary, resolvedFileName : resolved } ;
212+ }
206213
207- const resolved = resolvedTypeScriptOnly (
208- loadNodeModuleFromDirectory ( Extensions . DtsOnly , candidate , failedLookupLocations ,
209- ! directoryProbablyExists ( candidateDirectory , host ) , moduleResolutionState ) ) ;
214+ return { resolvedTypeReferenceDirective, failedLookupLocations } ;
210215
211- if ( resolved ) {
212- if ( traceEnabled ) {
213- trace ( host , Diagnostics . Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2 , typeReferenceDirectiveName , resolved , true ) ;
214- }
215- return {
216- resolvedTypeReferenceDirective : { primary : true , resolvedFileName : resolved } ,
217- failedLookupLocations
218- } ;
216+ function primaryLookup ( ) : string | undefined {
217+ // Check primary library paths
218+ if ( typeRoots && typeRoots . length ) {
219+ if ( traceEnabled ) {
220+ trace ( host , Diagnostics . Resolving_with_primary_search_path_0 , typeRoots . join ( ", " ) ) ;
219221 }
222+ return forEach ( typeRoots , typeRoot => {
223+ const candidate = combinePaths ( typeRoot , typeReferenceDirectiveName ) ;
224+ const candidateDirectory = getDirectoryPath ( candidate ) ;
225+ return resolvedTypeScriptOnly (
226+ loadNodeModuleFromDirectory ( Extensions . DtsOnly , candidate , failedLookupLocations ,
227+ ! directoryProbablyExists ( candidateDirectory , host ) , moduleResolutionState ) ) ;
228+ } ) ;
220229 }
221- }
222- else {
223- if ( traceEnabled ) {
224- trace ( host , Diagnostics . Root_directory_cannot_be_determined_skipping_primary_search_paths ) ;
230+ else {
231+ if ( traceEnabled ) {
232+ trace ( host , Diagnostics . Root_directory_cannot_be_determined_skipping_primary_search_paths ) ;
233+ }
225234 }
226235 }
227236
228- let resolvedFile : string ;
229- const initialLocationForSecondaryLookup = containingFile && getDirectoryPath ( containingFile ) ;
237+ function secondaryLookup ( ) : string | undefined {
238+ let resolvedFile : string ;
239+ const initialLocationForSecondaryLookup = containingFile && getDirectoryPath ( containingFile ) ;
230240
231- if ( initialLocationForSecondaryLookup !== undefined ) {
232- // check secondary locations
233- if ( traceEnabled ) {
234- trace ( host , Diagnostics . Looking_up_in_node_modules_folder_initial_location_0 , initialLocationForSecondaryLookup ) ;
235- }
236- resolvedFile = resolvedTypeScriptOnly ( loadModuleFromNodeModules ( Extensions . DtsOnly , typeReferenceDirectiveName , initialLocationForSecondaryLookup , failedLookupLocations , moduleResolutionState , /*checkOneLevel*/ false ) ) ;
237- if ( traceEnabled ) {
238- if ( resolvedFile ) {
239- trace ( host , Diagnostics . Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2 , typeReferenceDirectiveName , resolvedFile , false ) ;
241+ if ( initialLocationForSecondaryLookup !== undefined ) {
242+ // check secondary locations
243+ if ( traceEnabled ) {
244+ trace ( host , Diagnostics . Looking_up_in_node_modules_folder_initial_location_0 , initialLocationForSecondaryLookup ) ;
240245 }
241- else {
246+ resolvedFile = resolvedTypeScriptOnly ( loadModuleFromNodeModules ( Extensions . DtsOnly , typeReferenceDirectiveName , initialLocationForSecondaryLookup , failedLookupLocations , moduleResolutionState , /*checkOneLevel*/ false ) ) ;
247+ if ( ! resolvedFile && traceEnabled ) {
242248 trace ( host , Diagnostics . Type_reference_directive_0_was_not_resolved , typeReferenceDirectiveName ) ;
243249 }
250+ return resolvedFile ;
244251 }
245- }
246- else {
247- if ( traceEnabled ) {
248- trace ( host , Diagnostics . Containing_file_is_not_specified_and_root_directory_cannot_be_determined_skipping_lookup_in_node_modules_folder ) ;
252+ else {
253+ if ( traceEnabled ) {
254+ trace ( host , Diagnostics . Containing_file_is_not_specified_and_root_directory_cannot_be_determined_skipping_lookup_in_node_modules_folder ) ;
255+ }
249256 }
250257 }
251- return {
252- resolvedTypeReferenceDirective : resolvedFile ? { primary : false , resolvedFileName : resolvedFile } : undefined ,
253- failedLookupLocations
254- } ;
255258 }
256259
257260 /**
@@ -564,7 +567,7 @@ namespace ts {
564567 }
565568 const resolved = loadModuleFromNodeModules ( extensions , moduleName , containingDirectory , failedLookupLocations , state , /*checkOneLevel*/ false ) ;
566569 // For node_modules lookups, get the real path so that multiple accesses to an `npm link`-ed module do not create duplicate files.
567- return resolved && { resolved : resolvedWithRealpath ( resolved , host , traceEnabled ) , isExternalLibraryImport : true } ;
570+ return resolved && { resolved : { path : realpath ( resolved . path , host , traceEnabled ) , extension : resolved . extension } , isExternalLibraryImport : true } ;
568571 }
569572 else {
570573 const candidate = normalizePath ( combinePaths ( containingDirectory , moduleName ) ) ;
@@ -574,16 +577,16 @@ namespace ts {
574577 }
575578 }
576579
577- function resolvedWithRealpath ( resolved : Resolved , host : ModuleResolutionHost , traceEnabled : boolean ) : Resolved {
580+ function realpath ( path : string , host : ModuleResolutionHost , traceEnabled : boolean ) : string {
578581 if ( ! host . realpath ) {
579- return resolved ;
582+ return path ;
580583 }
581584
582- const real = normalizePath ( host . realpath ( resolved . path ) ) ;
585+ const real = normalizePath ( host . realpath ( path ) ) ;
583586 if ( traceEnabled ) {
584- trace ( host , Diagnostics . Resolving_real_path_for_0_result_1 , resolved . path , real ) ;
587+ trace ( host , Diagnostics . Resolving_real_path_for_0_result_1 , path , real ) ;
585588 }
586- return { path : real , extension : resolved . extension } ;
589+ return real ;
587590 }
588591
589592 function nodeLoadModuleByRelativeName ( extensions : Extensions , candidate : string , failedLookupLocations : string [ ] , onlyRecordFailures : boolean , state : ModuleResolutionState ) : Resolved | undefined {
0 commit comments