From ccf8a9f0cd2ef93bcd5ae3cc4c6f4f41f551e1a8 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Tue, 20 Aug 2019 13:25:03 -0400 Subject: [PATCH] Add support for taking relative CLI args and using them as absolute internally - fixes #30457 --- src/compiler/commandLineParser.ts | 14 ++++++++++++- .../unittests/config/tsconfigParsing.ts | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 9ba03842cecfc..97e9007d044f3 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -675,7 +675,7 @@ namespace ts { name: "out", type: "string", affectsEmit: true, - isFilePath: false, // This is intentionally broken to support compatability with existing tsconfig files + isFilePath: false, // This is intentionally broken to support compatibility with existing tsconfig files // for correct behaviour, please use outFile category: Diagnostics.Advanced_Options, paramType: Diagnostics.FILE, @@ -2058,6 +2058,7 @@ namespace ts { const parsedConfig = parseConfig(json, sourceFile, host, basePath, configFileName, resolutionStack, errors, extendedConfigCache); const { raw } = parsedConfig; + makeFilesReferencesAbsolute(existingOptions); const options = extend(existingOptions, parsedConfig.options || {}); options.configFilePath = configFileName && normalizeSlashes(configFileName); setConfigFileInOptions(options, sourceFile); @@ -2169,6 +2170,17 @@ namespace ts { errors.push(createCompilerDiagnostic(message, arg0, arg1)); } } + + function makeFilesReferencesAbsolute(optionsFromCLI: CompilerOptions) { + Object.keys(optionsFromCLI).forEach(key => { + const optionForKey = getOptionDeclarationFromName(getOptionNameMap, key, /*allowShort*/ true); + const value = optionsFromCLI[key]; + const relative = isString(value) && !isRootedDiskPath(value); + if (relative && optionForKey && optionForKey.isFilePath && configFileName) { + optionsFromCLI[key] = getNormalizedAbsolutePath(value as string, getDirectoryPath(configFileName)); + } + }); + } } function isErrorNoInputFiles(error: Diagnostic) { diff --git a/src/testRunner/unittests/config/tsconfigParsing.ts b/src/testRunner/unittests/config/tsconfigParsing.ts index 638a394a34e41..754136cc9e4a7 100644 --- a/src/testRunner/unittests/config/tsconfigParsing.ts +++ b/src/testRunner/unittests/config/tsconfigParsing.ts @@ -381,5 +381,25 @@ namespace ts { const parsed = getParsedCommandJsonNode(jsonText, "/apath/tsconfig.json", "tests/cases/unittests", ["/apath/a.ts"]); assert.isTrue(parsed.errors.length >= 0); }); + + it("converts relative paths from the CLI to absolute paths internally based on the tsconfig", () => { + const existingOptions = { + rootDir: "src" + }; + + const jsonText = `{ + "compilerOptions": { + "incremental": true, + "outDir": "dist" + } + }`; + + const parsed = parseJsonText("/path/to/config.tsconfig", jsonText); + const files = ["/path/to/src/file.ts"].reduce((files, value) => (files[value] = "", files), {} as vfs.FileSet); + const host: ParseConfigHost = new fakes.ParseConfigHost(new vfs.FileSystem(/*ignoreCase*/ false, { cwd: ".", files: { "/": {}, ...files } })); + const config = parseJsonSourceFileConfigFileContent(parsed, host, ".", existingOptions, "/path/to/config.tsconfig"); + + assert.equal(config.options.rootDir, "/path/to/src"); + }); }); }