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 (#39645)

* Add tests corresponding to repro from #37928

* 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

* Remove the scenario not fixed in this PR
This commit is contained in:
Sheetal Nandi
2020-07-17 16:25:32 -07:00
committed by GitHub
parent 92f41c81fb
commit 3b22339df8
6 changed files with 545 additions and 2 deletions

View File

@@ -37470,7 +37470,7 @@ namespace ts {
if (fileToDirective.has(file.path)) return;
fileToDirective.set(file.path, key);
for (const { fileName } of file.referencedFiles) {
const resolvedFile = resolveTripleslashReference(fileName, file.originalFileName);
const resolvedFile = resolveTripleslashReference(fileName, file.fileName);
const referencedFile = host.getSourceFile(resolvedFile);
if (referencedFile) {
addReferencedFilesToTypeDirective(referencedFile, key);

View File

@@ -2667,7 +2667,7 @@ namespace ts {
return;
}
forEach(file.referencedFiles, (ref, index) => {
const referencedFileName = resolveTripleslashReference(ref.fileName, file.originalFileName);
const referencedFileName = resolveTripleslashReference(ref.fileName, file.fileName);
processSourceFile(
referencedFileName,
isDefaultLib,

View File

@@ -110,6 +110,7 @@
"unittests/tsbuild/amdModulesWithOut.ts",
"unittests/tsbuild/configFileErrors.ts",
"unittests/tsbuild/containerOnlyReferenced.ts",
"unittests/tsbuild/declarationEmit.ts",
"unittests/tsbuild/demo.ts",
"unittests/tsbuild/emitDeclarationOnly.ts",
"unittests/tsbuild/emptyFiles.ts",

View File

@@ -0,0 +1,79 @@
namespace ts {
describe("unittests:: tsbuild:: declarationEmit", () => {
function getFiles(): vfs.FileSet {
return {
"/src/solution/tsconfig.base.json": JSON.stringify({
compilerOptions: {
rootDir: "./",
outDir: "lib"
}
}),
"/src/solution/tsconfig.json": JSON.stringify({
compilerOptions: { composite: true },
references: [{ path: "./src" }],
include: []
}),
"/src/solution/src/tsconfig.json": JSON.stringify({
compilerOptions: { composite: true },
references: [{ path: "./subProject" }, { path: "./subProject2" }],
include: []
}),
"/src/solution/src/subProject/tsconfig.json": JSON.stringify({
extends: "../../tsconfig.base.json",
compilerOptions: { composite: true },
references: [{ path: "../common" }],
include: ["./index.ts"]
}),
"/src/solution/src/subProject/index.ts": Utils.dedent`
import { Nominal } from '../common/nominal';
export type MyNominal = Nominal<string, 'MyNominal'>;`,
"/src/solution/src/subProject2/tsconfig.json": JSON.stringify({
extends: "../../tsconfig.base.json",
compilerOptions: { composite: true },
references: [{ path: "../subProject" }],
include: ["./index.ts"]
}),
"/src/solution/src/subProject2/index.ts": Utils.dedent`
import { MyNominal } from '../subProject/index';
const variable = {
key: 'value' as MyNominal,
};
export function getVar(): keyof typeof variable {
return 'key';
}`,
"/src/solution/src/common/tsconfig.json": JSON.stringify({
extends: "../../tsconfig.base.json",
compilerOptions: { composite: true },
include: ["./nominal.ts"]
}),
"/src/solution/src/common/nominal.ts": Utils.dedent`
/// <reference path="./types.d.ts" />
export declare type Nominal<T, Name extends string> = MyNominal<T, Name>;`,
"/src/solution/src/common/types.d.ts": Utils.dedent`
declare type MyNominal<T, Name extends string> = T & {
specialKey: Name;
};`,
};
}
verifyTsc({
scenario: "declarationEmit",
subScenario: "when declaration file is referenced through triple slash",
fs: () => loadProjectFromFiles(getFiles()),
commandLineArgs: ["--b", "/src/solution/tsconfig.json", "--verbose"]
});
verifyTsc({
scenario: "declarationEmit",
subScenario: "when declaration file is referenced through triple slash but uses no references",
fs: () => loadProjectFromFiles({
...getFiles(),
"/src/solution/tsconfig.json": JSON.stringify({
extends: "./tsconfig.base.json",
compilerOptions: { composite: true },
include: ["./src/**/*.ts"]
}),
}),
commandLineArgs: ["--b", "/src/solution/tsconfig.json", "--verbose"]
});
});
}

View File

@@ -0,0 +1,175 @@
Input::
//// [/lib/lib.d.ts]
/// <reference no-default-lib="true"/>
interface Boolean {}
interface Function {}
interface CallableFunction {}
interface NewableFunction {}
interface IArguments {}
interface Number { toExponential: any; }
interface Object {}
interface RegExp {}
interface String { charAt: any; }
interface Array<T> { length: number; [n: number]: T; }
interface ReadonlyArray<T> {}
declare const console: { log(msg: any): void; };
//// [/src/solution/src/common/nominal.ts]
/// <reference path="./types.d.ts" />
export declare type Nominal<T, Name extends string> = MyNominal<T, Name>;
//// [/src/solution/src/common/tsconfig.json]
//// [/src/solution/src/common/types.d.ts]
declare type MyNominal<T, Name extends string> = T & {
specialKey: Name;
};
//// [/src/solution/src/subProject/index.ts]
import { Nominal } from '../common/nominal';
export type MyNominal = Nominal<string, 'MyNominal'>;
//// [/src/solution/src/subProject/tsconfig.json]
//// [/src/solution/src/subProject2/index.ts]
import { MyNominal } from '../subProject/index';
const variable = {
key: 'value' as MyNominal,
};
export function getVar(): keyof typeof variable {
return 'key';
}
//// [/src/solution/src/subProject2/tsconfig.json]
//// [/src/solution/src/tsconfig.json]
//// [/src/solution/tsconfig.base.json]
{"compilerOptions":{"rootDir":"./","outDir":"lib"}}
//// [/src/solution/tsconfig.json]
{"extends":"./tsconfig.base.json","compilerOptions":{"composite":true},"include":["./src/**/*.ts"]}
Output::
/lib/tsc --b /src/solution/tsconfig.json --verbose
[12:00:00 AM] Projects in this build:
* src/solution/tsconfig.json
[12:00:00 AM] Project 'src/solution/tsconfig.json' is out of date because output file 'src/solution/lib/src/common/nominal.js' does not exist
[12:00:00 AM] Building project '/src/solution/tsconfig.json'...
exitCode:: ExitStatus.Success
//// [/src/solution/lib/src/common/nominal.d.ts]
/// <reference path="../../../src/common/types.d.ts" />
export declare type Nominal<T, Name extends string> = MyNominal<T, Name>;
//// [/src/solution/lib/src/common/nominal.js]
"use strict";
exports.__esModule = true;
/// <reference path="./types.d.ts" />
//// [/src/solution/lib/src/subProject/index.d.ts]
import { Nominal } from '../common/nominal';
export declare type MyNominal = Nominal<string, 'MyNominal'>;
//// [/src/solution/lib/src/subProject/index.js]
"use strict";
exports.__esModule = true;
//// [/src/solution/lib/src/subProject2/index.d.ts]
declare const variable: {
key: globalThis.MyNominal<string, "MyNominal">;
};
export declare function getVar(): keyof typeof variable;
export {};
//// [/src/solution/lib/src/subProject2/index.js]
"use strict";
exports.__esModule = true;
exports.getVar = void 0;
var variable = {
key: 'value'
};
function getVar() {
return 'key';
}
exports.getVar = getVar;
//// [/src/solution/lib/tsconfig.tsbuildinfo]
{
"program": {
"fileInfos": {
"../../../lib/lib.d.ts": {
"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; };",
"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; };",
"affectsGlobalScope": true
},
"../src/common/types.d.ts": {
"version": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
"signature": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
"affectsGlobalScope": true
},
"../src/common/nominal.ts": {
"version": "4107369137-/// <reference path=\"./types.d.ts\" />\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;",
"signature": "-18894149496-/// <reference path=\"../../../src/common/types.d.ts\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
"affectsGlobalScope": false
},
"../src/subproject/index.ts": {
"version": "-25117049605-import { Nominal } from '../common/nominal';\nexport type MyNominal = Nominal<string, 'MyNominal'>;",
"signature": "-21416888433-import { Nominal } from '../common/nominal';\r\nexport declare type MyNominal = Nominal<string, 'MyNominal'>;\r\n",
"affectsGlobalScope": false
},
"../src/subproject2/index.ts": {
"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}",
"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",
"affectsGlobalScope": false
}
},
"options": {
"rootDir": "..",
"outDir": "./",
"composite": true,
"configFilePath": "../tsconfig.json"
},
"referencedMap": {
"../src/common/nominal.ts": [
"../src/common/types.d.ts"
],
"../src/subproject/index.ts": [
"../src/common/nominal.ts"
],
"../src/subproject2/index.ts": [
"../src/subproject/index.ts"
]
},
"exportedModulesMap": {
"../src/subproject/index.ts": [
"../src/common/nominal.ts"
]
},
"semanticDiagnosticsPerFile": [
"../../../lib/lib.d.ts",
"../src/common/nominal.ts",
"../src/common/types.d.ts",
"../src/subproject/index.ts",
"../src/subproject2/index.ts"
]
},
"version": "FakeTSVersion"
}

View File

@@ -0,0 +1,288 @@
Input::
//// [/lib/lib.d.ts]
/// <reference no-default-lib="true"/>
interface Boolean {}
interface Function {}
interface CallableFunction {}
interface NewableFunction {}
interface IArguments {}
interface Number { toExponential: any; }
interface Object {}
interface RegExp {}
interface String { charAt: any; }
interface Array<T> { length: number; [n: number]: T; }
interface ReadonlyArray<T> {}
declare const console: { log(msg: any): void; };
//// [/src/solution/src/common/nominal.ts]
/// <reference path="./types.d.ts" />
export declare type Nominal<T, Name extends string> = MyNominal<T, Name>;
//// [/src/solution/src/common/tsconfig.json]
{"extends":"../../tsconfig.base.json","compilerOptions":{"composite":true},"include":["./nominal.ts"]}
//// [/src/solution/src/common/types.d.ts]
declare type MyNominal<T, Name extends string> = T & {
specialKey: Name;
};
//// [/src/solution/src/subProject/index.ts]
import { Nominal } from '../common/nominal';
export type MyNominal = Nominal<string, 'MyNominal'>;
//// [/src/solution/src/subProject/tsconfig.json]
{"extends":"../../tsconfig.base.json","compilerOptions":{"composite":true},"references":[{"path":"../common"}],"include":["./index.ts"]}
//// [/src/solution/src/subProject2/index.ts]
import { MyNominal } from '../subProject/index';
const variable = {
key: 'value' as MyNominal,
};
export function getVar(): keyof typeof variable {
return 'key';
}
//// [/src/solution/src/subProject2/tsconfig.json]
{"extends":"../../tsconfig.base.json","compilerOptions":{"composite":true},"references":[{"path":"../subProject"}],"include":["./index.ts"]}
//// [/src/solution/src/tsconfig.json]
{"compilerOptions":{"composite":true},"references":[{"path":"./subProject"},{"path":"./subProject2"}],"include":[]}
//// [/src/solution/tsconfig.base.json]
{"compilerOptions":{"rootDir":"./","outDir":"lib"}}
//// [/src/solution/tsconfig.json]
{"compilerOptions":{"composite":true},"references":[{"path":"./src"}],"include":[]}
Output::
/lib/tsc --b /src/solution/tsconfig.json --verbose
[12:00:00 AM] Projects in this build:
* src/solution/src/common/tsconfig.json
* src/solution/src/subProject/tsconfig.json
* src/solution/src/subProject2/tsconfig.json
* src/solution/src/tsconfig.json
* src/solution/tsconfig.json
[12:00:00 AM] Project 'src/solution/src/common/tsconfig.json' is out of date because output file 'src/solution/lib/src/common/nominal.js' does not exist
[12:00:00 AM] Building project '/src/solution/src/common/tsconfig.json'...
[12:00:00 AM] Project 'src/solution/src/subProject/tsconfig.json' is out of date because output file 'src/solution/lib/src/subProject/index.js' does not exist
[12:00:00 AM] Building project '/src/solution/src/subProject/tsconfig.json'...
[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
[12:00:00 AM] Building project '/src/solution/src/subProject2/tsconfig.json'...
exitCode:: ExitStatus.Success
//// [/src/solution/lib/src/common/nominal.d.ts]
/// <reference path="../../../src/common/types.d.ts" />
export declare type Nominal<T, Name extends string> = MyNominal<T, Name>;
//// [/src/solution/lib/src/common/nominal.js]
"use strict";
exports.__esModule = true;
/// <reference path="./types.d.ts" />
//// [/src/solution/lib/src/common/tsconfig.tsbuildinfo]
{
"program": {
"fileInfos": {
"../../../../../lib/lib.d.ts": {
"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; };",
"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; };",
"affectsGlobalScope": true
},
"../../../src/common/types.d.ts": {
"version": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
"signature": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
"affectsGlobalScope": true
},
"../../../src/common/nominal.ts": {
"version": "4107369137-/// <reference path=\"./types.d.ts\" />\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;",
"signature": "-18894149496-/// <reference path=\"../../../src/common/types.d.ts\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
"affectsGlobalScope": false
}
},
"options": {
"rootDir": "../../..",
"outDir": "../..",
"composite": true,
"configFilePath": "../../../src/common/tsconfig.json"
},
"referencedMap": {
"../../../src/common/nominal.ts": [
"../../../src/common/types.d.ts"
]
},
"exportedModulesMap": {},
"semanticDiagnosticsPerFile": [
"../../../../../lib/lib.d.ts",
"../../../src/common/nominal.ts",
"../../../src/common/types.d.ts"
]
},
"version": "FakeTSVersion"
}
//// [/src/solution/lib/src/subProject/index.d.ts]
import { Nominal } from '../common/nominal';
export declare type MyNominal = Nominal<string, 'MyNominal'>;
//// [/src/solution/lib/src/subProject/index.js]
"use strict";
exports.__esModule = true;
//// [/src/solution/lib/src/subProject/tsconfig.tsbuildinfo]
{
"program": {
"fileInfos": {
"../../../../../lib/lib.d.ts": {
"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; };",
"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; };",
"affectsGlobalScope": true
},
"../../../src/common/types.d.ts": {
"version": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
"signature": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
"affectsGlobalScope": true
},
"../common/nominal.d.ts": {
"version": "-18894149496-/// <reference path=\"../../../src/common/types.d.ts\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
"signature": "-18894149496-/// <reference path=\"../../../src/common/types.d.ts\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
"affectsGlobalScope": false
},
"../../../src/subproject/index.ts": {
"version": "-25117049605-import { Nominal } from '../common/nominal';\nexport type MyNominal = Nominal<string, 'MyNominal'>;",
"signature": "-21416888433-import { Nominal } from '../common/nominal';\r\nexport declare type MyNominal = Nominal<string, 'MyNominal'>;\r\n",
"affectsGlobalScope": false
}
},
"options": {
"rootDir": "../../..",
"outDir": "../..",
"composite": true,
"configFilePath": "../../../src/subProject/tsconfig.json"
},
"referencedMap": {
"../common/nominal.d.ts": [
"../../../src/common/types.d.ts"
],
"../../../src/subproject/index.ts": [
"../common/nominal.d.ts"
]
},
"exportedModulesMap": {
"../common/nominal.d.ts": [
"../../../src/common/types.d.ts"
],
"../../../src/subproject/index.ts": [
"../common/nominal.d.ts"
]
},
"semanticDiagnosticsPerFile": [
"../../../../../lib/lib.d.ts",
"../common/nominal.d.ts",
"../../../src/common/types.d.ts",
"../../../src/subproject/index.ts"
]
},
"version": "FakeTSVersion"
}
//// [/src/solution/lib/src/subProject2/index.d.ts]
declare const variable: {
key: globalThis.MyNominal<string, "MyNominal">;
};
export declare function getVar(): keyof typeof variable;
export {};
//// [/src/solution/lib/src/subProject2/index.js]
"use strict";
exports.__esModule = true;
exports.getVar = void 0;
var variable = {
key: 'value'
};
function getVar() {
return 'key';
}
exports.getVar = getVar;
//// [/src/solution/lib/src/subProject2/tsconfig.tsbuildinfo]
{
"program": {
"fileInfos": {
"../../../../../lib/lib.d.ts": {
"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; };",
"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; };",
"affectsGlobalScope": true
},
"../../../src/common/types.d.ts": {
"version": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
"signature": "23815050294-declare type MyNominal<T, Name extends string> = T & {\n specialKey: Name;\n};",
"affectsGlobalScope": true
},
"../common/nominal.d.ts": {
"version": "-18894149496-/// <reference path=\"../../../src/common/types.d.ts\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
"signature": "-18894149496-/// <reference path=\"../../../src/common/types.d.ts\" />\r\nexport declare type Nominal<T, Name extends string> = MyNominal<T, Name>;\r\n",
"affectsGlobalScope": false
},
"../subproject/index.d.ts": {
"version": "-21416888433-import { Nominal } from '../common/nominal';\r\nexport declare type MyNominal = Nominal<string, 'MyNominal'>;\r\n",
"signature": "-21416888433-import { Nominal } from '../common/nominal';\r\nexport declare type MyNominal = Nominal<string, 'MyNominal'>;\r\n",
"affectsGlobalScope": false
},
"../../../src/subproject2/index.ts": {
"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}",
"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",
"affectsGlobalScope": false
}
},
"options": {
"rootDir": "../../..",
"outDir": "../..",
"composite": true,
"configFilePath": "../../../src/subProject2/tsconfig.json"
},
"referencedMap": {
"../common/nominal.d.ts": [
"../../../src/common/types.d.ts"
],
"../subproject/index.d.ts": [
"../common/nominal.d.ts"
],
"../../../src/subproject2/index.ts": [
"../subproject/index.d.ts"
]
},
"exportedModulesMap": {
"../common/nominal.d.ts": [
"../../../src/common/types.d.ts"
],
"../subproject/index.d.ts": [
"../common/nominal.d.ts"
]
},
"semanticDiagnosticsPerFile": [
"../../../../../lib/lib.d.ts",
"../common/nominal.d.ts",
"../subproject/index.d.ts",
"../../../src/common/types.d.ts",
"../../../src/subproject2/index.ts"
]
},
"version": "FakeTSVersion"
}