Skip to content

Commit 0c9e000

Browse files
authored
Fix open file scenario bugs due to struct null changes (#5137)
1 parent 2091cc2 commit 0c9e000

5 files changed

Lines changed: 60 additions & 44 deletions

File tree

Extension/src/LanguageServer/client.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,6 @@ export class DefaultClient implements Client {
602602
}
603603
this.storagePath = storagePath;
604604
const rootUri: vscode.Uri | undefined = this.RootUri;
605-
if (!rootUri) {
606-
throw new Error("Empty URI in client constructor");
607-
}
608605
this.settingsTracker = getTracker(rootUri);
609606
this.colorizationSettings = new ColorizationSettings(rootUri);
610607
try {
@@ -624,9 +621,6 @@ export class DefaultClient implements Client {
624621
this.queueBlockingTask(() => languageClient.onReady().then(
625622
() => {
626623
let workspaceFolder: vscode.WorkspaceFolder | undefined = this.rootFolder;
627-
if (!workspaceFolder) {
628-
throw new Error("Empty URI in client constructor");
629-
}
630624
this.innerConfiguration = new configs.CppProperties(rootUri, workspaceFolder);
631625
this.innerConfiguration.ConfigurationsChanged((e) => this.onConfigurationsChanged(e));
632626
this.innerConfiguration.SelectionChanged((e) => this.onSelectedConfigurationChanged(e));

Extension/src/LanguageServer/colorization.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class ThemeStyle {
5757
}
5858

5959
export class ColorizationSettings {
60-
private uri: vscode.Uri;
60+
private uri: vscode.Uri | undefined;
6161
private pendingTask?: util.BlockingTask<any>;
6262
private editorBackground?: string;
6363

@@ -76,7 +76,7 @@ export class ColorizationSettings {
7676
["variable", "variables"]
7777
]);
7878

79-
constructor(uri: vscode.Uri) {
79+
constructor(uri: vscode.Uri | undefined) {
8080
this.uri = uri;
8181
}
8282

Extension/src/LanguageServer/configurations.ts

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ export interface CompilerDefaults {
105105
}
106106

107107
export class CppProperties {
108-
private rootUri: vscode.Uri;
108+
private rootUri: vscode.Uri | undefined;
109109
private propertiesFile: vscode.Uri | undefined | null = undefined; // undefined and null values are handled differently
110110
private readonly configFolder: string;
111111
private configurationJson?: ConfigurationJson;
112-
private currentConfigurationIndex: PersistentFolderState<number>;
112+
private currentConfigurationIndex: PersistentFolderState<number> | undefined;
113113
private configFileWatcher: vscode.FileSystemWatcher | null = null;
114114
private configFileWatcherFallbackTime: Date = new Date(); // Used when file watching fails.
115115
private compileCommandFileWatchers: fs.FSWatcher[] = [];
@@ -137,10 +137,12 @@ export class CppProperties {
137137
// we want to track when the default includes have been added to it.
138138
private configurationIncomplete: boolean = true;
139139

140-
constructor(rootUri: vscode.Uri, workspaceFolder: vscode.WorkspaceFolder) {
140+
constructor(rootUri: vscode.Uri | undefined, workspaceFolder: vscode.WorkspaceFolder | undefined) {
141141
this.rootUri = rootUri;
142142
let rootPath: string = rootUri ? rootUri.fsPath : "";
143-
this.currentConfigurationIndex = new PersistentFolderState<number>("CppProperties.currentConfigurationIndex", -1, workspaceFolder);
143+
if (workspaceFolder) {
144+
this.currentConfigurationIndex = new PersistentFolderState<number>("CppProperties.currentConfigurationIndex", -1, workspaceFolder);
145+
}
144146
this.configFolder = path.join(rootPath, ".vscode");
145147
this.diagnosticCollection = vscode.languages.createDiagnosticCollection(rootPath);
146148
this.buildVcpkgIncludePath();
@@ -151,7 +153,7 @@ export class CppProperties {
151153
public get SelectionChanged(): vscode.Event<number> { return this.selectionChanged.event; }
152154
public get CompileCommandsChanged(): vscode.Event<string> { return this.compileCommandsChanged.event; }
153155
public get Configurations(): Configuration[] | undefined { return this.configurationJson ? this.configurationJson.configurations : undefined; }
154-
public get CurrentConfigurationIndex(): number { return this.currentConfigurationIndex.Value; }
156+
public get CurrentConfigurationIndex(): number { return this.currentConfigurationIndex === undefined ? 0 : this.currentConfigurationIndex.Value; }
155157
public get CurrentConfiguration(): Configuration | undefined { return this.Configurations ? this.Configurations[this.CurrentConfigurationIndex] : undefined; }
156158
public get KnownCompiler(): KnownCompiler[] | undefined { return this.knownCompilers; }
157159

@@ -263,10 +265,12 @@ export class CppProperties {
263265
if (resetIndex || this.CurrentConfigurationIndex < 0 ||
264266
this.CurrentConfigurationIndex >= this.configurationJson.configurations.length) {
265267
let index: number | undefined = this.getConfigIndexForPlatform(this.configurationJson);
266-
if (index === undefined) {
267-
this.currentConfigurationIndex.setDefault();
268-
} else {
269-
this.currentConfigurationIndex.Value = index;
268+
if (this.currentConfigurationIndex !== undefined) {
269+
if (index === undefined) {
270+
this.currentConfigurationIndex.setDefault();
271+
} else {
272+
this.currentConfigurationIndex.Value = index;
273+
}
270274
}
271275
}
272276
this.configurationIncomplete = true;
@@ -493,7 +497,9 @@ export class CppProperties {
493497
}
494498
}
495499

496-
this.currentConfigurationIndex.Value = index;
500+
if (this.currentConfigurationIndex !== undefined) {
501+
this.currentConfigurationIndex.Value = index;
502+
}
497503
this.onSelectionChanged();
498504
}
499505

@@ -712,7 +718,7 @@ export class CppProperties {
712718
let configNames: string[] | undefined = this.ConfigurationNames;
713719
if (configNames && this.configurationJson) {
714720
// Use the active configuration as the default selected configuration to load on UI editor
715-
this.settingsPanel.selectedConfigIndex = this.currentConfigurationIndex.Value;
721+
this.settingsPanel.selectedConfigIndex = this.CurrentConfigurationIndex;
716722
this.settingsPanel.createOrShow(configNames,
717723
this.configurationJson.configurations[this.settingsPanel.selectedConfigIndex],
718724
this.getErrorsForConfigUI(this.settingsPanel.selectedConfigIndex));
@@ -740,7 +746,7 @@ export class CppProperties {
740746
// The settings UI became visible or active.
741747
// Ensure settingsPanel has copy of latest current configuration
742748
if (this.settingsPanel.selectedConfigIndex >= this.configurationJson.configurations.length) {
743-
this.settingsPanel.selectedConfigIndex = this.currentConfigurationIndex.Value;
749+
this.settingsPanel.selectedConfigIndex = this.CurrentConfigurationIndex;
744750
}
745751
this.settingsPanel.updateConfigUI(configNames,
746752
this.configurationJson.configurations[this.settingsPanel.selectedConfigIndex],
@@ -809,10 +815,12 @@ export class CppProperties {
809815
this.CurrentConfigurationIndex >= this.configurationJson.configurations.length) {
810816
// If the index is out of bounds (during initialization or due to removal of configs), fix it.
811817
let index: number | undefined = this.getConfigIndexForPlatform(this.configurationJson);
812-
if (!index) {
813-
this.currentConfigurationIndex.setDefault();
814-
} else {
815-
this.currentConfigurationIndex.Value = index;
818+
if (this.currentConfigurationIndex !== undefined) {
819+
if (!index) {
820+
this.currentConfigurationIndex.setDefault();
821+
} else {
822+
this.currentConfigurationIndex.Value = index;
823+
}
816824
}
817825
}
818826
}
@@ -882,18 +890,22 @@ export class CppProperties {
882890
this.CurrentConfigurationIndex >= 0 && this.CurrentConfigurationIndex < this.configurationJson.configurations.length) {
883891
for (let i: number = 0; i < newJson.configurations.length; i++) {
884892
if (newJson.configurations[i].name === this.configurationJson.configurations[this.CurrentConfigurationIndex].name) {
885-
this.currentConfigurationIndex.Value = i;
893+
if (this.currentConfigurationIndex !== undefined) {
894+
this.currentConfigurationIndex.Value = i;
895+
}
886896
break;
887897
}
888898
}
889899
}
890900
this.configurationJson = newJson;
891901
if (this.CurrentConfigurationIndex < 0 || this.CurrentConfigurationIndex >= newJson.configurations.length) {
892902
let index: number | undefined = this.getConfigIndexForPlatform(newJson);
893-
if (index === undefined) {
894-
this.currentConfigurationIndex.setDefault();
895-
} else {
896-
this.currentConfigurationIndex.Value = index;
903+
if (this.currentConfigurationIndex !== undefined) {
904+
if (index === undefined) {
905+
this.currentConfigurationIndex.setDefault();
906+
} else {
907+
this.currentConfigurationIndex.Value = index;
908+
}
897909
}
898910
}
899911

@@ -975,11 +987,13 @@ export class CppProperties {
975987

976988
// first resolve variables
977989
result = util.resolveVariables(path, this.ExtendedEnvironment);
978-
if (result.includes("${workspaceFolder}")) {
979-
result = result.replace("${workspaceFolder}", this.rootUri.fsPath);
980-
}
981-
if (result.includes("${workspaceRoot}")) {
982-
result = result.replace("${workspaceRoot}", this.rootUri.fsPath);
990+
if (this.rootUri) {
991+
if (result.includes("${workspaceFolder}")) {
992+
result = result.replace("${workspaceFolder}", this.rootUri.fsPath);
993+
}
994+
if (result.includes("${workspaceRoot}")) {
995+
result = result.replace("${workspaceRoot}", this.rootUri.fsPath);
996+
}
983997
}
984998
if (result.includes("${vcpkgRoot}") && util.getVcpkgRoot()) {
985999
result = result.replace("${vcpkgRoot}", util.getVcpkgRoot());
@@ -1040,6 +1054,8 @@ export class CppProperties {
10401054
if (!fs.existsSync(resolvedCompilerPath)) {
10411055
if (existsWithExeAdded(resolvedCompilerPath)) {
10421056
resolvedCompilerPath += ".exe";
1057+
} else if (!this.rootUri) {
1058+
pathExists = false;
10431059
} else {
10441060
// Check again for a relative path.
10451061
const relativePath: string = this.rootUri.fsPath + path.sep + resolvedCompilerPath;
@@ -1118,12 +1134,16 @@ export class CppProperties {
11181134

11191135
// Check if resolved path exists
11201136
if (!fs.existsSync(resolvedPath)) {
1121-
// Check for relative path if resolved path does not exists
1122-
const relativePath: string = this.rootUri.fsPath + path.sep + resolvedPath;
1123-
if (!fs.existsSync(relativePath)) {
1137+
if (!this.rootUri) {
11241138
pathExists = false;
11251139
} else {
1126-
resolvedPath = relativePath;
1140+
// Check for relative path if resolved path does not exists
1141+
const relativePath: string = this.rootUri.fsPath + path.sep + resolvedPath;
1142+
if (!fs.existsSync(relativePath)) {
1143+
pathExists = false;
1144+
} else {
1145+
resolvedPath = relativePath;
1146+
}
11271147
}
11281148
}
11291149

@@ -1307,6 +1327,8 @@ export class CppProperties {
13071327
if (!fs.existsSync(resolvedPath)) {
13081328
if (existsWithExeAdded(resolvedPath)) {
13091329
resolvedPath += ".exe";
1330+
} else if (!this.rootUri) {
1331+
pathExists = false;
13101332
} else {
13111333
// Check again for a relative path.
13121334
const relativePath: string = this.rootUri.fsPath + path.sep + resolvedPath;

Extension/src/LanguageServer/settingsTracker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ let cache: SettingsTracker;
1818

1919
export class SettingsTracker {
2020
private previousCppSettings: { [key: string]: any } = {};
21-
private resource: vscode.Uri;
21+
private resource: vscode.Uri | undefined;
2222

23-
constructor(resource: vscode.Uri) {
23+
constructor(resource: vscode.Uri | undefined) {
2424
this.resource = resource;
2525
this.collectSettings(() => true);
2626
}
@@ -185,7 +185,7 @@ export class SettingsTracker {
185185
}
186186
}
187187

188-
export function getTracker(resource: vscode.Uri): SettingsTracker {
188+
export function getTracker(resource: vscode.Uri | undefined): SettingsTracker {
189189
if (!cache) {
190190
cache = new SettingsTracker(resource);
191191
}

Extension/src/common.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,13 @@ export function resolveVariables(input: string | undefined, additionalEnvironmen
350350
switch (varType) {
351351
case "env": {
352352
if (additionalEnvironment) {
353-
let v: string | string[] = additionalEnvironment[name];
353+
let v: string | string[] | undefined = additionalEnvironment[name];
354354
if (isString(v)) {
355355
newValue = v;
356356
} else if (input === match && isArrayOfString(v)) {
357357
newValue = v.join(";");
358358
}
359-
if (!newValue) {
359+
if (newValue === undefined) {
360360
newValue = process.env[name];
361361
}
362362
}
@@ -383,7 +383,7 @@ export function resolveVariables(input: string | undefined, additionalEnvironmen
383383
}
384384
default: { assert.fail("unknown varType matched"); }
385385
}
386-
return newValue ? newValue : match;
386+
return newValue !== undefined ? newValue : match;
387387
});
388388
}
389389

0 commit comments

Comments
 (0)