Skip to content

Commit 6505a1d

Browse files
committed
Triple slash references must resolve against the resolved file name (.d.ts and not original source file) since they are rewritten in the .d.ts emit
1 parent 5b3bc60 commit 6505a1d

File tree

3 files changed

+114
-40
lines changed

3 files changed

+114
-40
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37470,7 +37470,7 @@ namespace ts {
3747037470
if (fileToDirective.has(file.path)) return;
3747137471
fileToDirective.set(file.path, key);
3747237472
for (const { fileName } of file.referencedFiles) {
37473-
const resolvedFile = resolveTripleslashReference(fileName, file.originalFileName);
37473+
const resolvedFile = resolveTripleslashReference(fileName, file.fileName);
3747437474
const referencedFile = host.getSourceFile(resolvedFile);
3747537475
if (referencedFile) {
3747637476
addReferencedFilesToTypeDirective(referencedFile, key);

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2667,7 +2667,7 @@ namespace ts {
26672667
return;
26682668
}
26692669
forEach(file.referencedFiles, (ref, index) => {
2670-
const referencedFileName = resolveTripleslashReference(ref.fileName, file.originalFileName);
2670+
const referencedFileName = resolveTripleslashReference(ref.fileName, file.fileName);
26712671
processSourceFile(
26722672
referencedFileName,
26732673
isDefaultLib,

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

Lines changed: 112 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:22 - error TS6053: File '/src/src/common/types.d.ts' not found.
76-
77-
1 /// <reference path="../../../src/common/types.d.ts" />
78-
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
7977

80-
[[90m12:00:00 AM[0m] Project 'src/solution/src/subProject2/tsconfig.json' can't be built because its dependency 'src/solution/src/subProject' has errors
78+
[[90m12:00:00 AM[0m] Building project '/src/solution/src/subProject2/tsconfig.json'...
8179

82-
[12:00:00 AM] Skipping build of project '/src/solution/src/subProject2/tsconfig.json' because its dependency '/src/solution/src/subProject' has errors
83-
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]
@@ -139,6 +132,16 @@ exports.__esModule = true;
139132
"version": "FakeTSVersion"
140133
}
141134

135+
//// [/src/solution/lib/src/subProject/index.d.ts]
136+
import { Nominal } from '../common/nominal';
137+
export declare type MyNominal = Nominal<string, 'MyNominal'>;
138+
139+
140+
//// [/src/solution/lib/src/subProject/index.js]
141+
"use strict";
142+
exports.__esModule = true;
143+
144+
142145
//// [/src/solution/lib/src/subProject/tsconfig.tsbuildinfo]
143146
{
144147
"program": {
@@ -148,6 +151,11 @@ exports.__esModule = true;
148151
"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; };",
149152
"affectsGlobalScope": true
150153
},
154+
"../../../src/common/types.d.ts": {
155+
"version": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
156+
"signature": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
157+
"affectsGlobalScope": true
158+
},
151159
"../common/nominal.d.ts": {
152160
"version": "-18894149496-/// <reference path=\"../../../src/common/types.d.ts\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
153161
"signature": "-18894149496-/// <reference path=\"../../../src/common/types.d.ts\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
@@ -183,30 +191,96 @@ exports.__esModule = true;
183191
},
184192
"semanticDiagnosticsPerFile": [
185193
"../../../../../lib/lib.d.ts",
186-
[
187-
"../common/nominal.d.ts",
188-
[
189-
{
190-
"file": "../common/nominal.d.ts",
191-
"start": 111,
192-
"length": 9,
193-
"messageText": "Cannot find name 'MyNominal'.",
194-
"category": 1,
195-
"code": 2304
196-
}
197-
]
198-
],
194+
"../common/nominal.d.ts",
195+
"../../../src/common/types.d.ts",
199196
"../../../src/subproject/index.ts"
200-
],
201-
"affectedFilesPendingEmit": [
202-
[
203-
"../common/nominal.d.ts",
204-
1
197+
]
198+
},
199+
"version": "FakeTSVersion"
200+
}
201+
202+
//// [/src/solution/lib/src/subProject2/index.d.ts]
203+
declare const variable: {
204+
key: globalThis.MyNominal<string, "MyNominal">;
205+
};
206+
export declare function getVar(): keyof typeof variable;
207+
export {};
208+
209+
210+
//// [/src/solution/lib/src/subProject2/index.js]
211+
"use strict";
212+
exports.__esModule = true;
213+
exports.getVar = void 0;
214+
var variable = {
215+
key: 'value'
216+
};
217+
function getVar() {
218+
return 'key';
219+
}
220+
exports.getVar = getVar;
221+
222+
223+
//// [/src/solution/lib/src/subProject2/tsconfig.tsbuildinfo]
224+
{
225+
"program": {
226+
"fileInfos": {
227+
"../../../../../lib/lib.d.ts": {
228+
"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; };",
229+
"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; };",
230+
"affectsGlobalScope": true
231+
},
232+
"../../../src/common/types.d.ts": {
233+
"version": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
234+
"signature": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
235+
"affectsGlobalScope": true
236+
},
237+
"../common/nominal.d.ts": {
238+
"version": "-18894149496-/// <reference path=\"../../../src/common/types.d.ts\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
239+
"signature": "-18894149496-/// <reference path=\"../../../src/common/types.d.ts\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
240+
"affectsGlobalScope": false
241+
},
242+
"../subproject/index.d.ts": {
243+
"version": "-21416888433-import { Nominal } from '../common/nominal';\r\nexport declare type MyNominal = Nominal<string, 'MyNominal'>;\r\n",
244+
"signature": "-21416888433-import { Nominal } from '../common/nominal';\r\nexport declare type MyNominal = Nominal<string, 'MyNominal'>;\r\n",
245+
"affectsGlobalScope": false
246+
},
247+
"../../../src/subproject2/index.ts": {
248+
"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}",
249+
"signature": "-448645961-declare const variable: {\r\n key: globalThis.MyNominal<string, \"MyNominal\">;\r\n};\r\nexport declare function getVar(): keyof typeof variable;\r\nexport {};\r\n",
250+
"affectsGlobalScope": false
251+
}
252+
},
253+
"options": {
254+
"rootDir": "../../..",
255+
"outDir": "../..",
256+
"composite": true,
257+
"configFilePath": "../../../src/subProject2/tsconfig.json"
258+
},
259+
"referencedMap": {
260+
"../common/nominal.d.ts": [
261+
"../../../src/common/types.d.ts"
205262
],
206-
[
207-
"../../../src/subproject/index.ts",
208-
1
263+
"../subproject/index.d.ts": [
264+
"../common/nominal.d.ts"
265+
],
266+
"../../../src/subproject2/index.ts": [
267+
"../subproject/index.d.ts"
209268
]
269+
},
270+
"exportedModulesMap": {
271+
"../common/nominal.d.ts": [
272+
"../../../src/common/types.d.ts"
273+
],
274+
"../subproject/index.d.ts": [
275+
"../common/nominal.d.ts"
276+
]
277+
},
278+
"semanticDiagnosticsPerFile": [
279+
"../../../../../lib/lib.d.ts",
280+
"../common/nominal.d.ts",
281+
"../subproject/index.d.ts",
282+
"../../../src/common/types.d.ts",
283+
"../../../src/subproject2/index.ts"
210284
]
211285
},
212286
"version": "FakeTSVersion"

0 commit comments

Comments
 (0)