Skip to content

Commit e2b5bf9

Browse files
authored
Address config issues: #1599, #1790 (#1807)
* addresses #1599, #1790 * Update all configs when c_cpp_properties.json is parsed
1 parent 7cff0f1 commit e2b5bf9

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

Extension/src/LanguageServer/configurations.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,14 @@ export class CppProperties {
141141
console.assert(rootPath !== undefined);
142142
this.currentConfigurationIndex = new PersistentFolderState<number>("CppProperties.currentConfigurationIndex", -1, rootPath);
143143
this.configFolder = path.join(rootPath, ".vscode");
144-
this.resetToDefaultSettings(this.currentConfigurationIndex.Value === -1);
145-
144+
146145
let configFilePath: string = path.join(this.configFolder, "c_cpp_properties.json");
147146
if (fs.existsSync(configFilePath)) {
148147
this.propertiesFile = vscode.Uri.file(configFilePath);
148+
this.parsePropertiesFile();
149+
}
150+
if (!this.configurationJson) {
151+
this.resetToDefaultSettings(this.CurrentConfiguration === -1);
149152
}
150153

151154
this.configFileWatcher = vscode.workspace.createFileSystemWatcher(path.join(this.configFolder, this.configurationGlobPattern));
@@ -452,13 +455,27 @@ export class CppProperties {
452455
// the system includes were available.
453456
this.configurationIncomplete = false;
454457

458+
// Update intelliSenseMode, compilerPath, cStandard, and cppStandard with the defaults if they're missing.
455459
let dirty: boolean = false;
456460
for (let i: number = 0; i < this.configurationJson.configurations.length; i++) {
457461
let config: Configuration = this.configurationJson.configurations[i];
458462
if (config.intelliSenseMode === undefined) {
459463
dirty = true;
460464
config.intelliSenseMode = this.getIntelliSenseModeForPlatform(config.name);
461465
}
466+
// Don't set the default if compileCommands exist, until it is fixed to have the correct value.
467+
if (config.compilerPath === undefined && this.defaultCompilerPath && !config.compileCommands) {
468+
config.compilerPath = this.defaultCompilerPath;
469+
dirty = true;
470+
}
471+
if (!config.cStandard && this.defaultCStandard) {
472+
config.cStandard = this.defaultCStandard;
473+
dirty = true;
474+
}
475+
if (!config.cppStandard && this.defaultCppStandard) {
476+
config.cppStandard = this.defaultCppStandard;
477+
dirty = true;
478+
}
462479
}
463480

464481
if (this.configurationJson.version !== configVersion) {
@@ -475,24 +492,13 @@ export class CppProperties {
475492
}
476493
}
477494

478-
// Update the compilerPath, cStandard, and cppStandard with the default if they're missing.
479-
let config: Configuration = this.configurationJson.configurations[this.CurrentConfiguration];
480-
// Don't set the default if compileCommands exist, until it is fixed to have the correct value.
481-
if (config.compilerPath === undefined && this.defaultCompilerPath && !config.compileCommands) {
482-
config.compilerPath = this.defaultCompilerPath;
483-
dirty = true;
484-
}
485-
if (!config.cStandard && this.defaultCStandard) {
486-
config.cStandard = this.defaultCStandard;
487-
dirty = true;
488-
}
489-
if (!config.cppStandard && this.defaultCppStandard) {
490-
config.cppStandard = this.defaultCppStandard;
491-
dirty = true;
492-
}
493-
494495
if (dirty) {
495-
fs.writeFileSync(this.propertiesFile.fsPath, JSON.stringify(this.configurationJson, null, 4));
496+
try {
497+
fs.writeFileSync(this.propertiesFile.fsPath, JSON.stringify(this.configurationJson, null, 4));
498+
} catch {
499+
// Ignore write errors, the file may be under source control. Updated settings will only be modified in memory.
500+
vscode.window.showWarningMessage('Attempt to update "' + this.propertiesFile.fsPath + '" failed (do you have write access?)');
501+
}
496502
}
497503
} catch (err) {
498504
vscode.window.showErrorMessage('Failed to parse "' + this.propertiesFile.fsPath + '": ' + err.message);

0 commit comments

Comments
 (0)