From 602aec2f7d9cd539f71c6c81f42985d8916a1422 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 2 Apr 2019 14:37:00 -0700 Subject: [PATCH] Never create redirect for sourceFiles that get emitted to single output file Fixes #30591 --- src/compiler/program.ts | 23 +- .../unittests/tsbuild/amdModulesWithOut.ts | 9 +- ...e-resolution-finds-original-source-file.js | 573 ++++++++++++++++++ 3 files changed, 598 insertions(+), 7 deletions(-) create mode 100644 tests/baselines/reference/tsbuild/amdModulesWithOut/initial-Build/when-the-module-resolution-finds-original-source-file.js diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 6418f55968b..ad3121860ad 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -2266,8 +2266,13 @@ namespace ts { let redirectedPath: Path | undefined; if (refFile) { - const redirect = getProjectReferenceRedirect(fileName); - if (redirect) { + const redirectProject = getProjectReferenceRedirectProject(fileName); + if (redirectProject) { + if (redirectProject.commandLine.options.outFile || redirectProject.commandLine.options.out) { + // Shouldnt create many to 1 mapping file in --out scenario + return undefined; + } + const redirect = getProjectReferenceOutputName(redirectProject, fileName); fileName = redirect; // Once we start redirecting to a file, we can potentially come back to it // via a back-reference from another file in the .d.ts folder. If that happens we'll @@ -2364,6 +2369,11 @@ namespace ts { } function getProjectReferenceRedirect(fileName: string): string | undefined { + const referencedProject = getProjectReferenceRedirectProject(fileName); + return referencedProject && getProjectReferenceOutputName(referencedProject, fileName); + } + + function getProjectReferenceRedirectProject(fileName: string) { // Ignore dts or any of the non ts files if (!resolvedProjectReferences || !resolvedProjectReferences.length || fileExtensionIs(fileName, Extension.Dts) || !fileExtensionIsOneOf(fileName, supportedTSExtensions)) { return undefined; @@ -2371,10 +2381,11 @@ namespace ts { // If this file is produced by a referenced project, we need to rewrite it to // look in the output folder of the referenced project rather than the input - const referencedProject = getResolvedProjectReferenceToRedirect(fileName); - if (!referencedProject) { - return undefined; - } + return getResolvedProjectReferenceToRedirect(fileName); + } + + + function getProjectReferenceOutputName(referencedProject: ResolvedProjectReference, fileName: string) { const out = referencedProject.commandLine.options.outFile || referencedProject.commandLine.options.out; return out ? changeExtension(out, Extension.Dts) : diff --git a/src/testRunner/unittests/tsbuild/amdModulesWithOut.ts b/src/testRunner/unittests/tsbuild/amdModulesWithOut.ts index 70f3fe187c8..3e1cab9b4a0 100644 --- a/src/testRunner/unittests/tsbuild/amdModulesWithOut.ts +++ b/src/testRunner/unittests/tsbuild/amdModulesWithOut.ts @@ -233,7 +233,14 @@ ${internal} export enum internalEnum { a, b, c }`); ], lastProjectOutputJs: outputFiles[project.app][ext.js], initialBuild: { - modifyFs + modifyFs, + expectedDiagnostics: [ + getExpectedDiagnosticForProjectsInBuild("src/lib/tsconfig.json", "src/app/tsconfig.json"), + [Diagnostics.Project_0_is_out_of_date_because_output_file_1_does_not_exist, "src/lib/tsconfig.json", "src/module.js"], + [Diagnostics.Building_project_0, sources[project.lib][source.config]], + [Diagnostics.Project_0_is_out_of_date_because_output_file_1_does_not_exist, "src/app/tsconfig.json", "src/app/module.js"], + [Diagnostics.Building_project_0, sources[project.app][source.config]], + ] }, outputFiles: [ ...libOutputFile, diff --git a/tests/baselines/reference/tsbuild/amdModulesWithOut/initial-Build/when-the-module-resolution-finds-original-source-file.js b/tests/baselines/reference/tsbuild/amdModulesWithOut/initial-Build/when-the-module-resolution-finds-original-source-file.js new file mode 100644 index 00000000000..119eb04f23a --- /dev/null +++ b/tests/baselines/reference/tsbuild/amdModulesWithOut/initial-Build/when-the-module-resolution-finds-original-source-file.js @@ -0,0 +1,573 @@ +//// [/src/app/file3.ts] +export const z = 30; +import { x } from "lib/file1"; + +//// [/src/app/module.d.ts] +declare const myGlob = 20; +declare module "lib/file1" { + export const x = 10; +} +declare module "lib/file2" { + export const y = 20; +} +declare const globalConst = 10; +declare module "file3" { + export const z = 30; +} +declare const myVar = 30; +//# sourceMappingURL=module.d.ts.map + +//// [/src/app/module.d.ts.map] +{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../lib/file0.ts","../lib/file1.ts","../lib/file2.ts","../lib/global.ts","file3.ts","file4.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,MAAM,KAAK,CAAC;;ICAlB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC;;;ICApB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC;;ACApB,QAAA,MAAM,WAAW,KAAK,CAAC;;ICAvB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC;;ACApB,QAAA,MAAM,KAAK,KAAK,CAAC"} + +//// [/src/app/module.d.ts.map.baseline.txt] +=================================================================== +JsFile: module.d.ts +mapUrl: module.d.ts.map +sourceRoot: +sources: ../lib/file0.ts,../lib/file1.ts,../lib/file2.ts,../lib/global.ts,file3.ts,file4.ts +=================================================================== +------------------------------------------------------------------- +emittedFile:/src/app/module.d.ts +sourceFile:../lib/file0.ts +------------------------------------------------------------------- +>>>declare const myGlob = 20; +1 > +2 >^^^^^^^^ +3 > ^^^^^^ +4 > ^^^^^^ +5 > ^^^^^ +6 > ^ +7 > ^^^-> +1 > +2 > +3 > const +4 > myGlob +5 > = 20 +6 > ; +1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0) +2 >Emitted(1, 9) Source(1, 1) + SourceIndex(0) +3 >Emitted(1, 15) Source(1, 7) + SourceIndex(0) +4 >Emitted(1, 21) Source(1, 13) + SourceIndex(0) +5 >Emitted(1, 26) Source(1, 18) + SourceIndex(0) +6 >Emitted(1, 27) Source(1, 19) + SourceIndex(0) +--- +------------------------------------------------------------------- +emittedFile:/src/app/module.d.ts +sourceFile:../lib/file1.ts +------------------------------------------------------------------- +>>>declare module "lib/file1" { +>>> export const x = 10; +1->^^^^ +2 > ^^^^^^ +3 > ^ +4 > ^^^^^^ +5 > ^ +6 > ^^^^^ +7 > ^ +1-> +2 > export +3 > +4 > const +5 > x +6 > = 10 +7 > ; +1->Emitted(3, 5) Source(1, 1) + SourceIndex(1) +2 >Emitted(3, 11) Source(1, 7) + SourceIndex(1) +3 >Emitted(3, 12) Source(1, 8) + SourceIndex(1) +4 >Emitted(3, 18) Source(1, 14) + SourceIndex(1) +5 >Emitted(3, 19) Source(1, 15) + SourceIndex(1) +6 >Emitted(3, 24) Source(1, 20) + SourceIndex(1) +7 >Emitted(3, 25) Source(1, 21) + SourceIndex(1) +--- +------------------------------------------------------------------- +emittedFile:/src/app/module.d.ts +sourceFile:../lib/file2.ts +------------------------------------------------------------------- +>>>} +>>>declare module "lib/file2" { +>>> export const y = 20; +1 >^^^^ +2 > ^^^^^^ +3 > ^ +4 > ^^^^^^ +5 > ^ +6 > ^^^^^ +7 > ^ +1 > +2 > export +3 > +4 > const +5 > y +6 > = 20 +7 > ; +1 >Emitted(6, 5) Source(1, 1) + SourceIndex(2) +2 >Emitted(6, 11) Source(1, 7) + SourceIndex(2) +3 >Emitted(6, 12) Source(1, 8) + SourceIndex(2) +4 >Emitted(6, 18) Source(1, 14) + SourceIndex(2) +5 >Emitted(6, 19) Source(1, 15) + SourceIndex(2) +6 >Emitted(6, 24) Source(1, 20) + SourceIndex(2) +7 >Emitted(6, 25) Source(1, 21) + SourceIndex(2) +--- +------------------------------------------------------------------- +emittedFile:/src/app/module.d.ts +sourceFile:../lib/global.ts +------------------------------------------------------------------- +>>>} +>>>declare const globalConst = 10; +1 > +2 >^^^^^^^^ +3 > ^^^^^^ +4 > ^^^^^^^^^^^ +5 > ^^^^^ +6 > ^ +1 > +2 > +3 > const +4 > globalConst +5 > = 10 +6 > ; +1 >Emitted(8, 1) Source(1, 1) + SourceIndex(3) +2 >Emitted(8, 9) Source(1, 1) + SourceIndex(3) +3 >Emitted(8, 15) Source(1, 7) + SourceIndex(3) +4 >Emitted(8, 26) Source(1, 18) + SourceIndex(3) +5 >Emitted(8, 31) Source(1, 23) + SourceIndex(3) +6 >Emitted(8, 32) Source(1, 24) + SourceIndex(3) +--- +------------------------------------------------------------------- +emittedFile:/src/app/module.d.ts +sourceFile:file3.ts +------------------------------------------------------------------- +>>>declare module "file3" { +>>> export const z = 30; +1 >^^^^ +2 > ^^^^^^ +3 > ^ +4 > ^^^^^^ +5 > ^ +6 > ^^^^^ +7 > ^ +1 > +2 > export +3 > +4 > const +5 > z +6 > = 30 +7 > ; +1 >Emitted(10, 5) Source(1, 1) + SourceIndex(4) +2 >Emitted(10, 11) Source(1, 7) + SourceIndex(4) +3 >Emitted(10, 12) Source(1, 8) + SourceIndex(4) +4 >Emitted(10, 18) Source(1, 14) + SourceIndex(4) +5 >Emitted(10, 19) Source(1, 15) + SourceIndex(4) +6 >Emitted(10, 24) Source(1, 20) + SourceIndex(4) +7 >Emitted(10, 25) Source(1, 21) + SourceIndex(4) +--- +------------------------------------------------------------------- +emittedFile:/src/app/module.d.ts +sourceFile:file4.ts +------------------------------------------------------------------- +>>>} +>>>declare const myVar = 30; +1 > +2 >^^^^^^^^ +3 > ^^^^^^ +4 > ^^^^^ +5 > ^^^^^ +6 > ^ +7 > ^^^^^^^^^^-> +1 > +2 > +3 > const +4 > myVar +5 > = 30 +6 > ; +1 >Emitted(12, 1) Source(1, 1) + SourceIndex(5) +2 >Emitted(12, 9) Source(1, 1) + SourceIndex(5) +3 >Emitted(12, 15) Source(1, 7) + SourceIndex(5) +4 >Emitted(12, 20) Source(1, 12) + SourceIndex(5) +5 >Emitted(12, 25) Source(1, 17) + SourceIndex(5) +6 >Emitted(12, 26) Source(1, 18) + SourceIndex(5) +--- +>>>//# sourceMappingURL=module.d.ts.map + +//// [/src/app/module.js] +var myGlob = 20; +define("lib/file1", ["require", "exports"], function (require, exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.x = 10; +}); +define("lib/file2", ["require", "exports"], function (require, exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.y = 20; +}); +var globalConst = 10; +define("file3", ["require", "exports"], function (require, exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.z = 30; +}); +var myVar = 30; +//# sourceMappingURL=module.js.map + +//// [/src/app/module.js.map] +{"version":3,"file":"module.js","sourceRoot":"","sources":["../lib/file0.ts","../lib/file1.ts","../lib/file2.ts","../lib/global.ts","file3.ts","file4.ts"],"names":[],"mappings":"AAAA,IAAM,MAAM,GAAG,EAAE,CAAC;;;;ICAL,QAAA,CAAC,GAAG,EAAE,CAAC;;;;;ICAP,QAAA,CAAC,GAAG,EAAE,CAAC;;ACApB,IAAM,WAAW,GAAG,EAAE,CAAC;;;;ICAV,QAAA,CAAC,GAAG,EAAE,CAAC;;ACApB,IAAM,KAAK,GAAG,EAAE,CAAC"} + +//// [/src/app/module.js.map.baseline.txt] +=================================================================== +JsFile: module.js +mapUrl: module.js.map +sourceRoot: +sources: ../lib/file0.ts,../lib/file1.ts,../lib/file2.ts,../lib/global.ts,file3.ts,file4.ts +=================================================================== +------------------------------------------------------------------- +emittedFile:/src/app/module.js +sourceFile:../lib/file0.ts +------------------------------------------------------------------- +>>>var myGlob = 20; +1 > +2 >^^^^ +3 > ^^^^^^ +4 > ^^^ +5 > ^^ +6 > ^ +7 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-> +1 > +2 >const +3 > myGlob +4 > = +5 > 20 +6 > ; +1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0) +2 >Emitted(1, 5) Source(1, 7) + SourceIndex(0) +3 >Emitted(1, 11) Source(1, 13) + SourceIndex(0) +4 >Emitted(1, 14) Source(1, 16) + SourceIndex(0) +5 >Emitted(1, 16) Source(1, 18) + SourceIndex(0) +6 >Emitted(1, 17) Source(1, 19) + SourceIndex(0) +--- +------------------------------------------------------------------- +emittedFile:/src/app/module.js +sourceFile:../lib/file1.ts +------------------------------------------------------------------- +>>>define("lib/file1", ["require", "exports"], function (require, exports) { +>>> "use strict"; +>>> Object.defineProperty(exports, "__esModule", { value: true }); +>>> exports.x = 10; +1->^^^^ +2 > ^^^^^^^^ +3 > ^ +4 > ^^^ +5 > ^^ +6 > ^ +1->export const +2 > +3 > x +4 > = +5 > 10 +6 > ; +1->Emitted(5, 5) Source(1, 14) + SourceIndex(1) +2 >Emitted(5, 13) Source(1, 14) + SourceIndex(1) +3 >Emitted(5, 14) Source(1, 15) + SourceIndex(1) +4 >Emitted(5, 17) Source(1, 18) + SourceIndex(1) +5 >Emitted(5, 19) Source(1, 20) + SourceIndex(1) +6 >Emitted(5, 20) Source(1, 21) + SourceIndex(1) +--- +------------------------------------------------------------------- +emittedFile:/src/app/module.js +sourceFile:../lib/file2.ts +------------------------------------------------------------------- +>>>}); +>>>define("lib/file2", ["require", "exports"], function (require, exports) { +>>> "use strict"; +>>> Object.defineProperty(exports, "__esModule", { value: true }); +>>> exports.y = 20; +1 >^^^^ +2 > ^^^^^^^^ +3 > ^ +4 > ^^^ +5 > ^^ +6 > ^ +1 >export const +2 > +3 > y +4 > = +5 > 20 +6 > ; +1 >Emitted(10, 5) Source(1, 14) + SourceIndex(2) +2 >Emitted(10, 13) Source(1, 14) + SourceIndex(2) +3 >Emitted(10, 14) Source(1, 15) + SourceIndex(2) +4 >Emitted(10, 17) Source(1, 18) + SourceIndex(2) +5 >Emitted(10, 19) Source(1, 20) + SourceIndex(2) +6 >Emitted(10, 20) Source(1, 21) + SourceIndex(2) +--- +------------------------------------------------------------------- +emittedFile:/src/app/module.js +sourceFile:../lib/global.ts +------------------------------------------------------------------- +>>>}); +>>>var globalConst = 10; +1 > +2 >^^^^ +3 > ^^^^^^^^^^^ +4 > ^^^ +5 > ^^ +6 > ^ +7 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-> +1 > +2 >const +3 > globalConst +4 > = +5 > 10 +6 > ; +1 >Emitted(12, 1) Source(1, 1) + SourceIndex(3) +2 >Emitted(12, 5) Source(1, 7) + SourceIndex(3) +3 >Emitted(12, 16) Source(1, 18) + SourceIndex(3) +4 >Emitted(12, 19) Source(1, 21) + SourceIndex(3) +5 >Emitted(12, 21) Source(1, 23) + SourceIndex(3) +6 >Emitted(12, 22) Source(1, 24) + SourceIndex(3) +--- +------------------------------------------------------------------- +emittedFile:/src/app/module.js +sourceFile:file3.ts +------------------------------------------------------------------- +>>>define("file3", ["require", "exports"], function (require, exports) { +>>> "use strict"; +>>> Object.defineProperty(exports, "__esModule", { value: true }); +>>> exports.z = 30; +1->^^^^ +2 > ^^^^^^^^ +3 > ^ +4 > ^^^ +5 > ^^ +6 > ^ +1->export const +2 > +3 > z +4 > = +5 > 30 +6 > ; +1->Emitted(16, 5) Source(1, 14) + SourceIndex(4) +2 >Emitted(16, 13) Source(1, 14) + SourceIndex(4) +3 >Emitted(16, 14) Source(1, 15) + SourceIndex(4) +4 >Emitted(16, 17) Source(1, 18) + SourceIndex(4) +5 >Emitted(16, 19) Source(1, 20) + SourceIndex(4) +6 >Emitted(16, 20) Source(1, 21) + SourceIndex(4) +--- +------------------------------------------------------------------- +emittedFile:/src/app/module.js +sourceFile:file4.ts +------------------------------------------------------------------- +>>>}); +>>>var myVar = 30; +1 > +2 >^^^^ +3 > ^^^^^ +4 > ^^^ +5 > ^^ +6 > ^ +7 > ^^^^^^^^^^^^^^^^^^-> +1 > +2 >const +3 > myVar +4 > = +5 > 30 +6 > ; +1 >Emitted(18, 1) Source(1, 1) + SourceIndex(5) +2 >Emitted(18, 5) Source(1, 7) + SourceIndex(5) +3 >Emitted(18, 10) Source(1, 12) + SourceIndex(5) +4 >Emitted(18, 13) Source(1, 15) + SourceIndex(5) +5 >Emitted(18, 15) Source(1, 17) + SourceIndex(5) +6 >Emitted(18, 16) Source(1, 18) + SourceIndex(5) +--- +>>>//# sourceMappingURL=module.js.map + +//// [/src/app/module.tsbuildinfo] +{ + "bundle": { + "commonSourceDirectory": "/src/app/", + "sourceFiles": [ + "/src/app/file3.ts", + "/src/app/file4.ts" + ], + "js": { + "sections": [ + { + "pos": 0, + "end": 417, + "kind": "prepend", + "data": "/src/module.js", + "texts": [ + { + "pos": 0, + "end": 417, + "kind": "text" + } + ] + }, + { + "pos": 417, + "end": 618, + "kind": "text" + } + ] + }, + "dts": { + "sections": [ + { + "pos": 0, + "end": 179, + "kind": "prepend", + "data": "/src/module.d.ts", + "texts": [ + { + "pos": 0, + "end": 179, + "kind": "text" + } + ] + }, + { + "pos": 179, + "end": 261, + "kind": "text" + } + ] + } + }, + "version": "FakeTSVersion" +} + +//// [/src/app/module.tsbuildinfo.baseline.txt] +====================================================================== +File:: /src/app/module.js +---------------------------------------------------------------------- +prepend: (0-417):: /src/module.js texts:: 1 +>>-------------------------------------------------------------------- +text: (0-417) +var myGlob = 20; +define("lib/file1", ["require", "exports"], function (require, exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.x = 10; +}); +define("lib/file2", ["require", "exports"], function (require, exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.y = 20; +}); +var globalConst = 10; + +---------------------------------------------------------------------- +text: (417-618) +define("file3", ["require", "exports"], function (require, exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.z = 30; +}); +var myVar = 30; + +====================================================================== +====================================================================== +File:: /src/app/module.d.ts +---------------------------------------------------------------------- +prepend: (0-179):: /src/module.d.ts texts:: 1 +>>-------------------------------------------------------------------- +text: (0-179) +declare const myGlob = 20; +declare module "lib/file1" { + export const x = 10; +} +declare module "lib/file2" { + export const y = 20; +} +declare const globalConst = 10; + +---------------------------------------------------------------------- +text: (179-261) +declare module "file3" { + export const z = 30; +} +declare const myVar = 30; + +====================================================================== + +//// [/src/lib/tsconfig.json] +{ + "compilerOptions": { + "target": "es5", + "module": "amd", + "composite": true, + "sourceMap": true, + "declarationMap": true, + "strict": false, + "outFile": "../module.js", "rootDir": "../" + }, + "exclude": ["module.d.ts"] + +} + +//// [/src/module.d.ts] +declare const myGlob = 20; +declare module "lib/file1" { + export const x = 10; +} +declare module "lib/file2" { + export const y = 20; +} +declare const globalConst = 10; +//# sourceMappingURL=module.d.ts.map + +//// [/src/module.d.ts.map] +{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["lib/file0.ts","lib/file1.ts","lib/file2.ts","lib/global.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,MAAM,KAAK,CAAC;;ICAlB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC;;;ICApB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC;;ACApB,QAAA,MAAM,WAAW,KAAK,CAAC"} + +//// [/src/module.js] +var myGlob = 20; +define("lib/file1", ["require", "exports"], function (require, exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.x = 10; +}); +define("lib/file2", ["require", "exports"], function (require, exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.y = 20; +}); +var globalConst = 10; +//# sourceMappingURL=module.js.map + +//// [/src/module.js.map] +{"version":3,"file":"module.js","sourceRoot":"","sources":["lib/file0.ts","lib/file1.ts","lib/file2.ts","lib/global.ts"],"names":[],"mappings":"AAAA,IAAM,MAAM,GAAG,EAAE,CAAC;;;;ICAL,QAAA,CAAC,GAAG,EAAE,CAAC;;;;;ICAP,QAAA,CAAC,GAAG,EAAE,CAAC;;ACApB,IAAM,WAAW,GAAG,EAAE,CAAC"} + +//// [/src/module.tsbuildinfo] +{ + "bundle": { + "commonSourceDirectory": "/src/", + "sourceFiles": [ + "/src/lib/file0.ts", + "/src/lib/file1.ts", + "/src/lib/file2.ts", + "/src/lib/global.ts" + ], + "js": { + "sections": [ + { + "pos": 0, + "end": 417, + "kind": "text" + } + ] + }, + "dts": { + "sections": [ + { + "pos": 0, + "end": 179, + "kind": "text" + } + ] + } + }, + "version": "FakeTSVersion" +} +