Skip to content

Commit

Permalink
Add support for taking relative CLI args and using them as absolute i…
Browse files Browse the repository at this point in the history
…nternally - fixes microsoft#30457
  • Loading branch information
orta committed Aug 20, 2019
1 parent b57b5fe commit ccf8a9f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions src/testRunner/unittests/config/tsconfigParsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
}

0 comments on commit ccf8a9f

Please sign in to comment.