Skip to content

Commit 7affa9e

Browse files
authored
Assume rootDir is the current configuration directory (#62418)
1 parent 4d94ccb commit 7affa9e

File tree

81 files changed

+2478
-2180
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+2478
-2180
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4480,6 +4480,10 @@
44804480
"category": "Error",
44814481
"code": 5010
44824482
},
4483+
"The common source directory of '{0}' is '{1}'. The 'rootDir' setting must be explicitly set to this or another path to adjust your output's file layout.": {
4484+
"category": "Error",
4485+
"code": 5011
4486+
},
44834487
"Cannot read file '{0}': {1}.": {
44844488
"category": "Error",
44854489
"code": 5012

src/compiler/emitter.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ export function getCommonSourceDirectory(
646646
commonSourceDirectory = getNormalizedAbsolutePath(options.rootDir, currentDirectory);
647647
checkSourceFilesBelongToPath?.(options.rootDir);
648648
}
649-
else if (options.composite && options.configFilePath) {
649+
else if (options.configFilePath) {
650650
// Project compilations never infer their root from the input source paths
651651
commonSourceDirectory = getDirectoryPath(normalizeSlashes(options.configFilePath));
652652
checkSourceFilesBelongToPath?.(commonSourceDirectory);
@@ -664,6 +664,23 @@ export function getCommonSourceDirectory(
664664
return commonSourceDirectory;
665665
}
666666

667+
/** @internal */
668+
export function getComputedCommonSourceDirectory(
669+
emittedFiles: readonly string[],
670+
currentDirectory: string,
671+
getCanonicalFileName: GetCanonicalFileName,
672+
): string {
673+
let commonSourceDirectory = computeCommonSourceDirectoryOfFilenames(emittedFiles, currentDirectory, getCanonicalFileName);
674+
675+
if (commonSourceDirectory && commonSourceDirectory[commonSourceDirectory.length - 1] !== directorySeparator) {
676+
// Make sure directory path ends with directory separator so this string can directly
677+
// used to replace with "" to get the relative path of the source file and the relative path doesn't
678+
// start with / making it rooted path
679+
commonSourceDirectory += directorySeparator;
680+
}
681+
return commonSourceDirectory;
682+
}
683+
667684
/** @internal */
668685
export function getCommonSourceDirectoryOfConfig({ options, fileNames }: ParsedCommandLine, ignoreCase: boolean): string {
669686
return getCommonSourceDirectory(

src/compiler/moduleNameResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2889,7 +2889,7 @@ function getLoadModuleFromTargetExportOrImport(extensions: Extensions, state: Mo
28892889
const commonSourceDirGuesses: string[] = [];
28902890
// A `rootDir` compiler option strongly indicates the root location
28912891
// A `composite` project is using project references and has it's common src dir set to `.`, so it shouldn't need to check any other locations
2892-
if (state.compilerOptions.rootDir || (state.compilerOptions.composite && state.compilerOptions.configFilePath)) {
2892+
if (state.compilerOptions.rootDir || state.compilerOptions.configFilePath) {
28932893
const commonDir = toAbsolutePath(getCommonSourceDirectory(state.compilerOptions, () => [], state.host.getCurrentDirectory?.() || "", getCanonicalFileName));
28942894
commonSourceDirGuesses.push(commonDir);
28952895
}

src/compiler/program.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ import {
109109
GetCanonicalFileName,
110110
getCommonSourceDirectory as ts_getCommonSourceDirectory,
111111
getCommonSourceDirectoryOfConfig,
112+
getComputedCommonSourceDirectory,
112113
getDeclarationDiagnostics as ts_getDeclarationDiagnostics,
113114
getDefaultLibFileName,
114115
getDirectoryPath,
@@ -132,6 +133,7 @@ import {
132133
getPackageScopeForPath,
133134
getPathFromPathComponents,
134135
getPositionOfLineAndCharacter,
136+
getRelativePathFromFile,
135137
getResolvedModuleFromResolution,
136138
getResolvedTypeReferenceDirectiveFromResolution,
137139
getResolveJsonModule,
@@ -4254,6 +4256,35 @@ export function createProgram(_rootNamesOrOptions: readonly string[] | CreatePro
42544256
}
42554257
}
42564258

4259+
if (
4260+
!options.noEmit &&
4261+
!options.composite &&
4262+
!options.rootDir &&
4263+
options.configFilePath &&
4264+
(options.outDir || // there is --outDir specified
4265+
(getEmitDeclarations(options) && options.declarationDir) || // there is --declarationDir specified
4266+
options.outFile)
4267+
) {
4268+
// Check if rootDir inferred changed and issue diagnostic
4269+
const dir = getCommonSourceDirectory();
4270+
const emittedFiles = mapDefined(files, file => !file.isDeclarationFile && sourceFileMayBeEmitted(file, program) ? file.fileName : undefined);
4271+
const dir59 = getComputedCommonSourceDirectory(emittedFiles, currentDirectory, getCanonicalFileName);
4272+
if (dir59 !== "" && getCanonicalFileName(dir) !== getCanonicalFileName(dir59)) {
4273+
// change in layout
4274+
createDiagnosticForOption(
4275+
/*onKey*/ true,
4276+
options.outFile ? "outFile" : options.outDir ? "outDir" : "declarationDir",
4277+
!options.outFile && options.outDir ? "declarationDir" : undefined,
4278+
chainDiagnosticMessages(
4279+
chainDiagnosticMessages(/*details*/ undefined, Diagnostics.Visit_https_Colon_Slash_Slashaka_ms_Slashts6_for_migration_information),
4280+
Diagnostics.The_common_source_directory_of_0_is_1_The_rootDir_setting_must_be_explicitly_set_to_this_or_another_path_to_adjust_your_output_s_file_layout,
4281+
getBaseFileName(options.configFilePath),
4282+
getRelativePathFromFile(options.configFilePath, dir59, getCanonicalFileName),
4283+
),
4284+
);
4285+
}
4286+
}
4287+
42574288
if (options.checkJs && !getAllowJSCompilerOption(options)) {
42584289
createDiagnosticForOptionName(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "checkJs", "allowJs");
42594290
}

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6632,7 +6632,7 @@ export function sourceFileMayBeEmitted(sourceFile: SourceFile, host: SourceFileM
66326632
// Json file is not emitted if outDir is not specified
66336633
if (!options.outDir) return false;
66346634
// Otherwise if rootDir or composite config file, we know common sourceDir and can check if file would be emitted in same location
6635-
if (options.rootDir || (options.composite && options.configFilePath)) {
6635+
if (options.rootDir || options.configFilePath) {
66366636
const commonDir = getNormalizedAbsolutePath(getCommonSourceDirectory(options, () => [], host.getCurrentDirectory(), host.getCanonicalFileName), host.getCurrentDirectory());
66376637
const outputPath = getSourceFilePathInNewDirWorker(sourceFile.fileName, options.outDir, host.getCurrentDirectory(), commonDir, host.getCanonicalFileName);
66386638
if (comparePaths(sourceFile.fileName, outputPath, host.getCurrentDirectory(), !host.useCaseSensitiveFileNames()) === Comparison.EqualTo) return false;

src/testRunner/unittests/helpers/monorepoSymlinkedSiblingPackages.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ function getMonorepoSymlinkedSiblingPackagesSysWithUnRelatedFolders(
151151
compilerOptions: {
152152
outDir: "lib",
153153
declaration: true,
154+
rootDir: "src",
154155
},
155156
include: ["src/**/*.ts"],
156157
}),
@@ -169,6 +170,7 @@ function getMonorepoSymlinkedSiblingPackagesSysWithUnRelatedFolders(
169170
compilerOptions: {
170171
outDir: "lib",
171172
declaration: true,
173+
rootDir: "src",
172174
},
173175
include: ["src/**/*.ts"],
174176
}),
@@ -183,6 +185,7 @@ function getMonorepoSymlinkedSiblingPackagesSysWithUnRelatedFolders(
183185
"/home/src/projects/b/2/b-impl/b/tsconfig.json": jsonToReadableText({
184186
compilerOptions: {
185187
outDir: "lib",
188+
rootDir: "src",
186189
},
187190
include: ["src/**/*.ts"],
188191
}),

src/testRunner/unittests/tsbuild/outputPaths.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe("unittests:: tsbuild:: outputPaths::", () => {
2626
...input,
2727
});
2828

29-
it("verify getOutputFileNames", () => {
29+
it("verify getOutputFileNames " + input.subScenario, () => {
3030
const sys = input.sys();
3131
assert.deepEqual(
3232
ts.getOutputFileNames(
@@ -58,7 +58,7 @@ describe("unittests:: tsbuild:: outputPaths::", () => {
5858
}),
5959
}),
6060
edits,
61-
}, ["/home/src/workspaces/project/dist/index.js"]);
61+
}, ["/home/src/workspaces/project/dist/src/index.js"]);
6262

6363
verify({
6464
subScenario: "when rootDir is not specified and is composite",

tests/baselines/reference/commonSourceDirectory.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,36 @@ foo_1.x + bar_1.y;
2727
//// [/app/bin/index.d.ts]
2828
/// <reference path="../../types/bar.d.ts" preserve="true" />
2929
export {};
30+
31+
32+
//// [DtsFileErrors]
33+
34+
35+
/app/tsconfig.json(3,9): error TS5011: The common source directory of 'tsconfig.json' is '../.src'. The 'rootDir' setting must be explicitly set to this or another path to adjust your output's file layout.
36+
Visit https://aka.ms/ts6 for migration information.
37+
38+
39+
==== /app/tsconfig.json (1 errors) ====
40+
{
41+
"compilerOptions": {
42+
"outDir": "bin",
43+
~~~~~~~~
44+
!!! error TS5011: The common source directory of 'tsconfig.json' is '../.src'. The 'rootDir' setting must be explicitly set to this or another path to adjust your output's file layout.
45+
!!! error TS5011: Visit https://aka.ms/ts6 for migration information.
46+
"typeRoots": ["../types"],
47+
"sourceMap": true,
48+
"mapRoot": "myMapRoot",
49+
"sourceRoot": "mySourceRoot",
50+
"declaration": true
51+
}
52+
}
53+
54+
==== /app/bin/index.d.ts (0 errors) ====
55+
/// <reference path="../../types/bar.d.ts" preserve="true" />
56+
export {};
57+
58+
==== /types/bar.d.ts (0 errors) ====
59+
declare module "bar" {
60+
export const y = 0;
61+
}
62+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/app/tsconfig.json(3,9): error TS5011: The common source directory of 'tsconfig.json' is './src'. The 'rootDir' setting must be explicitly set to this or another path to adjust your output's file layout.
2+
Visit https://aka.ms/ts6 for migration information.
3+
4+
5+
==== /app/tsconfig.json (1 errors) ====
6+
{
7+
"compilerOptions": {
8+
"outDir": "bin",
9+
~~~~~~~~
10+
!!! error TS5011: The common source directory of 'tsconfig.json' is './src'. The 'rootDir' setting must be explicitly set to this or another path to adjust your output's file layout.
11+
!!! error TS5011: Visit https://aka.ms/ts6 for migration information.
12+
"sourceMap": true,
13+
"mapRoot": "myMapRoot",
14+
"sourceRoot": "mySourceRoot",
15+
"declaration": true
16+
}
17+
}
18+
19+
==== /app/lib/bar.d.ts (0 errors) ====
20+
declare const y: number;
21+
22+
==== /app/src/index.ts (0 errors) ====
23+
/// <reference path="../lib/bar.d.ts" preserve="true" />
24+
export const x = y;
25+

tests/baselines/reference/commonSourceDirectory_dts.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ declare const y: number;
88
export const x = y;
99

1010

11-
//// [/app/bin/index.js]
11+
//// [/app/bin/src/index.js]
1212
"use strict";
1313
Object.defineProperty(exports, "__esModule", { value: true });
1414
exports.x = void 0;
1515
/// <reference path="../lib/bar.d.ts" preserve="true" />
1616
exports.x = y;
17-
//# sourceMappingURL=../src/myMapRoot/index.js.map
17+
//# sourceMappingURL=../../myMapRoot/src/index.js.map
1818

19-
//// [/app/bin/index.d.ts]
20-
/// <reference path="../lib/bar.d.ts" preserve="true" />
19+
//// [/app/bin/src/index.d.ts]
20+
/// <reference path="../../lib/bar.d.ts" preserve="true" />
2121
export declare const x: number;

0 commit comments

Comments
 (0)