Skip to content

Commit ccf8a9f

Browse files
committed
Add support for taking relative CLI args and using them as absolute internally - fixes microsoft#30457
1 parent b57b5fe commit ccf8a9f

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/compiler/commandLineParser.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ namespace ts {
675675
name: "out",
676676
type: "string",
677677
affectsEmit: true,
678-
isFilePath: false, // This is intentionally broken to support compatability with existing tsconfig files
678+
isFilePath: false, // This is intentionally broken to support compatibility with existing tsconfig files
679679
// for correct behaviour, please use outFile
680680
category: Diagnostics.Advanced_Options,
681681
paramType: Diagnostics.FILE,
@@ -2058,6 +2058,7 @@ namespace ts {
20582058

20592059
const parsedConfig = parseConfig(json, sourceFile, host, basePath, configFileName, resolutionStack, errors, extendedConfigCache);
20602060
const { raw } = parsedConfig;
2061+
makeFilesReferencesAbsolute(existingOptions);
20612062
const options = extend(existingOptions, parsedConfig.options || {});
20622063
options.configFilePath = configFileName && normalizeSlashes(configFileName);
20632064
setConfigFileInOptions(options, sourceFile);
@@ -2169,6 +2170,17 @@ namespace ts {
21692170
errors.push(createCompilerDiagnostic(message, arg0, arg1));
21702171
}
21712172
}
2173+
2174+
function makeFilesReferencesAbsolute(optionsFromCLI: CompilerOptions) {
2175+
Object.keys(optionsFromCLI).forEach(key => {
2176+
const optionForKey = getOptionDeclarationFromName(getOptionNameMap, key, /*allowShort*/ true);
2177+
const value = optionsFromCLI[key];
2178+
const relative = isString(value) && !isRootedDiskPath(value);
2179+
if (relative && optionForKey && optionForKey.isFilePath && configFileName) {
2180+
optionsFromCLI[key] = getNormalizedAbsolutePath(value as string, getDirectoryPath(configFileName));
2181+
}
2182+
});
2183+
}
21722184
}
21732185

21742186
function isErrorNoInputFiles(error: Diagnostic) {

src/testRunner/unittests/config/tsconfigParsing.ts

+20
Original file line numberDiff line numberDiff line change
@@ -381,5 +381,25 @@ namespace ts {
381381
const parsed = getParsedCommandJsonNode(jsonText, "/apath/tsconfig.json", "tests/cases/unittests", ["/apath/a.ts"]);
382382
assert.isTrue(parsed.errors.length >= 0);
383383
});
384+
385+
it("converts relative paths from the CLI to absolute paths internally based on the tsconfig", () => {
386+
const existingOptions = {
387+
rootDir: "src"
388+
};
389+
390+
const jsonText = `{
391+
"compilerOptions": {
392+
"incremental": true,
393+
"outDir": "dist"
394+
}
395+
}`;
396+
397+
const parsed = parseJsonText("/path/to/config.tsconfig", jsonText);
398+
const files = ["/path/to/src/file.ts"].reduce((files, value) => (files[value] = "", files), {} as vfs.FileSet);
399+
const host: ParseConfigHost = new fakes.ParseConfigHost(new vfs.FileSystem(/*ignoreCase*/ false, { cwd: ".", files: { "/": {}, ...files } }));
400+
const config = parseJsonSourceFileConfigFileContent(parsed, host, ".", existingOptions, "/path/to/config.tsconfig");
401+
402+
assert.equal(config.options.rootDir, "/path/to/src");
403+
});
384404
});
385405
}

0 commit comments

Comments
 (0)