From 73852b27d5679dde045a540d78add1586811c3ed Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 6 Jul 2022 13:56:46 -0700 Subject: [PATCH 1/3] Always look for tsconfig, allow command line to override tsconfig --- src/executeCommandLine/executeCommandLine.ts | 10 +-- src/testRunner/tsconfig.json | 1 + src/testRunner/unittests/tsbuild/helpers.ts | 5 +- .../tsc/commandLineOverridesConfig.ts | 83 +++++++++++++++++++ ...n-commandline-override-default-tsconfig.js | 53 ++++++++++++ ...on-commandline-override-passed-tsconfig.js | 53 ++++++++++++ ...n-commandline-override-default-tsconfig.js | 46 ++++++++++ ...on-commandline-override-passed-tsconfig.js | 46 ++++++++++ 8 files changed, 289 insertions(+), 8 deletions(-) create mode 100644 src/testRunner/unittests/tsc/commandLineOverridesConfig.ts create mode 100644 tests/baselines/reference/tsc/commandLineOverridesConfig/compiler-options-passed-on-commandline-override-default-tsconfig.js create mode 100644 tests/baselines/reference/tsc/commandLineOverridesConfig/compiler-options-passed-on-commandline-override-passed-tsconfig.js create mode 100644 tests/baselines/reference/tsc/commandLineOverridesConfig/files-passed-on-commandline-override-default-tsconfig.js create mode 100644 tests/baselines/reference/tsc/commandLineOverridesConfig/files-passed-on-commandline-override-passed-tsconfig.js diff --git a/src/executeCommandLine/executeCommandLine.ts b/src/executeCommandLine/executeCommandLine.ts index 68d2e51e9d4eb..565b8e227243d 100644 --- a/src/executeCommandLine/executeCommandLine.ts +++ b/src/executeCommandLine/executeCommandLine.ts @@ -507,11 +507,6 @@ namespace ts { } if (commandLine.options.project) { - if (commandLine.fileNames.length !== 0) { - reportDiagnostic(createCompilerDiagnostic(Diagnostics.Option_project_cannot_be_mixed_with_source_files_on_a_command_line)); - return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped); - } - const fileOrDirectory = normalizePath(commandLine.options.project); if (!fileOrDirectory /* current directory "." */ || sys.directoryExists(fileOrDirectory)) { configFileName = combinePaths(fileOrDirectory, "tsconfig.json"); @@ -528,7 +523,7 @@ namespace ts { } } } - else if (commandLine.fileNames.length === 0) { + else { const searchPath = normalizePath(sys.getCurrentDirectory()); configFileName = findConfigFile(searchPath, fileName => sys.fileExists(fileName)); } @@ -552,6 +547,9 @@ namespace ts { if (configFileName) { const extendedConfigCache = new Map(); const configParseResult = parseConfigFileWithSystem(configFileName, commandLineOptions, extendedConfigCache, commandLine.watchOptions, sys, reportDiagnostic)!; // TODO: GH#18217 + if (length(commandLine.fileNames)) { + configParseResult.fileNames = commandLine.fileNames; + } if (commandLineOptions.showConfig) { if (configParseResult.errors.length !== 0) { reportDiagnostic = updateReportDiagnostic( diff --git a/src/testRunner/tsconfig.json b/src/testRunner/tsconfig.json index 80cde07e7c751..0709889213227 100644 --- a/src/testRunner/tsconfig.json +++ b/src/testRunner/tsconfig.json @@ -152,6 +152,7 @@ "unittests/tsbuildWatch/reexport.ts", "unittests/tsbuildWatch/watchEnvironment.ts", "unittests/tsc/cancellationToken.ts", + "unittests/tsc/commandLineOverridesConfig.ts", "unittests/tsc/composite.ts", "unittests/tsc/declarationEmit.ts", "unittests/tsc/incremental.ts", diff --git a/src/testRunner/unittests/tsbuild/helpers.ts b/src/testRunner/unittests/tsbuild/helpers.ts index 33e816adfc389..ba89576ba5c5b 100644 --- a/src/testRunner/unittests/tsbuild/helpers.ts +++ b/src/testRunner/unittests/tsbuild/helpers.ts @@ -117,11 +117,12 @@ interface Symbol { */ export function loadProjectFromFiles( files: vfs.FileSet, - libContentToAppend?: string + libContentToAppend?: string, + cwd = "/" ): vfs.FileSystem { const fs = new vfs.FileSystem(/*ignoreCase*/ true, { files, - cwd: "/", + cwd, meta: { defaultLibLocation: "/lib" }, }); addLibAndMakeReadonly(fs, libContentToAppend); diff --git a/src/testRunner/unittests/tsc/commandLineOverridesConfig.ts b/src/testRunner/unittests/tsc/commandLineOverridesConfig.ts new file mode 100644 index 0000000000000..3ca51b056bd5d --- /dev/null +++ b/src/testRunner/unittests/tsc/commandLineOverridesConfig.ts @@ -0,0 +1,83 @@ +namespace ts { + describe("unittests:: tsc:: commandLineOverridesConfig::", () => { + verifyTsc({ + scenario: "commandLineOverridesConfig", + subScenario: "files passed on commandline override default tsconfig", + fs: () => loadProjectFromFiles({ + "/src/project/src/main.ts": "export const x = 10;", + "/src/project/src/file.ts": "export const y: string = undefined;", + "/src/project/tsconfig.json": Utils.dedent` + { + "compilerOptions": { + "strict": true, + }, + "include": [ + "src/**/*.ts" + ] + }`, + }, /*libContent*/ undefined, "/src/project/"), + commandLineArgs: ["src/main.ts"], + environmentVariables: {}, + }); + + verifyTsc({ + scenario: "commandLineOverridesConfig", + subScenario: "compiler options passed on commandline override default tsconfig", + fs: () => loadProjectFromFiles({ + "/src/project/src/main.ts": "export const x = 10;", + "/src/project/src/file.ts": "export const y: string = undefined;", + "/src/project/tsconfig.json": Utils.dedent` + { + "compilerOptions": { + "strict": true, + }, + "include": [ + "src/**/*.ts" + ] + }`, + }, /*libContent*/ undefined, "/src/project/"), + commandLineArgs: ["--strict", "false"], + environmentVariables: {}, + }); + + verifyTsc({ + scenario: "commandLineOverridesConfig", + subScenario: "files passed on commandline override passed tsconfig", + fs: () => loadProjectFromFiles({ + "/src/project/src/main.ts": "export const x = 10;", + "/src/project/src/file.ts": "export const y: string = undefined;", + "/src/project/tsconfig.new.json": Utils.dedent` + { + "compilerOptions": { + "strict": true, + }, + "include": [ + "src/**/*.ts" + ] + }`, + }, /*libContent*/ undefined, "/src/project/"), + commandLineArgs: ["-p", "tsconfig.new.json", "src/main.ts"], + environmentVariables: {}, + }); + + verifyTsc({ + scenario: "commandLineOverridesConfig", + subScenario: "compiler options passed on commandline override passed tsconfig", + fs: () => loadProjectFromFiles({ + "/src/project/src/main.ts": "export const x = 10;", + "/src/project/src/file.ts": "export const y: string = undefined;", + "/src/project/tsconfig.new.json": Utils.dedent` + { + "compilerOptions": { + "strict": true, + }, + "include": [ + "src/**/*.ts" + ] + }`, + }, /*libContent*/ undefined, "/src/project/"), + commandLineArgs: ["-p", "tsconfig.new.json", "--strict", "false"], + environmentVariables: {}, + }); + }); +} diff --git a/tests/baselines/reference/tsc/commandLineOverridesConfig/compiler-options-passed-on-commandline-override-default-tsconfig.js b/tests/baselines/reference/tsc/commandLineOverridesConfig/compiler-options-passed-on-commandline-override-default-tsconfig.js new file mode 100644 index 0000000000000..de5c6f5b6a0f5 --- /dev/null +++ b/tests/baselines/reference/tsc/commandLineOverridesConfig/compiler-options-passed-on-commandline-override-default-tsconfig.js @@ -0,0 +1,53 @@ +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/project/src/file.ts] +export const y: string = undefined; + +//// [/src/project/src/main.ts] +export const x = 10; + +//// [/src/project/tsconfig.json] +{ + "compilerOptions": { + "strict": true, + }, + "include": [ + "src/**/*.ts" + ] +} + + + +Output:: +/lib/tsc --strict false +exitCode:: ExitStatus.Success + + +//// [/src/project/src/file.js] +"use strict"; +exports.__esModule = true; +exports.y = void 0; +exports.y = undefined; + + +//// [/src/project/src/main.js] +"use strict"; +exports.__esModule = true; +exports.x = void 0; +exports.x = 10; + + diff --git a/tests/baselines/reference/tsc/commandLineOverridesConfig/compiler-options-passed-on-commandline-override-passed-tsconfig.js b/tests/baselines/reference/tsc/commandLineOverridesConfig/compiler-options-passed-on-commandline-override-passed-tsconfig.js new file mode 100644 index 0000000000000..470c843d25019 --- /dev/null +++ b/tests/baselines/reference/tsc/commandLineOverridesConfig/compiler-options-passed-on-commandline-override-passed-tsconfig.js @@ -0,0 +1,53 @@ +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/project/src/file.ts] +export const y: string = undefined; + +//// [/src/project/src/main.ts] +export const x = 10; + +//// [/src/project/tsconfig.new.json] +{ + "compilerOptions": { + "strict": true, + }, + "include": [ + "src/**/*.ts" + ] +} + + + +Output:: +/lib/tsc -p tsconfig.new.json --strict false +exitCode:: ExitStatus.Success + + +//// [/src/project/src/file.js] +"use strict"; +exports.__esModule = true; +exports.y = void 0; +exports.y = undefined; + + +//// [/src/project/src/main.js] +"use strict"; +exports.__esModule = true; +exports.x = void 0; +exports.x = 10; + + diff --git a/tests/baselines/reference/tsc/commandLineOverridesConfig/files-passed-on-commandline-override-default-tsconfig.js b/tests/baselines/reference/tsc/commandLineOverridesConfig/files-passed-on-commandline-override-default-tsconfig.js new file mode 100644 index 0000000000000..e4dd24a43c355 --- /dev/null +++ b/tests/baselines/reference/tsc/commandLineOverridesConfig/files-passed-on-commandline-override-default-tsconfig.js @@ -0,0 +1,46 @@ +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/project/src/file.ts] +export const y: string = undefined; + +//// [/src/project/src/main.ts] +export const x = 10; + +//// [/src/project/tsconfig.json] +{ + "compilerOptions": { + "strict": true, + }, + "include": [ + "src/**/*.ts" + ] +} + + + +Output:: +/lib/tsc src/main.ts +exitCode:: ExitStatus.Success + + +//// [/src/project/src/main.js] +"use strict"; +exports.__esModule = true; +exports.x = void 0; +exports.x = 10; + + diff --git a/tests/baselines/reference/tsc/commandLineOverridesConfig/files-passed-on-commandline-override-passed-tsconfig.js b/tests/baselines/reference/tsc/commandLineOverridesConfig/files-passed-on-commandline-override-passed-tsconfig.js new file mode 100644 index 0000000000000..021e20e488031 --- /dev/null +++ b/tests/baselines/reference/tsc/commandLineOverridesConfig/files-passed-on-commandline-override-passed-tsconfig.js @@ -0,0 +1,46 @@ +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/project/src/file.ts] +export const y: string = undefined; + +//// [/src/project/src/main.ts] +export const x = 10; + +//// [/src/project/tsconfig.new.json] +{ + "compilerOptions": { + "strict": true, + }, + "include": [ + "src/**/*.ts" + ] +} + + + +Output:: +/lib/tsc -p tsconfig.new.json src/main.ts +exitCode:: ExitStatus.Success + + +//// [/src/project/src/main.js] +"use strict"; +exports.__esModule = true; +exports.x = void 0; +exports.x = 10; + + From 8ca75135eb7a91d72d1be1b8304eeaa6dbb2d583 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 8 Jul 2022 11:59:28 -0700 Subject: [PATCH 2/3] Support -p null --- src/executeCommandLine/executeCommandLine.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/executeCommandLine/executeCommandLine.ts b/src/executeCommandLine/executeCommandLine.ts index 565b8e227243d..81445decd53e1 100644 --- a/src/executeCommandLine/executeCommandLine.ts +++ b/src/executeCommandLine/executeCommandLine.ts @@ -523,7 +523,7 @@ namespace ts { } } } - else { + else if (!hasProperty(commandLine.options, "project")) { const searchPath = normalizePath(sys.getCurrentDirectory()); configFileName = findConfigFile(searchPath, fileName => sys.fileExists(fileName)); } From 5e6aed5a059c1748a961092231d8b3b3d41be583 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 8 Jul 2022 12:10:17 -0700 Subject: [PATCH 3/3] Add p null test --- .../tsc/commandLineOverridesConfig.ts | 20 ++++++++ .../-p-null-disables-config.js | 46 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/baselines/reference/tsc/commandLineOverridesConfig/-p-null-disables-config.js diff --git a/src/testRunner/unittests/tsc/commandLineOverridesConfig.ts b/src/testRunner/unittests/tsc/commandLineOverridesConfig.ts index 3ca51b056bd5d..c80afd735fbfa 100644 --- a/src/testRunner/unittests/tsc/commandLineOverridesConfig.ts +++ b/src/testRunner/unittests/tsc/commandLineOverridesConfig.ts @@ -79,5 +79,25 @@ namespace ts { commandLineArgs: ["-p", "tsconfig.new.json", "--strict", "false"], environmentVariables: {}, }); + + verifyTsc({ + scenario: "commandLineOverridesConfig", + subScenario: "-p null disables config", + fs: () => loadProjectFromFiles({ + "/src/project/src/main.ts": "export const x = 10;", + "/src/project/src/file.ts": "export const y: string = undefined;", + "/src/project/tsconfig.json": Utils.dedent` + { + "compilerOptions": { + "strict": true, + }, + "include": [ + "src/**/*.ts" + ] + }`, + }, /*libContent*/ undefined, "/src/project/"), + commandLineArgs: ["-p", "null", "src/file.ts"], + environmentVariables: {}, + }); }); } diff --git a/tests/baselines/reference/tsc/commandLineOverridesConfig/-p-null-disables-config.js b/tests/baselines/reference/tsc/commandLineOverridesConfig/-p-null-disables-config.js new file mode 100644 index 0000000000000..9d38137431c78 --- /dev/null +++ b/tests/baselines/reference/tsc/commandLineOverridesConfig/-p-null-disables-config.js @@ -0,0 +1,46 @@ +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/project/src/file.ts] +export const y: string = undefined; + +//// [/src/project/src/main.ts] +export const x = 10; + +//// [/src/project/tsconfig.json] +{ + "compilerOptions": { + "strict": true, + }, + "include": [ + "src/**/*.ts" + ] +} + + + +Output:: +/lib/tsc -p null src/file.ts +exitCode:: ExitStatus.Success + + +//// [/src/project/src/file.js] +"use strict"; +exports.__esModule = true; +exports.y = void 0; +exports.y = undefined; + +