Skip to content

Commit 30fde91

Browse files
authored
use location of config file as initial location for automatic type reference inclusion if possible (#12341) (#12361)
1 parent 5f6ec46 commit 30fde91

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

src/compiler/program.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ namespace ts {
364364

365365
if (typeReferences.length) {
366366
// This containingFilename needs to match with the one used in managed-side
367-
const containingFilename = combinePaths(host.getCurrentDirectory(), "__inferred type names__.ts");
367+
const containingDirectory = options.configFilePath ? getDirectoryPath(options.configFilePath) : host.getCurrentDirectory();
368+
const containingFilename = combinePaths(containingDirectory, "__inferred type names__.ts");
368369
const resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, containingFilename);
369370
for (let i = 0; i < typeReferences.length; i++) {
370371
processTypeReferenceDirective(typeReferences[i], resolutions[i]);

src/harness/unittests/tsserverProjectSystem.ts

+24
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,30 @@ namespace ts.projectSystem {
23392339
projectService.openExternalProject({ rootFiles: toExternalFiles([f1.path, config.path]), options: {}, projectFileName: projectName });
23402340
projectService.checkNumberOfProjects({ configuredProjects: 1 });
23412341
});
2342+
2343+
it("types should load from config file path if config exists", () => {
2344+
const f1 = {
2345+
path: "/a/b/app.ts",
2346+
content: "let x = 1"
2347+
};
2348+
const config = {
2349+
path: "/a/b/tsconfig.json",
2350+
content: JSON.stringify({ compilerOptions: { types: ["node"], typeRoots: [] } })
2351+
};
2352+
const node = {
2353+
path: "/a/b/node_modules/@types/node/index.d.ts",
2354+
content: "declare var process: any"
2355+
};
2356+
const cwd = {
2357+
path: "/a/c"
2358+
};
2359+
debugger;
2360+
const host = createServerHost([f1, config, node, cwd], { currentDirectory: cwd.path });
2361+
const projectService = createProjectService(host);
2362+
projectService.openClientFile(f1.path);
2363+
projectService.checkNumberOfProjects({ configuredProjects: 1 });
2364+
checkProjectActualFiles(projectService.configuredProjects[0], [f1.path, node.path]);
2365+
});
23422366
});
23432367

23442368
describe("add the missing module file for inferred project", () => {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[
2-
"======== Resolving type reference directive 'jquery', containing file '/__inferred type names__.ts', root directory '/a/types'. ========",
2+
"======== Resolving type reference directive 'jquery', containing file '/a/__inferred type names__.ts', root directory '/a/types'. ========",
33
"Resolving with primary search path '/a/types'",
44
"File '/a/types/jquery/package.json' does not exist.",
55
"File '/a/types/jquery/index.d.ts' exist - use it as a name resolution result.",

tests/baselines/reference/typeRootsFromMultipleNodeModulesDirectories.trace.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,19 @@
149149
"File '/node_modules/abc/index.js' does not exist.",
150150
"File '/node_modules/abc/index.jsx' does not exist.",
151151
"======== Module name 'abc' was not resolved. ========",
152-
"======== Resolving type reference directive 'grumpy', containing file '/src/__inferred type names__.ts', root directory '/foo/node_modules/@types,/node_modules/@types'. ========",
152+
"======== Resolving type reference directive 'grumpy', containing file '/foo/bar/__inferred type names__.ts', root directory '/foo/node_modules/@types,/node_modules/@types'. ========",
153153
"Resolving with primary search path '/foo/node_modules/@types, /node_modules/@types'",
154154
"File '/foo/node_modules/@types/grumpy/package.json' does not exist.",
155155
"File '/foo/node_modules/@types/grumpy/index.d.ts' exist - use it as a name resolution result.",
156156
"Resolving real path for '/foo/node_modules/@types/grumpy/index.d.ts', result '/foo/node_modules/@types/grumpy/index.d.ts'",
157157
"======== Type reference directive 'grumpy' was successfully resolved to '/foo/node_modules/@types/grumpy/index.d.ts', primary: true. ========",
158-
"======== Resolving type reference directive 'sneezy', containing file '/src/__inferred type names__.ts', root directory '/foo/node_modules/@types,/node_modules/@types'. ========",
158+
"======== Resolving type reference directive 'sneezy', containing file '/foo/bar/__inferred type names__.ts', root directory '/foo/node_modules/@types,/node_modules/@types'. ========",
159159
"Resolving with primary search path '/foo/node_modules/@types, /node_modules/@types'",
160160
"File '/foo/node_modules/@types/sneezy/package.json' does not exist.",
161161
"File '/foo/node_modules/@types/sneezy/index.d.ts' exist - use it as a name resolution result.",
162162
"Resolving real path for '/foo/node_modules/@types/sneezy/index.d.ts', result '/foo/node_modules/@types/sneezy/index.d.ts'",
163163
"======== Type reference directive 'sneezy' was successfully resolved to '/foo/node_modules/@types/sneezy/index.d.ts', primary: true. ========",
164-
"======== Resolving type reference directive 'dopey', containing file '/src/__inferred type names__.ts', root directory '/foo/node_modules/@types,/node_modules/@types'. ========",
164+
"======== Resolving type reference directive 'dopey', containing file '/foo/bar/__inferred type names__.ts', root directory '/foo/node_modules/@types,/node_modules/@types'. ========",
165165
"Resolving with primary search path '/foo/node_modules/@types, /node_modules/@types'",
166166
"File '/foo/node_modules/@types/dopey/package.json' does not exist.",
167167
"File '/foo/node_modules/@types/dopey/index.d.ts' does not exist.",

0 commit comments

Comments
 (0)