Skip to content

Commit d2bd3e0

Browse files
authored
Refactor compilerPath logic to reflect settings validation changes (#12556)
* Remove unnesseacary null check and set compilerPathIsExplicit when defaultCompilerPath is null * Remove unneccessary else statement. * Refactor changes * Propogate null usage to reflect null typing for compilerPath. * Remove normalization of null to undefined. * Fix linting * Add null to resolvedCompilerPath * Update cpp_properties schema
1 parent d4c46c7 commit d2bd3e0

File tree

5 files changed

+18
-15
lines changed

5 files changed

+18
-15
lines changed

Extension/c_cpp_properties.schema.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
"compilerPath": {
1919
"markdownDescription": "Full path of the compiler being used, e.g. `/usr/bin/gcc`, to enable more accurate IntelliSense.",
2020
"descriptionHint": "Markdown text between `` should not be translated or localized (they represent literal text) and the capitalization, spacing, and punctuation (including the ``) should not be altered.",
21-
"type": "string"
21+
"type": [
22+
"string",
23+
"null"
24+
]
2225
},
2326
"compilerArgs": {
2427
"markdownDescription": "Compiler arguments to modify the includes or defines used, e.g. `-nostdinc++`, `-m32`, etc. Arguments that take additional space-delimited arguments should be entered as separate arguments in the array, e.g. for `--sysroot <arg>` use `\"--sysroot\", \"<arg>\"`.",

Extension/src/LanguageServer/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,7 +3052,7 @@ export class DefaultClient implements Client {
30523052
providerVersion < Version.v6,
30533053
itemConfig.compilerPath,
30543054
util.isArrayOfString(itemConfig.compilerArgs) ? itemConfig.compilerArgs : undefined);
3055-
itemConfig.compilerPath = compilerPathAndArgs.compilerPath;
3055+
itemConfig.compilerPath = compilerPathAndArgs.compilerPath ?? undefined;
30563056
if (itemConfig.compilerPath !== undefined) {
30573057
void this.addTrustedCompiler(itemConfig.compilerPath).catch(logAndReturn.undefined);
30583058
}
@@ -3156,7 +3156,7 @@ export class DefaultClient implements Client {
31563156
providerVersion < Version.v6,
31573157
sanitized.compilerPath,
31583158
util.isArrayOfString(sanitized.compilerArgs) ? sanitized.compilerArgs : undefined);
3159-
sanitized.compilerPath = compilerPathAndArgs.compilerPath;
3159+
sanitized.compilerPath = compilerPathAndArgs.compilerPath ?? undefined;
31603160
if (sanitized.compilerPath !== undefined) {
31613161
void this.addTrustedCompiler(sanitized.compilerPath).catch(logAndReturn.undefined);
31623162
}

Extension/src/LanguageServer/configurations.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ export interface ConfigurationJson {
6262

6363
export interface Configuration {
6464
name: string;
65-
compilerPathInCppPropertiesJson?: string;
66-
compilerPath?: string;
65+
compilerPathInCppPropertiesJson?: string | null;
66+
compilerPath?: string | null;
6767
compilerPathIsExplicit?: boolean;
6868
compilerArgs?: string[];
6969
compilerArgsLegacy?: string[];
@@ -988,10 +988,9 @@ export class CppProperties {
988988
} else {
989989
// However, if compileCommands are used and compilerPath is explicitly set, it's still necessary to resolve variables in it.
990990
if (configuration.compilerPath === "${default}") {
991-
configuration.compilerPath = settings.defaultCompilerPath ?? undefined;
991+
configuration.compilerPath = settings.defaultCompilerPath;
992992
}
993993
if (configuration.compilerPath === null) {
994-
configuration.compilerPath = undefined;
995994
configuration.compilerPathIsExplicit = true;
996995
} else if (configuration.compilerPath !== undefined) {
997996
configuration.compilerPath = util.resolveVariables(configuration.compilerPath, env);
@@ -1516,7 +1515,7 @@ export class CppProperties {
15161515
return success;
15171516
}
15181517

1519-
private resolvePath(input_path: string | undefined, replaceAsterisks: boolean = true, assumeRelative: boolean = true): string {
1518+
private resolvePath(input_path: string | undefined | null, replaceAsterisks: boolean = true, assumeRelative: boolean = true): string {
15201519
if (!input_path || input_path === "${default}") {
15211520
return "";
15221521
}
@@ -1567,10 +1566,10 @@ export class CppProperties {
15671566

15681567
// Check if config name is unique.
15691568
errors.name = this.isConfigNameUnique(config.name);
1570-
let resolvedCompilerPath: string | undefined;
1569+
let resolvedCompilerPath: string | undefined | null;
15711570
// Validate compilerPath
15721571
if (config.compilerPath) {
1573-
resolvedCompilerPath = which.sync(config.compilerPath, { nothrow: true }) ?? undefined;
1572+
resolvedCompilerPath = which.sync(config.compilerPath, { nothrow: true });
15741573
}
15751574

15761575
if (resolvedCompilerPath === undefined) {
@@ -1590,6 +1589,7 @@ export class CppProperties {
15901589
(compilerPathAndArgs.compilerArgsFromCommandLineInPath && compilerPathAndArgs.compilerArgsFromCommandLineInPath.length > 0) &&
15911590
!resolvedCompilerPath.startsWith('"') &&
15921591
compilerPathAndArgs.compilerPath !== undefined &&
1592+
compilerPathAndArgs.compilerPath !== null &&
15931593
compilerPathAndArgs.compilerPath.includes(" ");
15941594

15951595
const compilerPathErrors: string[] = [];
@@ -1598,7 +1598,7 @@ export class CppProperties {
15981598
}
15991599

16001600
// Get compiler path without arguments before checking if it exists
1601-
resolvedCompilerPath = compilerPathAndArgs.compilerPath;
1601+
resolvedCompilerPath = compilerPathAndArgs.compilerPath ?? undefined;
16021602
if (resolvedCompilerPath) {
16031603
let pathExists: boolean = true;
16041604
const existsWithExeAdded: (path: string) => boolean = (path: string) => isWindows && !path.startsWith("/") && fs.existsSync(path + ".exe");

Extension/src/LanguageServer/cppBuildTaskProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class CppBuildTaskProvider implements TaskProvider {
9898

9999
// Get user compiler path.
100100
const userCompilerPathAndArgs: util.CompilerPathAndArgs | undefined = await activeClient.getCurrentCompilerPathAndArgs();
101-
let userCompilerPath: string | undefined;
101+
let userCompilerPath: string | undefined | null;
102102
if (userCompilerPathAndArgs) {
103103
userCompilerPath = userCompilerPathAndArgs.compilerPath;
104104
if (userCompilerPath && userCompilerPathAndArgs.compilerName) {

Extension/src/common.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,15 +1095,15 @@ export function isCl(compilerPath: string): boolean {
10951095

10961096
/** CompilerPathAndArgs retains original casing of text input for compiler path and args */
10971097
export interface CompilerPathAndArgs {
1098-
compilerPath?: string;
1098+
compilerPath?: string | null;
10991099
compilerName: string;
11001100
compilerArgs?: string[];
11011101
compilerArgsFromCommandLineInPath: string[];
11021102
allCompilerArgs: string[];
11031103
}
11041104

1105-
export function extractCompilerPathAndArgs(useLegacyBehavior: boolean, inputCompilerPath?: string, compilerArgs?: string[]): CompilerPathAndArgs {
1106-
let compilerPath: string | undefined = inputCompilerPath;
1105+
export function extractCompilerPathAndArgs(useLegacyBehavior: boolean, inputCompilerPath?: string | null, compilerArgs?: string[]): CompilerPathAndArgs {
1106+
let compilerPath: string | undefined | null = inputCompilerPath;
11071107
let compilerName: string = "";
11081108
let compilerArgsFromCommandLineInPath: string[] = [];
11091109
if (compilerPath) {

0 commit comments

Comments
 (0)