Skip to content

Commit e2a23fd

Browse files
committed
remove compilationRoot parameter, use typesRoot/config file location as a root when computing primary locations
1 parent ecbbe02 commit e2a23fd

35 files changed

Lines changed: 174 additions & 213 deletions

src/compiler/commandLineParser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ namespace ts {
321321
isFilePath: true
322322
}
323323
},
324+
{
325+
name: "typesRoot",
326+
type: "string"
327+
},
324328
{
325329
name: "traceResolution",
326330
type: "boolean",

src/compiler/diagnosticMessages.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,7 +2637,7 @@
26372637
"category": "Error",
26382638
"code": 6114
26392639
},
2640-
"======== Resolving type reference directive '{0}' from '{1}' with compilation root dir '{2}'. ========": {
2640+
"======== Resolving type reference directive '{0}' from '{1}', root dir '{2}'. ========": {
26412641
"category": "Message",
26422642
"code": 6115
26432643
},
@@ -2661,7 +2661,14 @@
26612661
"category": "Message",
26622662
"code": 6120
26632663
},
2664-
2664+
"Root directory cannot be determined, skipping primary search paths.": {
2665+
"category": "Message",
2666+
"code": 6121
2667+
},
2668+
"======== Resolving type reference directive '{0}' from '{1}', root dir not set. ========": {
2669+
"category": "Message",
2670+
"code": 6122
2671+
},
26652672
"Variable '{0}' implicitly has an '{1}' type.": {
26662673
"category": "Error",
26672674
"code": 7005

src/compiler/program.ts

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/compiler/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,6 +2473,9 @@ namespace ts {
24732473
/* @internal */
24742474
// When options come from a config file, its path is recorded here
24752475
configFilePath?: string;
2476+
/* @internal */
2477+
// Path used to used to compute primary search locations
2478+
typesRoot?: string;
24762479

24772480
list?: string[];
24782481
[option: string]: CompilerOptionsValue;

src/harness/fourslash.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ namespace FourSlash {
247247
// Create a new Services Adapter
248248
this.cancellationToken = new TestCancellationToken();
249249
const compilationOptions = convertGlobalOptionsToCompilerOptions(this.testData.globalOptions);
250+
if (compilationOptions.typesRoot) {
251+
compilationOptions.typesRoot = ts.getNormalizedAbsolutePath(compilationOptions.typesRoot, this.basePath);
252+
}
253+
250254
const languageServiceAdapter = this.getLanguageServiceAdapter(testType, this.cancellationToken, compilationOptions);
251255
this.languageServiceAdapterHost = languageServiceAdapter.getHost();
252256
this.languageService = languageServiceAdapter.getLanguageService();

src/harness/harnessLanguageService.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,9 @@ namespace Harness.LanguageService {
247247
const scriptInfo = this.getScriptInfo(fileName);
248248
const preprocessInfo = ts.preProcessFile(scriptInfo.content, /*readImportFiles*/ false);
249249
const resolutions: ts.Map<ts.ResolvedTypeReferenceDirective> = {};
250-
const rootFiles = this.getFilenames();
251-
const currentDirectory = this.getCurrentDirectory();
252250
const settings = this.nativeHost.getCompilationSettings();
253-
const compilationRoot = ts.computeCompilationRoot(rootFiles, currentDirectory, settings, ts.createGetCanonicalFileName(false));
254251
for (const typeReferenceDirective of preprocessInfo.typeReferenceDirectives) {
255-
const resolutionInfo = ts.resolveTypeReferenceDirective(typeReferenceDirective.fileName, fileName, compilationRoot, settings, moduleResolutionHost);
252+
const resolutionInfo = ts.resolveTypeReferenceDirective(typeReferenceDirective.fileName, fileName, settings, moduleResolutionHost);
256253
if (resolutionInfo.resolvedTypeReferenceDirective.resolvedFileName) {
257254
resolutions[typeReferenceDirective.fileName] = resolutionInfo.resolvedTypeReferenceDirective;
258255
}

src/server/editorServices.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ namespace ts.server {
9696
compilationSettings: ts.CompilerOptions;
9797
filenameToScript: ts.FileMap<ScriptInfo>;
9898
roots: ScriptInfo[] = [];
99-
compilationRoot: string;
10099

101100
private resolvedModuleNames: ts.FileMap<Map<TimestampedResolvedModule>>;
102101
private resolvedTypeReferenceDirectives: ts.FileMap<Map<TimestampedResolvedTypeReferenceDirective>>;
@@ -172,13 +171,7 @@ namespace ts.server {
172171
}
173172

174173
resolveTypeReferenceDirectives(typeDirectiveNames: string[], containingFile: string): ResolvedTypeReferenceDirective[] {
175-
if (this.compilationRoot === undefined) {
176-
this.compilationRoot = computeCompilationRoot(this.getScriptFileNames(), this.getCurrentDirectory(), this.getCompilationSettings(), this.getCanonicalFileName);
177-
}
178-
const loader = (name: string, containingFile: string, options: CompilerOptions, host: ModuleResolutionHost) => {
179-
return resolveTypeReferenceDirective(name, containingFile, this.compilationRoot, options, this.moduleResolutionHost);
180-
};
181-
return this.resolveNamesWithLocalCache(typeDirectiveNames, containingFile, this.resolvedTypeReferenceDirectives, loader, m => m.resolvedTypeReferenceDirective);
174+
return this.resolveNamesWithLocalCache(typeDirectiveNames, containingFile, this.resolvedTypeReferenceDirectives, resolveTypeReferenceDirective, m => m.resolvedTypeReferenceDirective);
182175
}
183176

184177
resolveModuleNames(moduleNames: string[], containingFile: string): ResolvedModule[] {
@@ -262,8 +255,6 @@ namespace ts.server {
262255
if (!this.filenameToScript.contains(info.path)) {
263256
this.filenameToScript.set(info.path, info);
264257
this.roots.push(info);
265-
// reset compilation root
266-
this.compilationRoot = undefined;
267258
}
268259
}
269260

@@ -273,8 +264,6 @@ namespace ts.server {
273264
this.roots = copyListRemovingItem(info, this.roots);
274265
this.resolvedModuleNames.remove(info.path);
275266
this.resolvedTypeReferenceDirectives.remove(info.path);
276-
// reset compilation root
277-
this.compilationRoot = undefined;
278267
}
279268
}
280269

src/services/shims.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,6 @@ namespace ts {
907907

908908
class CoreServicesShimObject extends ShimBase implements CoreServicesShim {
909909
private logPerformance = false;
910-
private getCanonicalFileName = createGetCanonicalFileName(false);
911910

912911
constructor(factory: ShimFactory, public logger: Logger, private host: CoreServicesShimHostAdapter) {
913912
super(factory);
@@ -928,18 +927,10 @@ namespace ts {
928927
});
929928
}
930929

931-
public computeCompilationRoot(filesJson: string, currentDirectory: string, compilerOptionsJson: string): string {
932-
return this.forwardJSONCall("computeCompilationRoot", () => {
933-
const files = <string[]>JSON.parse(filesJson);
934-
const compilerOptions = <CompilerOptions>JSON.parse(compilerOptionsJson);
935-
return computeCompilationRoot(files, currentDirectory, compilerOptions, this.getCanonicalFileName);
936-
});
937-
}
938-
939-
public resolveTypeReferenceDirective(fileName: string, typeReferenceDirective: string, compilationRoot: string, compilerOptionsJson: string): string {
930+
public resolveTypeReferenceDirective(fileName: string, typeReferenceDirective: string, compilerOptionsJson: string): string {
940931
return this.forwardJSONCall(`resolveTypeReferenceDirective(${fileName})`, () => {
941932
const compilerOptions = <CompilerOptions>JSON.parse(compilerOptionsJson);
942-
const result = resolveTypeReferenceDirective(typeReferenceDirective, normalizeSlashes(fileName), compilationRoot, compilerOptions, this.host);
933+
const result = resolveTypeReferenceDirective(typeReferenceDirective, normalizeSlashes(fileName), compilerOptions, this.host);
943934
return {
944935
resolvedFileName: result.resolvedTypeReferenceDirective ? result.resolvedTypeReferenceDirective.resolvedFileName : undefined,
945936
primary: result.resolvedTypeReferenceDirective ? result.resolvedTypeReferenceDirective.primary : true,

tests/baselines/reference/library-reference-1.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
"======== Resolving type reference directive 'jquery' from '/consumer.ts' with compilation root dir '/'. ========",
2+
"======== Resolving type reference directive 'jquery' from '/consumer.ts', root dir '/'. ========",
33
"Resolving with primary search path '/types/'",
44
"File '/types/jquery/package.json' does not exist.",
55
"File '/types/jquery/index.d.ts' exist - use it as a name resolution result.",

tests/baselines/reference/library-reference-10.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
"======== Resolving type reference directive 'jquery' from '/consumer.ts' with compilation root dir '/'. ========",
2+
"======== Resolving type reference directive 'jquery' from '/consumer.ts', root dir '/'. ========",
33
"Resolving with primary search path '/types/'",
44
"Found 'package.json' at '/types/jquery/package.json'.",
55
"'package.json' has 'typings' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",

0 commit comments

Comments
 (0)