Skip to content

Commit e80bda9

Browse files
committed
When resolving type reference directive, try optional resolution settings eg. baseUrl
Fixes #37928
1 parent 6505a1d commit e80bda9

File tree

2 files changed

+140
-50
lines changed

2 files changed

+140
-50
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,27 @@ namespace ts {
365365
const initialLocationForSecondaryLookup = containingFile && getDirectoryPath(containingFile);
366366

367367
if (initialLocationForSecondaryLookup !== undefined) {
368-
// check secondary locations
369-
if (traceEnabled) {
370-
trace(host, Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup);
371-
}
372-
let result: Resolved | undefined;
373-
if (!isExternalModuleNameRelative(typeReferenceDirectiveName)) {
374-
const searchResult = loadModuleFromNearestNodeModulesDirectory(Extensions.DtsOnly, typeReferenceDirectiveName, initialLocationForSecondaryLookup, moduleResolutionState, /*cache*/ undefined, /*redirectedReference*/ undefined);
375-
result = searchResult && searchResult.value;
376-
}
377-
else {
378-
const { path: candidate } = normalizePathAndParts(combinePaths(initialLocationForSecondaryLookup, typeReferenceDirectiveName));
379-
result = nodeLoadModuleByRelativeName(Extensions.DtsOnly, candidate, /*onlyRecordFailures*/ false, moduleResolutionState, /*considerPackageJson*/ true);
368+
// Try using baseUrl resolution
369+
let result = tryLoadModuleUsingOptionalResolutionSettings(
370+
Extensions.DtsOnly,
371+
typeReferenceDirectiveName,
372+
initialLocationForSecondaryLookup,
373+
(extensions, candidate, onlyRecordFailures, state) => nodeLoadModuleByRelativeName(extensions, candidate, onlyRecordFailures, state, /*considerPackageJson*/ true),
374+
moduleResolutionState
375+
);
376+
if (!result) {
377+
// check secondary locations
378+
if (traceEnabled) {
379+
trace(host, Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup);
380+
}
381+
if (!isExternalModuleNameRelative(typeReferenceDirectiveName)) {
382+
const searchResult = loadModuleFromNearestNodeModulesDirectory(Extensions.DtsOnly, typeReferenceDirectiveName, initialLocationForSecondaryLookup, moduleResolutionState, /*cache*/ undefined, /*redirectedReference*/ undefined);
383+
result = searchResult && searchResult.value;
384+
}
385+
else {
386+
const { path: candidate } = normalizePathAndParts(combinePaths(initialLocationForSecondaryLookup, typeReferenceDirectiveName));
387+
result = nodeLoadModuleByRelativeName(Extensions.DtsOnly, candidate, /*onlyRecordFailures*/ false, moduleResolutionState, /*considerPackageJson*/ true);
388+
}
380389
}
381390
const resolvedFile = resolvedTypeScriptOnly(result);
382391
if (!resolvedFile && traceEnabled) {

tests/baselines/reference/tsbuild/declarationEmit/initial-build/when-declaration-file-is-referenced-through-triple-slash-and-uses-baseUrl.js

Lines changed: 119 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ export type MyNominal = Nominal<string, 'MyNominal'>;
3434
{"extends":"../../tsconfig.base.json","compilerOptions":{"composite":true},"references":[{"path":"../common"}],"include":["./index.ts"]}
3535

3636
//// [/src/solution/src/subProject2/index.ts]
37-
37+
import { MyNominal } from '../subProject/index';
38+
const variable = {
39+
key: 'value' as MyNominal,
40+
};
41+
export function getVar(): keyof typeof variable {
42+
return 'key';
43+
}
3844

3945
//// [/src/solution/src/subProject2/tsconfig.json]
4046
{"extends":"../../tsconfig.base.json","compilerOptions":{"composite":true},"references":[{"path":"../subProject"}],"include":["./index.ts"]}
@@ -67,24 +73,11 @@ Output::
6773

6874
[12:00:00 AM] Building project '/src/solution/src/subProject/tsconfig.json'...
6975

70-
src/solution/lib/src/common/nominal.d.ts:2:55 - error TS2304: Cannot find name 'MyNominal'.
71-
72-
2 export declare type Nominal<T, Name extends string> = MyNominal<T, Name>;
73-
   ~~~~~~~~~
74-
75-
src/solution/lib/src/common/nominal.d.ts:1:23 - error TS2688: Cannot find type definition file for 'types'.
76-
77-
1 /// <reference types="types" />
78-
   ~~~~~
79-
80-
[12:00:00 AM] Project 'src/solution/src/subProject2/tsconfig.json' can't be built because its dependency 'src/solution/src/subProject' has errors
76+
[12:00:00 AM] Project 'src/solution/src/subProject2/tsconfig.json' is out of date because output file 'src/solution/lib/src/subProject2/index.js' does not exist
8177

82-
[[90m12:00:00 AM[0m] Skipping build of project '/src/solution/src/subProject2/tsconfig.json' because its dependency '/src/solution/src/subProject' has errors
78+
[[90m12:00:00 AM[0m] Building project '/src/solution/src/subProject2/tsconfig.json'...
8379

84-
85-
Found 2 errors.
86-
87-
exitCode:: ExitStatus.DiagnosticsPresent_OutputsGenerated
80+
exitCode:: ExitStatus.Success
8881

8982

9083
//// [/src/solution/lib/src/common/nominal.d.ts]
@@ -140,6 +133,16 @@ exports.__esModule = true;
140133
"version": "FakeTSVersion"
141134
}
142135

136+
//// [/src/solution/lib/src/subProject/index.d.ts]
137+
import { Nominal } from '../common/nominal';
138+
export declare type MyNominal = Nominal<string, 'MyNominal'>;
139+
140+
141+
//// [/src/solution/lib/src/subProject/index.js]
142+
"use strict";
143+
exports.__esModule = true;
144+
145+
143146
//// [/src/solution/lib/src/subProject/tsconfig.tsbuildinfo]
144147
{
145148
"program": {
@@ -149,6 +152,11 @@ exports.__esModule = true;
149152
"signature": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
150153
"affectsGlobalScope": true
151154
},
155+
"../../../src/common/types.d.ts": {
156+
"version": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
157+
"signature": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
158+
"affectsGlobalScope": true
159+
},
152160
"../common/nominal.d.ts": {
153161
"version": "-2060908103-/// <reference types=\"types\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
154162
"signature": "-2060908103-/// <reference types=\"types\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
@@ -167,41 +175,114 @@ exports.__esModule = true;
167175
"configFilePath": "../../../src/subProject/tsconfig.json"
168176
},
169177
"referencedMap": {
178+
"../common/nominal.d.ts": [
179+
"../../../src/common/types.d.ts"
180+
],
170181
"../../../src/subproject/index.ts": [
171182
"../common/nominal.d.ts"
172183
]
173184
},
174185
"exportedModulesMap": {
186+
"../common/nominal.d.ts": [
187+
"../../../src/common/types.d.ts"
188+
],
175189
"../../../src/subproject/index.ts": [
176190
"../common/nominal.d.ts"
177191
]
178192
},
179193
"semanticDiagnosticsPerFile": [
180194
"../../../../../lib/lib.d.ts",
181-
[
182-
"../common/nominal.d.ts",
183-
[
184-
{
185-
"file": "../common/nominal.d.ts",
186-
"start": 87,
187-
"length": 9,
188-
"messageText": "Cannot find name 'MyNominal'.",
189-
"category": 1,
190-
"code": 2304
191-
}
192-
]
193-
],
195+
"../common/nominal.d.ts",
196+
"../../../src/common/types.d.ts",
194197
"../../../src/subproject/index.ts"
195-
],
196-
"affectedFilesPendingEmit": [
197-
[
198-
"../common/nominal.d.ts",
199-
1
198+
]
199+
},
200+
"version": "FakeTSVersion"
201+
}
202+
203+
//// [/src/solution/lib/src/subProject2/index.d.ts]
204+
/// <reference types="types" />
205+
declare const variable: {
206+
key: globalThis.MyNominal<string, "MyNominal">;
207+
};
208+
export declare function getVar(): keyof typeof variable;
209+
export {};
210+
211+
212+
//// [/src/solution/lib/src/subProject2/index.js]
213+
"use strict";
214+
exports.__esModule = true;
215+
exports.getVar = void 0;
216+
var variable = {
217+
key: 'value'
218+
};
219+
function getVar() {
220+
return 'key';
221+
}
222+
exports.getVar = getVar;
223+
224+
225+
//// [/src/solution/lib/src/subProject2/tsconfig.tsbuildinfo]
226+
{
227+
"program": {
228+
"fileInfos": {
229+
"../../../../../lib/lib.d.ts": {
230+
"version": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
231+
"signature": "3858781397-/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }\ninterface ReadonlyArray<T> {}\ndeclare const console: { log(msg: any): void; };",
232+
"affectsGlobalScope": true
233+
},
234+
"../../../src/common/types.d.ts": {
235+
"version": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
236+
"signature": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
237+
"affectsGlobalScope": true
238+
},
239+
"../common/nominal.d.ts": {
240+
"version": "-2060908103-/// <reference types=\"types\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
241+
"signature": "-2060908103-/// <reference types=\"types\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
242+
"affectsGlobalScope": false
243+
},
244+
"../subproject/index.d.ts": {
245+
"version": "-21416888433-import { Nominal } from '../common/nominal';\r\nexport declare type MyNominal = Nominal<string, 'MyNominal'>;\r\n",
246+
"signature": "-21416888433-import { Nominal } from '../common/nominal';\r\nexport declare type MyNominal = Nominal<string, 'MyNominal'>;\r\n",
247+
"affectsGlobalScope": false
248+
},
249+
"../../../src/subproject2/index.ts": {
250+
"version": "2747033208-import { MyNominal } from '../subProject/index';\nconst variable = {\n key: 'value' as MyNominal,\n};\nexport function getVar(): keyof typeof variable {\n return 'key';\n}",
251+
"signature": "-8779403458-/// <reference types=\"types\" />\r\ndeclare const variable: {\r\n key: globalThis.MyNominal<string, \"MyNominal\">;\r\n};\r\nexport declare function getVar(): keyof typeof variable;\r\nexport {};\r\n",
252+
"affectsGlobalScope": false
253+
}
254+
},
255+
"options": {
256+
"rootDir": "../../..",
257+
"outDir": "../..",
258+
"composite": true,
259+
"configFilePath": "../../../src/subProject2/tsconfig.json"
260+
},
261+
"referencedMap": {
262+
"../common/nominal.d.ts": [
263+
"../../../src/common/types.d.ts"
264+
],
265+
"../subproject/index.d.ts": [
266+
"../common/nominal.d.ts"
267+
],
268+
"../../../src/subproject2/index.ts": [
269+
"../subproject/index.d.ts"
270+
]
271+
},
272+
"exportedModulesMap": {
273+
"../common/nominal.d.ts": [
274+
"../../../src/common/types.d.ts"
200275
],
201-
[
202-
"../../../src/subproject/index.ts",
203-
1
276+
"../subproject/index.d.ts": [
277+
"../common/nominal.d.ts"
204278
]
279+
},
280+
"semanticDiagnosticsPerFile": [
281+
"../../../../../lib/lib.d.ts",
282+
"../common/nominal.d.ts",
283+
"../subproject/index.d.ts",
284+
"../../../src/common/types.d.ts",
285+
"../../../src/subproject2/index.ts"
205286
]
206287
},
207288
"version": "FakeTSVersion"

0 commit comments

Comments
 (0)