Skip to content

Commit c6cc077

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

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/compiler/commandLineParser.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace ts {
1+
 namespace ts {
22
/* @internal */
33
export const compileOnSaveCommandLineOption: CommandLineOption = { name: "compileOnSave", type: "boolean" };
44

@@ -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,
@@ -1049,6 +1049,7 @@ namespace ts {
10491049
errors
10501050
};
10511051

1052+
10521053
function parseStrings(args: ReadonlyArray<string>) {
10531054
let i = 0;
10541055
while (i < args.length) {
@@ -2056,8 +2057,20 @@ namespace ts {
20562057
Debug.assert((json === undefined && sourceFile !== undefined) || (json !== undefined && sourceFile === undefined));
20572058
const errors: Diagnostic[] = [];
20582059

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

src/testRunner/unittests/config/tsconfigParsing.ts

Lines changed: 20 additions & 0 deletions
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)