@@ -41,14 +41,8 @@ namespace ts {
4141 return normalizePath ( referencedFileName ) ;
4242 }
4343
44- export function computeCompilationRoot ( rootFileNames : string [ ] , currentDirectory : string , options : CompilerOptions , getCanonicalFileName : ( fileName : string ) => string ) : string {
45- const rootDirOrConfigFilePath = options . rootDir || ( options . configFilePath && getDirectoryPath ( options . configFilePath ) ) ;
46- return rootDirOrConfigFilePath
47- ? getNormalizedAbsolutePath ( rootDirOrConfigFilePath , currentDirectory )
48- : computeCommonSourceDirectoryOfFilenames ( rootFileNames , currentDirectory , getCanonicalFileName ) ;
49- }
50-
51- function computeCommonSourceDirectoryOfFilenames ( fileNames : string [ ] , currentDirectory : string , getCanonicalFileName : ( fileName : string ) => string ) : string {
44+ /* @internal */
45+ export function computeCommonSourceDirectoryOfFilenames ( fileNames : string [ ] , currentDirectory : string , getCanonicalFileName : ( fileName : string ) => string ) : string {
5246 let commonPathComponents : string [ ] ;
5347 const failed = forEach ( fileNames , sourceFile => {
5448 // Each file contributes into common source file path
@@ -196,7 +190,8 @@ namespace ts {
196190 }
197191
198192 const typeReferenceExtensions = [ ".d.ts" ] ;
199- export function resolveTypeReferenceDirective ( typeReferenceDirectiveName : string , containingFile : string , compilationRoot : string , options : CompilerOptions , host : ModuleResolutionHost ) : ResolvedTypeReferenceDirectiveWithFailedLookupLocations {
193+
194+ export function resolveTypeReferenceDirective ( typeReferenceDirectiveName : string , containingFile : string , options : CompilerOptions , host : ModuleResolutionHost ) : ResolvedTypeReferenceDirectiveWithFailedLookupLocations {
200195 const traceEnabled = isTraceEnabled ( options , host ) ;
201196 const moduleResolutionState : ModuleResolutionState = {
202197 compilerOptions : options ,
@@ -205,30 +200,47 @@ namespace ts {
205200 traceEnabled
206201 } ;
207202
203+ // use typesRoot and fallback to directory that contains tsconfig if typesRoot is not set
204+ const rootDir = options . typesRoot || ( options . configFilePath ? getDirectoryPath ( options . configFilePath ) : undefined ) ;
205+
208206 if ( traceEnabled ) {
209- trace ( host , Diagnostics . Resolving_type_reference_directive_0_from_1_with_compilation_root_dir_2 , typeReferenceDirectiveName , containingFile , compilationRoot ) ;
207+ if ( rootDir !== undefined ) {
208+ trace ( host , Diagnostics . Resolving_type_reference_directive_0_from_1_root_dir_2 , typeReferenceDirectiveName , containingFile , rootDir ) ;
209+ }
210+ else {
211+ trace ( host , Diagnostics . Resolving_type_reference_directive_0_from_1_root_dir_not_set , typeReferenceDirectiveName , containingFile ) ;
212+ }
210213 }
214+
211215 const failedLookupLocations : string [ ] = [ ] ;
212- // Check primary library paths
213- const effectivePrimarySearchPaths = options . typesSearchPaths || defaultLibrarySearchPaths ;
214- for ( const searchPath of effectivePrimarySearchPaths ) {
215- const primaryPath = combinePaths ( compilationRoot , searchPath ) ;
216- if ( traceEnabled ) {
217- trace ( host , Diagnostics . Resolving_with_primary_search_path_0 , primaryPath ) ;
218- }
219- const candidate = combinePaths ( primaryPath , typeReferenceDirectiveName ) ;
220- const candidateDirectory = getDirectoryPath ( candidate ) ;
221- const resolvedFile = loadNodeModuleFromDirectory ( typeReferenceExtensions , candidate , failedLookupLocations ,
222- ! directoryProbablyExists ( candidateDirectory , host ) , moduleResolutionState ) ;
223216
224- if ( resolvedFile ) {
217+ // Check primary library paths
218+ if ( rootDir !== undefined ) {
219+ const effectivePrimarySearchPaths = options . typesSearchPaths || defaultLibrarySearchPaths ;
220+ for ( const searchPath of effectivePrimarySearchPaths ) {
221+ const primaryPath = combinePaths ( rootDir , searchPath ) ;
225222 if ( traceEnabled ) {
226- trace ( host , Diagnostics . Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2 , typeReferenceDirectiveName , resolvedFile , true ) ;
223+ trace ( host , Diagnostics . Resolving_with_primary_search_path_0 , primaryPath ) ;
224+ }
225+ const candidate = combinePaths ( primaryPath , typeReferenceDirectiveName ) ;
226+ const candidateDirectory = getDirectoryPath ( candidate ) ;
227+ const resolvedFile = loadNodeModuleFromDirectory ( typeReferenceExtensions , candidate , failedLookupLocations ,
228+ ! directoryProbablyExists ( candidateDirectory , host ) , moduleResolutionState ) ;
229+
230+ if ( resolvedFile ) {
231+ if ( traceEnabled ) {
232+ trace ( host , Diagnostics . Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2 , typeReferenceDirectiveName , resolvedFile , true ) ;
233+ }
234+ return {
235+ resolvedTypeReferenceDirective : { primary : true , resolvedFileName : resolvedFile } ,
236+ failedLookupLocations
237+ } ;
227238 }
228- return {
229- resolvedTypeReferenceDirective : { primary : true , resolvedFileName : resolvedFile } ,
230- failedLookupLocations
231- } ;
239+ }
240+ }
241+ else {
242+ if ( traceEnabled ) {
243+ trace ( host , Diagnostics . Root_directory_cannot_be_determined_skipping_primary_search_paths ) ;
232244 }
233245 }
234246
@@ -889,8 +901,6 @@ namespace ts {
889901
890902 host = host || createCompilerHost ( options ) ;
891903
892- const compilationRoot = computeCompilationRoot ( rootNames , currentDirectory , options , getCanonicalFileName ) ;
893-
894904 // Map storing if there is emit blocking diagnostics for given input
895905 const hasEmitBlockingDiagnostics = createFileMap < boolean > ( getCanonicalFileName ) ;
896906
@@ -908,7 +918,7 @@ namespace ts {
908918 resolveTypeReferenceDirectiveNamesWorker = ( typeDirectiveNames , containingFile ) => host . resolveTypeReferenceDirectives ( typeDirectiveNames , containingFile ) ;
909919 }
910920 else {
911- const loader = ( typesRef : string , containingFile : string ) => resolveTypeReferenceDirective ( typesRef , containingFile , compilationRoot , options , host ) . resolvedTypeReferenceDirective ;
921+ const loader = ( typesRef : string , containingFile : string ) => resolveTypeReferenceDirective ( typesRef , containingFile , options , host ) . resolvedTypeReferenceDirective ;
912922 resolveTypeReferenceDirectiveNamesWorker = ( typeReferenceDirectiveNames , containingFile ) => loadWithLocalCache ( typeReferenceDirectiveNames , containingFile , loader ) ;
913923 }
914924
@@ -1690,7 +1700,7 @@ namespace ts {
16901700 const basePath = getDirectoryPath ( fileName ) ;
16911701 if ( ! options . noResolve ) {
16921702 processReferencedFiles ( file , basePath , isDefaultLib ) ;
1693- processTypeReferenceDirectives ( file , compilationRoot ) ;
1703+ processTypeReferenceDirectives ( file ) ;
16941704 }
16951705
16961706 // always process imported modules to record module name resolutions
@@ -1714,7 +1724,7 @@ namespace ts {
17141724 } ) ;
17151725 }
17161726
1717- function processTypeReferenceDirectives ( file : SourceFile , compilationRoot : string ) {
1727+ function processTypeReferenceDirectives ( file : SourceFile ) {
17181728 const typeDirectives = map ( file . typeReferenceDirectives , l => l . fileName ) ;
17191729 const resolutions = resolveTypeReferenceDirectiveNamesWorker ( typeDirectives , file . fileName ) ;
17201730
0 commit comments