Skip to content

Commit 4b10145

Browse files
committed
Make sure commandline options are absolute paths so they dont conflict with tsbuildinfo paths
Fixes #33667
1 parent 827427f commit 4b10145

File tree

5 files changed

+63
-42
lines changed

5 files changed

+63
-42
lines changed

src/compiler/builder.ts

+1-35
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ namespace ts {
11181118

11191119
const state: ReusableBuilderProgramState = {
11201120
fileInfos,
1121-
compilerOptions: convertFromReusableCompilerOptions(program.options, toAbsolutePath),
1121+
compilerOptions: convertToOptionsWithAbsolutePaths(program.options, toAbsolutePath),
11221122
referencedMap: getMapOfReferencedSet(program.referencedMap, toPath),
11231123
exportedModulesMap: getMapOfReferencedSet(program.exportedModulesMap, toPath),
11241124
semanticDiagnosticsPerFile: program.semanticDiagnosticsPerFile && arrayToMap(program.semanticDiagnosticsPerFile, value => toPath(isString(value) ? value : value[0]), value => isString(value) ? emptyArray : value[1]),
@@ -1156,40 +1156,6 @@ namespace ts {
11561156
}
11571157
}
11581158

1159-
function convertFromReusableCompilerOptions(options: CompilerOptions, toAbsolutePath: (path: string) => string) {
1160-
const result: CompilerOptions = {};
1161-
const optionsNameMap = getOptionNameMap().optionNameMap;
1162-
1163-
for (const name in options) {
1164-
if (hasProperty(options, name)) {
1165-
result[name] = convertFromReusableCompilerOptionValue(
1166-
optionsNameMap.get(name.toLowerCase()),
1167-
options[name] as CompilerOptionsValue,
1168-
toAbsolutePath
1169-
);
1170-
}
1171-
}
1172-
if (result.configFilePath) {
1173-
result.configFilePath = toAbsolutePath(result.configFilePath);
1174-
}
1175-
return result;
1176-
}
1177-
1178-
function convertFromReusableCompilerOptionValue(option: CommandLineOption | undefined, value: CompilerOptionsValue, toAbsolutePath: (path: string) => string) {
1179-
if (option) {
1180-
if (option.type === "list") {
1181-
const values = value as readonly (string | number)[];
1182-
if (option.element.isFilePath && values.length) {
1183-
return values.map(toAbsolutePath);
1184-
}
1185-
}
1186-
else if (option.isFilePath) {
1187-
return toAbsolutePath(value as string);
1188-
}
1189-
}
1190-
return value;
1191-
}
1192-
11931159
export function createRedirectedBuilderProgram(state: { program: Program | undefined; compilerOptions: CompilerOptions; }, configFileParsingDiagnostics: readonly Diagnostic[]): BuilderProgram {
11941160
return {
11951161
getState: notImplemented,

src/compiler/commandLineParser.ts

+35
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,41 @@ namespace ts {
20412041
}
20422042
}
20432043

2044+
/* @internal */
2045+
export function convertToOptionsWithAbsolutePaths(options: CompilerOptions, toAbsolutePath: (path: string) => string) {
2046+
const result: CompilerOptions = {};
2047+
const optionsNameMap = getOptionNameMap().optionNameMap;
2048+
2049+
for (const name in options) {
2050+
if (hasProperty(options, name)) {
2051+
result[name] = convertToOptionValueWithAbsolutePaths(
2052+
optionsNameMap.get(name.toLowerCase()),
2053+
options[name] as CompilerOptionsValue,
2054+
toAbsolutePath
2055+
);
2056+
}
2057+
}
2058+
if (result.configFilePath) {
2059+
result.configFilePath = toAbsolutePath(result.configFilePath);
2060+
}
2061+
return result;
2062+
}
2063+
2064+
function convertToOptionValueWithAbsolutePaths(option: CommandLineOption | undefined, value: CompilerOptionsValue, toAbsolutePath: (path: string) => string) {
2065+
if (option) {
2066+
if (option.type === "list") {
2067+
const values = value as readonly (string | number)[];
2068+
if (option.element.isFilePath && values.length) {
2069+
return values.map(toAbsolutePath);
2070+
}
2071+
}
2072+
else if (option.isFilePath) {
2073+
return toAbsolutePath(value as string);
2074+
}
2075+
}
2076+
return value;
2077+
}
2078+
20442079
/**
20452080
* Parse the contents of a config file (tsconfig.json).
20462081
* @param json The contents of the config file to parse

src/testRunner/unittests/tsc/helpers.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,15 @@ namespace ts {
5050

5151
Debug.assert(commandLine.fileNames.length !== 0 || !!configFileName);
5252

53+
const currentDirectory = sys.getCurrentDirectory();
54+
const getCanonicalFileName = createGetCanonicalFileName(sys.useCaseSensitiveFileNames);
55+
const commandLineOptions = convertToOptionsWithAbsolutePaths(
56+
commandLine.options,
57+
fileName => toPath(fileName, currentDirectory, getCanonicalFileName)
58+
);
59+
5360
if (configFileName) {
54-
const configParseResult = Debug.assertDefined(parseConfigFileWithSystem(configFileName, commandLine.options, sys, reportDiagnostic));
61+
const configParseResult = Debug.assertDefined(parseConfigFileWithSystem(configFileName, commandLineOptions, sys, reportDiagnostic));
5562
if (isIncrementalCompilation(configParseResult.options)) {
5663
performIncrementalCompilation(sys, configParseResult);
5764
}
@@ -61,10 +68,16 @@ namespace ts {
6168
}
6269
else {
6370
if (isIncrementalCompilation(commandLine.options)) {
64-
performIncrementalCompilation(sys, commandLine);
71+
performIncrementalCompilation(sys, {
72+
...commandLine,
73+
options: commandLineOptions
74+
});
6575
}
6676
else {
67-
performCompilation(sys, commandLine);
77+
performCompilation(sys, {
78+
...commandLine,
79+
options: commandLineOptions
80+
});
6881
}
6982
}
7083
}

src/tsc/tsc.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ namespace ts {
120120
return sys.exit(ExitStatus.Success);
121121
}
122122

123-
const commandLineOptions = commandLine.options;
123+
const currentDirectory = sys.getCurrentDirectory();
124+
const getCanonicalFileName = createGetCanonicalFileName(sys.useCaseSensitiveFileNames);
125+
const commandLineOptions = convertToOptionsWithAbsolutePaths(
126+
commandLine.options,
127+
fileName => toPath(fileName, currentDirectory, getCanonicalFileName)
128+
);
124129
if (configFileName) {
125130
const configParseResult = parseConfigFileWithSystem(configFileName, commandLineOptions, sys, reportDiagnostic)!; // TODO: GH#18217
126131
if (commandLineOptions.showConfig) {
@@ -148,7 +153,7 @@ namespace ts {
148153
else {
149154
if (commandLineOptions.showConfig) {
150155
// eslint-disable-next-line no-null/no-null
151-
sys.write(JSON.stringify(convertToTSConfig(commandLine, combinePaths(sys.getCurrentDirectory(), "tsconfig.json"), sys), null, 4) + sys.newLine);
156+
sys.write(JSON.stringify(convertToTSConfig(commandLine, combinePaths(currentDirectory, "tsconfig.json"), sys), null, 4) + sys.newLine);
152157
return sys.exit(ExitStatus.Success);
153158
}
154159
updateReportDiagnostic(commandLineOptions);
@@ -157,7 +162,10 @@ namespace ts {
157162
createWatchOfFilesAndCompilerOptions(commandLine.fileNames, commandLineOptions);
158163
}
159164
else if (isIncrementalCompilation(commandLineOptions)) {
160-
performIncrementalCompilation(commandLine);
165+
performIncrementalCompilation({
166+
...commandLine,
167+
options: commandLineOptions
168+
});
161169
}
162170
else {
163171
performCompilation(commandLine.fileNames, /*references*/ undefined, commandLineOptions);

tests/baselines/reference/tsc/incremental/incremental-declaration-doesnt-change/when-passing-passing-filename-for-buildinfo-on-commandline.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ exitCode:: 0
44

55

66
//// [/src/project/.tsbuildinfo] file written with same contents
7-
//// [/src/project/src/main.js] file written with same contents

0 commit comments

Comments
 (0)