diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e0dea081f67e8..815b29454fd4b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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); diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 7ac32c14a7e3d..132938d5fb8c7 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -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, diff --git a/src/testRunner/tsconfig.json b/src/testRunner/tsconfig.json index 00764792e70ac..8b0123e1ef9be 100644 --- a/src/testRunner/tsconfig.json +++ b/src/testRunner/tsconfig.json @@ -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", diff --git a/src/testRunner/unittests/tsbuild/declarationEmit.ts b/src/testRunner/unittests/tsbuild/declarationEmit.ts new file mode 100644 index 0000000000000..6139f585b9663 --- /dev/null +++ b/src/testRunner/unittests/tsbuild/declarationEmit.ts @@ -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;`, + "/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` +/// +export declare type Nominal = MyNominal;`, + "/src/solution/src/common/types.d.ts": Utils.dedent` +declare type MyNominal = 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"] + }); + }); +} diff --git a/tests/baselines/reference/tsbuild/declarationEmit/initial-build/when-declaration-file-is-referenced-through-triple-slash-but-uses-no-references.js b/tests/baselines/reference/tsbuild/declarationEmit/initial-build/when-declaration-file-is-referenced-through-triple-slash-but-uses-no-references.js new file mode 100644 index 0000000000000..1505191c5bd2b --- /dev/null +++ b/tests/baselines/reference/tsbuild/declarationEmit/initial-build/when-declaration-file-is-referenced-through-triple-slash-but-uses-no-references.js @@ -0,0 +1,175 @@ +Input:: +//// [/lib/lib.d.ts] +/// +interface Boolean {} +interface Function {} +interface CallableFunction {} +interface NewableFunction {} +interface IArguments {} +interface Number { toExponential: any; } +interface Object {} +interface RegExp {} +interface String { charAt: any; } +interface Array { length: number; [n: number]: T; } +interface ReadonlyArray {} +declare const console: { log(msg: any): void; }; + +//// [/src/solution/src/common/nominal.ts] +/// +export declare type Nominal = MyNominal; + +//// [/src/solution/src/common/tsconfig.json] + + +//// [/src/solution/src/common/types.d.ts] +declare type MyNominal = T & { + specialKey: Name; +}; + +//// [/src/solution/src/subProject/index.ts] +import { Nominal } from '../common/nominal'; +export type MyNominal = Nominal; + +//// [/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] +/// +export declare type Nominal = MyNominal; + + +//// [/src/solution/lib/src/common/nominal.js] +"use strict"; +exports.__esModule = true; +/// + + +//// [/src/solution/lib/src/subProject/index.d.ts] +import { Nominal } from '../common/nominal'; +export declare type MyNominal = Nominal; + + +//// [/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; +}; +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-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };", + "signature": "3858781397-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };", + "affectsGlobalScope": true + }, + "../src/common/types.d.ts": { + "version": "23815050294-declare type MyNominal = T & {\n specialKey: Name;\n};", + "signature": "23815050294-declare type MyNominal = T & {\n specialKey: Name;\n};", + "affectsGlobalScope": true + }, + "../src/common/nominal.ts": { + "version": "4107369137-/// \nexport declare type Nominal = MyNominal;", + "signature": "-18894149496-/// \r\nexport declare type Nominal = MyNominal;\r\n", + "affectsGlobalScope": false + }, + "../src/subproject/index.ts": { + "version": "-25117049605-import { Nominal } from '../common/nominal';\nexport type MyNominal = Nominal;", + "signature": "-21416888433-import { Nominal } from '../common/nominal';\r\nexport declare type MyNominal = Nominal;\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;\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" +} + diff --git a/tests/baselines/reference/tsbuild/declarationEmit/initial-build/when-declaration-file-is-referenced-through-triple-slash.js b/tests/baselines/reference/tsbuild/declarationEmit/initial-build/when-declaration-file-is-referenced-through-triple-slash.js new file mode 100644 index 0000000000000..b7d7dd16ac6b4 --- /dev/null +++ b/tests/baselines/reference/tsbuild/declarationEmit/initial-build/when-declaration-file-is-referenced-through-triple-slash.js @@ -0,0 +1,288 @@ +Input:: +//// [/lib/lib.d.ts] +/// +interface Boolean {} +interface Function {} +interface CallableFunction {} +interface NewableFunction {} +interface IArguments {} +interface Number { toExponential: any; } +interface Object {} +interface RegExp {} +interface String { charAt: any; } +interface Array { length: number; [n: number]: T; } +interface ReadonlyArray {} +declare const console: { log(msg: any): void; }; + +//// [/src/solution/src/common/nominal.ts] +/// +export declare type Nominal = MyNominal; + +//// [/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 & { + specialKey: Name; +}; + +//// [/src/solution/src/subProject/index.ts] +import { Nominal } from '../common/nominal'; +export type MyNominal = Nominal; + +//// [/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] +/// +export declare type Nominal = MyNominal; + + +//// [/src/solution/lib/src/common/nominal.js] +"use strict"; +exports.__esModule = true; +/// + + +//// [/src/solution/lib/src/common/tsconfig.tsbuildinfo] +{ + "program": { + "fileInfos": { + "../../../../../lib/lib.d.ts": { + "version": "3858781397-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };", + "signature": "3858781397-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };", + "affectsGlobalScope": true + }, + "../../../src/common/types.d.ts": { + "version": "23815050294-declare type MyNominal = T & {\n specialKey: Name;\n};", + "signature": "23815050294-declare type MyNominal = T & {\n specialKey: Name;\n};", + "affectsGlobalScope": true + }, + "../../../src/common/nominal.ts": { + "version": "4107369137-/// \nexport declare type Nominal = MyNominal;", + "signature": "-18894149496-/// \r\nexport declare type Nominal = MyNominal;\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; + + +//// [/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-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };", + "signature": "3858781397-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };", + "affectsGlobalScope": true + }, + "../../../src/common/types.d.ts": { + "version": "23815050294-declare type MyNominal = T & {\n specialKey: Name;\n};", + "signature": "23815050294-declare type MyNominal = T & {\n specialKey: Name;\n};", + "affectsGlobalScope": true + }, + "../common/nominal.d.ts": { + "version": "-18894149496-/// \r\nexport declare type Nominal = MyNominal;\r\n", + "signature": "-18894149496-/// \r\nexport declare type Nominal = MyNominal;\r\n", + "affectsGlobalScope": false + }, + "../../../src/subproject/index.ts": { + "version": "-25117049605-import { Nominal } from '../common/nominal';\nexport type MyNominal = Nominal;", + "signature": "-21416888433-import { Nominal } from '../common/nominal';\r\nexport declare type MyNominal = Nominal;\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; +}; +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-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };", + "signature": "3858781397-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };", + "affectsGlobalScope": true + }, + "../../../src/common/types.d.ts": { + "version": "23815050294-declare type MyNominal = T & {\n specialKey: Name;\n};", + "signature": "23815050294-declare type MyNominal = T & {\n specialKey: Name;\n};", + "affectsGlobalScope": true + }, + "../common/nominal.d.ts": { + "version": "-18894149496-/// \r\nexport declare type Nominal = MyNominal;\r\n", + "signature": "-18894149496-/// \r\nexport declare type Nominal = MyNominal;\r\n", + "affectsGlobalScope": false + }, + "../subproject/index.d.ts": { + "version": "-21416888433-import { Nominal } from '../common/nominal';\r\nexport declare type MyNominal = Nominal;\r\n", + "signature": "-21416888433-import { Nominal } from '../common/nominal';\r\nexport declare type MyNominal = Nominal;\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;\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" +} +