@@ -1183,74 +1183,66 @@ export function isCl(compilerPath: string): boolean {
11831183
11841184/** CompilerPathAndArgs retains original casing of text input for compiler path and args */
11851185export interface CompilerPathAndArgs {
1186- compilerPath ?: string | null ;
1186+ compilerPath ?: string ;
11871187 compilerName : string ;
11881188 compilerArgs ?: string [ ] ;
11891189 compilerArgsFromCommandLineInPath : string [ ] ;
11901190 allCompilerArgs : string [ ] ;
1191+ error ?: string ;
1192+ telemetry ?: { [ key : string ] : number } ;
11911193}
11921194
1193- export function extractCompilerPathAndArgs ( useLegacyBehavior : boolean , inputCompilerPath ?: string | null , compilerArgs ?: string [ ] ) : CompilerPathAndArgs {
1194- let compilerPath : string | undefined | null = inputCompilerPath ;
1195+ /**
1196+ * 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
1197+ * verified that the compiler exists. (e.g. `compilerArgsFromCommandLineInPath` will be empty)
1198+ *
1199+ * @param useLegacyBehavior - If true, use the legacy behavior of separating the compilerPath from the args.
1200+ * @param inputCompilerPath - The compiler path input from the user.
1201+ * @param compilerArgs - The compiler args input from the user.
1202+ * @param cwd - The directory used to resolve relative paths.
1203+ */
1204+ export function extractCompilerPathAndArgs ( useLegacyBehavior : boolean , inputCompilerPath ?: string , compilerArgs ?: string [ ] , cwd ?: string ) : CompilerPathAndArgs {
1205+ let compilerPath : string | undefined = inputCompilerPath ;
11951206 let compilerName : string = "" ;
11961207 let compilerArgsFromCommandLineInPath : string [ ] = [ ] ;
1208+ const trimLegacyQuotes = ( compilerPath ?: string ) : string | undefined => {
1209+ if ( compilerPath && useLegacyBehavior ) {
1210+ // Try to trim quotes from compiler path.
1211+ const tempCompilerPath : string [ ] = extractArgs ( compilerPath ) ;
1212+ if ( tempCompilerPath . length > 0 ) {
1213+ return tempCompilerPath [ 0 ] ;
1214+ }
1215+ }
1216+ return compilerPath ;
1217+ } ;
11971218 if ( compilerPath ) {
11981219 compilerPath = compilerPath . trim ( ) ;
11991220 if ( isCl ( compilerPath ) || checkExecutableWithoutExtensionExistsSync ( compilerPath ) ) {
12001221 // If the path ends with cl, or if a file is found at that path, accept it without further validation.
12011222 compilerName = path . basename ( compilerPath ) ;
1223+ } else if ( cwd && checkExecutableWithoutExtensionExistsSync ( path . join ( cwd , compilerPath ) ) ) {
1224+ // If the path is relative and a file is found at that path, accept it without further validation.
1225+ compilerPath = path . join ( cwd , compilerPath ) ;
1226+ compilerName = path . basename ( compilerPath ) ;
12021227 } else if ( compilerPath . startsWith ( "\"" ) || ( os . platform ( ) !== 'win32' && compilerPath . startsWith ( "'" ) ) ) {
12031228 // If the string starts with a quote, treat it as a command line.
12041229 // Otherwise, a path with a leading quote would not be valid.
1205- if ( useLegacyBehavior ) {
1206- compilerArgsFromCommandLineInPath = legacyExtractArgs ( compilerPath ) ;
1207- if ( compilerArgsFromCommandLineInPath . length > 0 ) {
1208- compilerPath = compilerArgsFromCommandLineInPath . shift ( ) ;
1209- if ( compilerPath ) {
1210- // Try to trim quotes from compiler path.
1211- const tempCompilerPath : string [ ] | undefined = extractArgs ( compilerPath ) ;
1212- if ( tempCompilerPath && compilerPath . length > 0 ) {
1213- compilerPath = tempCompilerPath [ 0 ] ;
1214- }
1215- compilerName = path . basename ( compilerPath ) ;
1216- }
1217- }
1218- } else {
1219- compilerArgsFromCommandLineInPath = extractArgs ( compilerPath ) ;
1220- if ( compilerArgsFromCommandLineInPath . length > 0 ) {
1221- compilerPath = compilerArgsFromCommandLineInPath . shift ( ) ;
1222- if ( compilerPath ) {
1223- compilerName = path . basename ( compilerPath ) ;
1224- }
1225- }
1230+ compilerArgsFromCommandLineInPath = useLegacyBehavior ? legacyExtractArgs ( compilerPath ) : extractArgs ( compilerPath ) ;
1231+ if ( compilerArgsFromCommandLineInPath . length > 0 ) {
1232+ compilerPath = trimLegacyQuotes ( compilerArgsFromCommandLineInPath . shift ( ) ) ;
1233+ compilerName = path . basename ( compilerPath ?? '' ) ;
12261234 }
12271235 } else {
1228- const spaceStart : number = compilerPath . lastIndexOf ( " " ) ;
1229- if ( spaceStart !== - 1 ) {
1230- // There is no leading quote, but a space suggests it might be a command line.
1231- // Try processing it as a command line, and validate that by checking for the executable.
1236+ if ( compilerPath . includes ( ' ' ) ) {
1237+ // There is no leading quote, but there is a space so we'll treat it as a command line.
12321238 const potentialArgs : string [ ] = useLegacyBehavior ? legacyExtractArgs ( compilerPath ) : extractArgs ( compilerPath ) ;
1233- let potentialCompilerPath : string | undefined = potentialArgs . shift ( ) ;
1234- if ( useLegacyBehavior ) {
1235- if ( potentialCompilerPath ) {
1236- const tempCompilerPath : string [ ] | undefined = extractArgs ( potentialCompilerPath ) ;
1237- if ( tempCompilerPath && compilerPath . length > 0 ) {
1238- potentialCompilerPath = tempCompilerPath [ 0 ] ;
1239- }
1240- }
1241- }
1242- if ( potentialCompilerPath ) {
1243- if ( isCl ( potentialCompilerPath ) || checkExecutableWithoutExtensionExistsSync ( potentialCompilerPath ) ) {
1244- compilerArgsFromCommandLineInPath = potentialArgs ;
1245- compilerPath = potentialCompilerPath ;
1246- compilerName = path . basename ( compilerPath ) ;
1247- }
1248- }
1239+ compilerPath = trimLegacyQuotes ( potentialArgs . shift ( ) ) ;
1240+ compilerArgsFromCommandLineInPath = potentialArgs ;
12491241 }
1242+ compilerName = path . basename ( compilerPath ?? '' ) ;
12501243 }
12511244 }
1252- let allCompilerArgs : string [ ] = ! compilerArgs ? [ ] : compilerArgs ;
1253- allCompilerArgs = allCompilerArgs . concat ( compilerArgsFromCommandLineInPath ) ;
1245+ const allCompilerArgs : string [ ] = ( compilerArgs ?? [ ] ) . concat ( compilerArgsFromCommandLineInPath ) ;
12541246 return { compilerPath, compilerName, compilerArgs, compilerArgsFromCommandLineInPath, allCompilerArgs } ;
12551247}
12561248
0 commit comments