Skip to content

Commit 669c25c

Browse files
authored
Add --ignoreConfig and dont allow specifying files on commandline without it if there is config file present (#62477)
1 parent fbb051f commit 669c25c

30 files changed

+1058
-13
lines changed

src/compiler/commandLineParser.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,15 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
674674
description: Diagnostics.Print_names_of_files_that_are_part_of_the_compilation_and_then_stop_processing,
675675
defaultValueDescription: false,
676676
},
677+
{
678+
name: "ignoreConfig",
679+
type: "boolean",
680+
showInSimplifiedHelpView: true,
681+
category: Diagnostics.Command_line_Options,
682+
isCommandLineOnly: true,
683+
description: Diagnostics.Ignore_the_tsconfig_found_and_build_with_commandline_options_and_files,
684+
defaultValueDescription: false,
685+
},
677686

678687
// Basic
679688
targetOptionDeclaration,

src/compiler/diagnosticMessages.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,10 @@
18571857
"category": "Error",
18581858
"code": 1548
18591859
},
1860+
"Ignore the tsconfig found and build with commandline options and files.": {
1861+
"category": "Message",
1862+
"code": 1549
1863+
},
18601864

18611865
"The types of '{0}' are incompatible between these types.": {
18621866
"category": "Error",
@@ -4733,6 +4737,10 @@
47334737
"category": "Message",
47344738
"code": 5111
47354739
},
4740+
"tsconfig.json is present but will not be loaded if files are specified on commandline. Use '--ignoreConfig' to skip this error.": {
4741+
"category": "Error",
4742+
"code": 5112
4743+
},
47364744

47374745
"Generates a sourcemap for each corresponding '.d.ts' file.": {
47384746
"category": "Message",

src/compiler/executeCommandLine.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -614,20 +614,27 @@ function executeCommandLineWorker(
614614
}
615615
}
616616
}
617-
else if (commandLine.fileNames.length === 0) {
617+
else if (!commandLine.options.ignoreConfig || commandLine.fileNames.length === 0) {
618618
const searchPath = normalizePath(sys.getCurrentDirectory());
619619
configFileName = findConfigFile(searchPath, fileName => sys.fileExists(fileName));
620-
}
621-
622-
if (commandLine.fileNames.length === 0 && !configFileName) {
623-
if (commandLine.options.showConfig) {
624-
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Cannot_find_a_tsconfig_json_file_at_the_current_directory_Colon_0, normalizePath(sys.getCurrentDirectory())));
620+
// if (!commandLine.options.ignoreConfig) {
621+
if (commandLine.fileNames.length !== 0) {
622+
if (configFileName) {
623+
// Error to not specify config file
624+
reportDiagnostic(createCompilerDiagnostic(Diagnostics.tsconfig_json_is_present_but_will_not_be_loaded_if_files_are_specified_on_commandline_Use_ignoreConfig_to_skip_this_error));
625+
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
626+
}
625627
}
626-
else {
627-
printVersion(sys);
628-
printHelp(sys, commandLine);
628+
else if (!configFileName) {
629+
if (commandLine.options.showConfig) {
630+
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Cannot_find_a_tsconfig_json_file_at_the_current_directory_Colon_0, normalizePath(sys.getCurrentDirectory())));
631+
}
632+
else {
633+
printVersion(sys);
634+
printHelp(sys, commandLine);
635+
}
636+
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
629637
}
630-
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
631638
}
632639

