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 c6cc077
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ts {
 namespace ts {
/* @internal */
export const compileOnSaveCommandLineOption: CommandLineOption = { name: "compileOnSave", type: "boolean" };

Expand Down 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 @@ -1049,6 +1049,7 @@ namespace ts {
errors
};


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

function makeFilesAbsolute(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));
}
});
}

const parsedConfig = parseConfig(json, sourceFile, host, basePath, configFileName, resolutionStack, errors, extendedConfigCache);
const { raw } = parsedConfig;
makeFilesAbsolute(existingOptions);
const options = extend(existingOptions, parsedConfig.options || {});
options.configFilePath = configFileName && normalizeSlashes(configFileName);
setConfigFileInOptions(options, sourceFile);
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 c6cc077

Please sign in to comment.