Skip to content

Commit f0c081f

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

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,
@@ -2056,8 +2056,20 @@ namespace ts {
20562056
Debug.assert((json === undefined && sourceFile !== undefined) || (json !== undefined && sourceFile === undefined));
20572057
const errors: Diagnostic[] = [];
20582058

2059+
function makeFilesAbsolute(optionsFromCLI: CompilerOptions) {
2060+
Object.keys(optionsFromCLI).forEach(key => {
2061+
const optionForKey = getOptionDeclarationFromName(getOptionNameMap, key, /*allowShort*/ true);
2062+
const value = optionsFromCLI[key];
2063+
const relative = isString(value) && !isRootedDiskPath(value);
2064+
if (relative && optionForKey && optionForKey.isFilePath && configFileName) {
2065+
optionsFromCLI[key] = getNormalizedAbsolutePath(value as string, getDirectoryPath(configFileName));
2066+
}
2067+
});
2068+
}
2069+
20592070
const parsedConfig = parseConfig(json, sourceFile, host, basePath, configFileName, resolutionStack, errors, extendedConfigCache);
20602071
const { raw } = parsedConfig;
2072+
makeFilesAbsolute(existingOptions);
20612073
const options = extend(existingOptions, parsedConfig.options || {});
20622074
options.configFilePath = configFileName && normalizeSlashes(configFileName);
20632075
setConfigFileInOptions(options, sourceFile);

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)