633640
const currentDirectory = sys.getCurrentDirectory();

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7552,6 +7552,7 @@ export interface CompilerOptions {
75527552
/** @internal */ watch?: boolean;
75537553
esModuleInterop?: boolean;
75547554
/** @internal */ showConfig?: boolean;
7555+
/** @internal */ ignoreConfig?: boolean;
75557556
useDefineForClassFields?: boolean;
75567557
/** @internal */ tscBuild?: boolean;
75577558

src/testRunner/tests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export * from "./unittests/tsc/composite.js";
119119
export * from "./unittests/tsc/declarationEmit.js";
120120
export * from "./unittests/tsc/extends.js";
121121
export * from "./unittests/tsc/forceConsistentCasingInFileNames.js";
122+
export * from "./unittests/tsc/ignoreConfig.js";
122123
export * from "./unittests/tsc/incremental.js";
123124
export * from "./unittests/tsc/libraryResolution.js";
124125
export * from "./unittests/tsc/listFilesOnly.js";
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import * as ts from "../../_namespaces/ts.js";
2+
import { jsonToReadableText } from "../helpers.js";
3+
import { verifyTsc } from "../helpers/tsc.js";
4+
import { TestServerHost } from "../helpers/virtualFileSystemWithWatch.js";
5+
6+
describe("unittests:: tsc:: ignoreConfig::", () => {
7+
function sysWithoutConfig() {
8+
return TestServerHost.createWatchedSystem({
9+
"/home/src/workspaces/project/src/a.ts": "export const a = 10;",
10+
"/home/src/workspaces/project/src/b.ts": "export const b = 10;",
11+
"/home/src/workspaces/project/c.ts": "export const c = 10;",
12+
});
13+
}
14+
function sysWithConfig() {
15+
const sys = sysWithoutConfig();
16+
sys.writeFile(
17+
"/home/src/workspaces/project/tsconfig.json",
18+
jsonToReadableText({
19+
include: ["src"],
20+
}),
21+
);
22+
return sys;
23+
}
24+
function runScenario(subScenario: string, commandLineArgs: readonly string[]) {
25+
verifyTsc({
26+
scenario: "ignoreConfig",
27+
subScenario,
28+
sys: sysWithConfig,
29+
commandLineArgs,
30+
});
31+
32+
verifyTsc({
33+
scenario: "ignoreConfig",
34+
subScenario: subScenario + " with --ignoreConfig",
35+
sys: sysWithConfig,
36+
commandLineArgs: commandLineArgs.concat("--ignoreConfig"),
37+
});
38+
39+
verifyTsc({
40+
scenario: "ignoreConfig",
41+
subScenario: subScenario + " when config file absent",
42+
sys: sysWithoutConfig,
43+
commandLineArgs,
44+
});
45+
46+
verifyTsc({
47+
scenario: "ignoreConfig",
48+
subScenario: subScenario + " when config file absent with --ignoreConfig",
49+
sys: sysWithoutConfig,
50+
commandLineArgs: commandLineArgs.concat("--ignoreConfig"),
51+
});
52+
}
53+
54+
runScenario("without any options", ts.emptyArray);
55+
runScenario("specifying files", ["src/a.ts"]);
56+
runScenario("specifying project", ["-p", "."]);
57+
runScenario("mixing project and files", ["-p", ".", "src/a.ts", "c.ts"]);
58+
});

src/testRunner/unittests/tscWatch/resolutionCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ declare module "fs" {
424424
],
425425
});
426426
}
427-
verifyIgnore("watch without configFile", ["--w", `/user/username/projects/myproject/test.ts`]);
427+
verifyIgnore("watch without configFile", ["--w", "--ignoreConfig", `/user/username/projects/myproject/test.ts`]);
428428
verifyIgnore("watch with configFile", ["--w", "-p", `/user/username/projects/myproject/tsconfig.json`]);
429429
});
430430

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"ignoreConfig": true
4+
}
5+
}

tests/baselines/reference/tsc/commandLine/does-not-add-color-when-NO_COLOR-is-set.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ Compile the project given the path to its configuration file, or to a folder wit
6767
--showConfig
6868
Print the final configuration instead of building.
6969

70+
--ignoreConfig
71+
Ignore the tsconfig found and build with commandline options and files.
72+
7073
--build, -b
7174
Build one or more projects and their dependencies, if out of date
7275

tests/baselines/reference/tsc/commandLine/help-all.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ Print this message.
3535
--help, -?
3636

3737

38+
--ignoreConfig
39+
Ignore the tsconfig found and build with commandline options and files.
40+
3841
--init
3942
Initializes a TypeScript project and creates a tsconfig.json file.
4043

0 commit comments

Comments
 (0)