@@ -1092,74 +1092,66 @@ export function isCl(compilerPath: string): boolean {
1092
1092
1093
1093
/** CompilerPathAndArgs retains original casing of text input for compiler path and args */
1094
1094
export interface CompilerPathAndArgs {
1095
- compilerPath ?: string | null ;
1095
+ compilerPath ?: string ;
1096
1096
compilerName : string ;
1097
1097
compilerArgs ?: string [ ] ;
1098
1098
compilerArgsFromCommandLineInPath : string [ ] ;
1099
1099
allCompilerArgs : string [ ] ;
1100
+ error ?: string ;
1101
+ telemetry ?: { [ key : string ] : number } ;
1100
1102
}
1101
1103
1102
- export function extractCompilerPathAndArgs ( useLegacyBehavior : boolean , inputCompilerPath ?: string | null , compilerArgs ?: string [ ] ) : CompilerPathAndArgs {
1103
- let compilerPath : string | undefined | null = inputCompilerPath ;
1104
+ /**
1105
+ * Parse the compiler path input into a compiler path and compiler args. If there are no args in the input string, this function will have
1106
+ * verified that the compiler exists. (e.g. `compilerArgsFromCommandLineInPath` will be empty)
1107
+ *
1108
+ * @param useLegacyBehavior - If true, use the legacy behavior of separating the compilerPath from the args.
1109
+ * @param inputCompilerPath - The compiler path input from the user.
1110
+ * @param compilerArgs - The compiler args input from the user.
1111
+ * @param cwd - The directory used to resolve relative paths.
1112
+ */
1113
+ export function extractCompilerPathAndArgs ( useLegacyBehavior : boolean , inputCompilerPath ?: string , compilerArgs ?: string [ ] , cwd ?: string ) : CompilerPathAndArgs {
1114
+ let compilerPath : string | undefined = inputCompilerPath ;
1104
1115
let compilerName : string = "" ;
1105
1116
let compilerArgsFromCommandLineInPath : string [ ] = [ ] ;
1117
+ const trimLegacyQuotes = ( compilerPath ?: string ) : string | undefined => {
1118
+ if ( compilerPath && useLegacyBehavior ) {
1119
+ // Try to trim quotes from compiler path.
1120
+ const tempCompilerPath : string [ ] = extractArgs ( compilerPath ) ;
1121
+ if ( tempCompilerPath . length > 0 ) {
1122
+ return tempCompilerPath [ 0 ] ;
1123
+ }
1124
+ }
1125
+ return compilerPath ;
1126
+ } ;
1106
1127
if ( compilerPath ) {
1107
1128
compilerPath = compilerPath . trim ( ) ;
1108
1129
if ( isCl ( compilerPath ) || checkExecutableWithoutExtensionExistsSync ( compilerPath ) ) {
1109
1130
// If the path ends with cl, or if a file is found at that path, accept it without further validation.
1110
1131
compilerName = path . basename ( compilerPath ) ;
1132
+ } else if ( cwd && checkExecutableWithoutExtensionExistsSync ( path . join ( cwd , compilerPath ) ) ) {
1133
+ // If the path is relative and a file is found at that path, accept it without further validation.
1134
+ compilerPath = path . join ( cwd , compilerPath ) ;
1135
+ compilerName = path . basename ( compilerPath ) ;
1111
1136
} else if ( compilerPath . startsWith ( "\"" ) || ( os . platform ( ) !== 'win32' && compilerPath . startsWith ( "'" ) ) ) {
1112
1137
// If the string starts with a quote, treat it as a command line.
1113
1138
// Otherwise, a path with a leading quote would not be valid.
1114
- if ( useLegacyBehavior ) {
1115
- compilerArgsFromCommandLineInPath = legacyExtractArgs ( compilerPath ) ;
1116
- if ( compilerArgsFromCommandLineInPath . length > 0 ) {
1117
- compilerPath = compilerArgsFromCommandLineInPath . shift ( ) ;
1118
- if ( compilerPath ) {
1119
- // Try to trim quotes from compiler path.
1120
- const tempCompilerPath : string [ ] | undefined = extractArgs ( compilerPath ) ;
1121
- if ( tempCompilerPath && compilerPath . length > 0 ) {
1122
- compilerPath = tempCompilerPath [ 0 ] ;
1123
- }
1124
- compilerName = path . basename ( compilerPath ) ;
1125
- }
1126
- }
1127
- } else {
1128
- compilerArgsFromCommandLineInPath = extractArgs ( compilerPath ) ;
1129
- if ( compilerArgsFromCommandLineInPath . length > 0 ) {
1130
- compilerPath = compilerArgsFromCommandLineInPath . shift ( ) ;
1131
- if ( compilerPath ) {
1132
- compilerName = path . basename ( compilerPath ) ;
1133
- }
1134
- }
1139
+ compilerArgsFromCommandLineInPath = useLegacyBehavior ? legacyExtractArgs ( compilerPath ) : extractArgs ( compilerPath ) ;
1140
+ if ( compilerArgsFromCommandLineInPath . length > 0 ) {
1141
+ compilerPath = trimLegacyQuotes ( compilerArgsFromCommandLineInPath . shift ( ) ) ;
1142
+ compilerName = path . basename ( compilerPath ?? '' ) ;
1135
1143
}
1136
1144
} else {
1137
- const spaceStart : number = compilerPath . lastIndexOf ( " " ) ;
1138
- if ( spaceStart !== - 1 ) {
1139
- // There is no leading quote, but a space suggests it might be a command line.
1140
- // Try processing it as a command line, and validate that by checking for the executable.
1145
+ if ( compilerPath . includes ( ' ' ) ) {
1146
+ // There is no leading quote, but there is a space so we'll treat it as a command line.
1141
1147
const potentialArgs : string [ ] = useLegacyBehavior ? legacyExtractArgs ( compilerPath ) : extractArgs ( compilerPath ) ;
1142
- let potentialCompilerPath : string | undefined = potentialArgs . shift ( ) ;
1143
- if ( useLegacyBehavior ) {
1144
- if ( potentialCompilerPath ) {
1145
- const tempCompilerPath : string [ ] | undefined = extractArgs ( potentialCompilerPath ) ;
1146
- if ( tempCompilerPath && compilerPath . length > 0 ) {
1147
- potentialCompilerPath = tempCompilerPath [ 0 ] ;
1148
- }
1149
- }
1150
- }
1151
- if ( potentialCompilerPath ) {
1152
- if ( isCl ( potentialCompilerPath ) || checkExecutableWithoutExtensionExistsSync ( potentialCompilerPath ) ) {
1153
- compilerArgsFromCommandLineInPath = potentialArgs ;
1154
- compilerPath = potentialCompilerPath ;
1155
- compilerName = path . basename ( compilerPath ) ;
1156
- }
1157
- }
1148
+ compilerPath = trimLegacyQuotes ( potentialArgs . shift ( ) ) ;
1149
+ compilerArgsFromCommandLineInPath = potentialArgs ;
1158
1150
}
1151
+ compilerName = path . basename ( compilerPath ?? '' ) ;
1159
1152
}
1160
1153
}
1161
- let allCompilerArgs : string [ ] = ! compilerArgs ? [ ] : compilerArgs ;
1162
- allCompilerArgs = allCompilerArgs . concat ( compilerArgsFromCommandLineInPath ) ;
1154
+ const allCompilerArgs : string [ ] = ( compilerArgs ?? [ ] ) . concat ( compilerArgsFromCommandLineInPath ) ;
1163
1155
return { compilerPath, compilerName, compilerArgs, compilerArgsFromCommandLineInPath, allCompilerArgs } ;
1164
1156
}
1165
1157
0 commit comments