Skip to content

Commit

Permalink
Fix issue with cached browse config not being removed (#10735)
Browse files Browse the repository at this point in the history
  • Loading branch information
Colengms authored Mar 23, 2023
1 parent e5b80da commit 2ad0a95
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 51 deletions.
63 changes: 44 additions & 19 deletions Extension/src/LanguageServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,11 @@ export class DefaultClient implements Client {
private loggingLevel: string | undefined;
private configurationProvider?: string;

public lastCustomBrowseConfiguration: PersistentFolderState<WorkspaceBrowseConfiguration | undefined> | undefined;
public lastCustomBrowseConfigurationProviderId: PersistentFolderState<string | undefined> | undefined;
public lastCustomBrowseConfigurationProviderVersion: PersistentFolderState<Version> | undefined;
private registeredProviders: PersistentFolderState<string[]> | undefined;

public static referencesParams: RenameParams | FindAllReferencesParams | undefined;
public static referencesRequestPending: boolean = false;
public static referencesPendingCancellations: ReferencesCancellationState[] = [];
Expand Down Expand Up @@ -1075,6 +1080,20 @@ export class DefaultClient implements Client {
*/

constructor(workspaceFolder?: vscode.WorkspaceFolder, initializeNow?: boolean) {
if (workspaceFolder !== undefined) {
this.lastCustomBrowseConfiguration = new PersistentFolderState<WorkspaceBrowseConfiguration | undefined>("CPP.lastCustomBrowseConfiguration", undefined, workspaceFolder);
this.lastCustomBrowseConfigurationProviderId = new PersistentFolderState<string | undefined>("CPP.lastCustomBrowseConfigurationProviderId", undefined, workspaceFolder);
this.lastCustomBrowseConfigurationProviderVersion = new PersistentFolderState<Version>("CPP.lastCustomBrowseConfigurationProviderVersion", Version.v5, workspaceFolder);
this.registeredProviders = new PersistentFolderState<string[]>("CPP.registeredProviders", [], workspaceFolder);
// If this provider did the register in the last session, clear out the cached browse config.
if (!this.isProviderRegistered(this.lastCustomBrowseConfigurationProviderId.Value)) {
this.lastCustomBrowseConfigurationProviderId.Value = undefined;
if (this.lastCustomBrowseConfiguration !== undefined) {
this.lastCustomBrowseConfiguration.Value = undefined;
}
}
this.registeredProviders.Value = [];
}
if (!semanticTokensLegend) {
// Semantic token types are identified by indexes in this list of types, in the legend.
const tokenTypesLegend: string[] = [];
Expand Down Expand Up @@ -1134,7 +1153,7 @@ export class DefaultClient implements Client {
await firstClientStarted;
try {
const workspaceFolder: vscode.WorkspaceFolder | undefined = this.rootFolder;
this.innerConfiguration = new configs.CppProperties(rootUri, workspaceFolder);
this.innerConfiguration = new configs.CppProperties(this, rootUri, workspaceFolder);
this.innerConfiguration.ConfigurationsChanged((e) => this.onConfigurationsChanged(e));
this.innerConfiguration.SelectionChanged((e) => this.onSelectedConfigurationChanged(e));
this.innerConfiguration.CompileCommandsChanged((e) => this.onCompileCommandsChanged(e));
Expand Down Expand Up @@ -1586,7 +1605,13 @@ export class DefaultClient implements Client {
openFileVersions.delete(uri);
}

private registeredProviders: CustomConfigurationProvider1[] = [];
public isProviderRegistered(extensionId: string | undefined): boolean {
if (extensionId === undefined || this.registeredProviders === undefined) {
return false;
}
return this.registeredProviders.Value.indexOf(extensionId) > -1;
}

public onRegisterCustomConfigurationProvider(provider: CustomConfigurationProvider1): Thenable<void> {
const onRegistered: () => void = () => {
// version 2 providers control the browse.path. Avoid thrashing the tag parser database by pausing parsing until
Expand All @@ -1596,10 +1621,12 @@ export class DefaultClient implements Client {
}
};
return this.notifyWhenLanguageClientReady(() => {
if (this.registeredProviders.includes(provider)) {
return; // Prevent duplicate processing.
if (this.registeredProviders === undefined // Shouldn't happen.
// Prevent duplicate processing.
|| this.registeredProviders.Value.includes(provider.extensionId)) {
return;
}
this.registeredProviders.push(provider);
this.registeredProviders.Value.push(provider.extensionId);
const rootFolder: vscode.WorkspaceFolder | undefined = this.RootFolder;
if (!rootFolder) {
return; // There is no c_cpp_properties.json to edit because there is no folder open.
Expand Down Expand Up @@ -2704,16 +2731,13 @@ export class DefaultClient implements Client {
});

await this.languageClient.sendRequest(ChangeCppPropertiesRequest, params);
const lastCustomBrowseConfigurationProviderId: PersistentFolderState<string | undefined> | undefined = cppProperties.LastCustomBrowseConfigurationProviderId;
const lastCustomBrowseConfigurationProviderVersion: PersistentFolderState<Version> | undefined = cppProperties.LastCustomBrowseConfigurationProviderVersion;
const lastCustomBrowseConfiguration: PersistentFolderState<WorkspaceBrowseConfiguration | undefined> | undefined = cppProperties.LastCustomBrowseConfiguration;
if (!!lastCustomBrowseConfigurationProviderId && !!lastCustomBrowseConfiguration && !!lastCustomBrowseConfigurationProviderVersion) {
if (!!this.lastCustomBrowseConfigurationProviderId && !!this.lastCustomBrowseConfiguration && !!this.lastCustomBrowseConfigurationProviderVersion) {
if (!this.doneInitialCustomBrowseConfigurationCheck) {
// Send the last custom browse configuration we received from this provider.
// This ensures we don't start tag parsing without it, and undo'ing work we have to re-do when the (likely same) browse config arrives
// Should only execute on launch, for the initial delivery of configurations
if (lastCustomBrowseConfiguration.Value) {
this.sendCustomBrowseConfiguration(lastCustomBrowseConfiguration.Value, lastCustomBrowseConfigurationProviderId.Value, lastCustomBrowseConfigurationProviderVersion.Value);
if (this.lastCustomBrowseConfiguration.Value) {
this.sendCustomBrowseConfiguration(this.lastCustomBrowseConfiguration.Value, this.lastCustomBrowseConfigurationProviderId.Value, this.lastCustomBrowseConfigurationProviderVersion.Value);
params.isReady = false;
}
this.doneInitialCustomBrowseConfigurationCheck = true;
Expand Down Expand Up @@ -2855,11 +2879,12 @@ export class DefaultClient implements Client {

private sendCustomBrowseConfiguration(config: any, providerId: string | undefined, providerVersion: Version, timeoutOccured?: boolean): void {
const rootFolder: vscode.WorkspaceFolder | undefined = this.RootFolder;
if (!rootFolder) {
if (!rootFolder
|| !this.lastCustomBrowseConfiguration
|| !this.lastCustomBrowseConfigurationProviderId) {
return;
}
const lastCustomBrowseConfiguration: PersistentFolderState<WorkspaceBrowseConfiguration | undefined> = new PersistentFolderState<WorkspaceBrowseConfiguration | undefined>("CPP.lastCustomBrowseConfiguration", undefined, rootFolder);
const lastCustomBrowseConfigurationProviderId: PersistentFolderState<string | undefined> = new PersistentFolderState<string | undefined>("CPP.lastCustomBrowseConfigurationProviderId", undefined, rootFolder);

let sanitized: util.Mutable<InternalWorkspaceBrowseConfiguration>;

this.browseConfigurationLogging = "";
Expand All @@ -2871,7 +2896,7 @@ export class DefaultClient implements Client {
if (!timeoutOccured) {
console.log("Received an invalid browse configuration from configuration provider.");
}
const configValue: WorkspaceBrowseConfiguration | undefined = lastCustomBrowseConfiguration.Value;
const configValue: WorkspaceBrowseConfiguration | undefined = this.lastCustomBrowseConfiguration.Value;
if (configValue) {
sanitized = configValue;
console.log("Falling back to last received browse configuration: ", JSON.stringify(sanitized, null, 2));
Expand All @@ -2884,7 +2909,7 @@ export class DefaultClient implements Client {
sanitized = { ...<InternalWorkspaceBrowseConfiguration>config };
if (!this.isWorkspaceBrowseConfiguration(sanitized)) {
console.log("Received an invalid browse configuration from configuration provider: " + JSON.stringify(sanitized));
const configValue: WorkspaceBrowseConfiguration | undefined = lastCustomBrowseConfiguration.Value;
const configValue: WorkspaceBrowseConfiguration | undefined = this.lastCustomBrowseConfiguration.Value;
if (configValue) {
sanitized = configValue;
console.log("Falling back to last received browse configuration: ", JSON.stringify(sanitized, null, 2));
Expand Down Expand Up @@ -2917,11 +2942,11 @@ export class DefaultClient implements Client {
}
}

lastCustomBrowseConfiguration.Value = sanitized;
this.lastCustomBrowseConfiguration.Value = sanitized;
if (!providerId) {
lastCustomBrowseConfigurationProviderId.setDefault();
this.lastCustomBrowseConfigurationProviderId.setDefault();
} else {
lastCustomBrowseConfigurationProviderId.Value = providerId;
this.lastCustomBrowseConfigurationProviderId.Value = providerId;
}
break;
}
Expand Down
45 changes: 13 additions & 32 deletions Extension/src/LanguageServer/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ import * as jsonc from 'comment-json';
import * as nls from 'vscode-nls';
import { setTimeout } from 'timers';
import * as which from 'which';
import { Version, WorkspaceBrowseConfiguration } from 'vscode-cpptools';
import { getOutputChannelLogger } from '../logger';
import { compilerPaths } from './client';
import { compilerPaths, DefaultClient } from './client';
nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
const localize: nls.LocalizeFunc = nls.loadMessageBundle();

Expand Down Expand Up @@ -122,6 +121,7 @@ export interface CompilerDefaults {
}

export class CppProperties {
private client: DefaultClient;
private rootUri: vscode.Uri | undefined;
private propertiesFile: vscode.Uri | undefined | null = undefined; // undefined and null values are handled differently
private readonly configFolder: string;
Expand Down Expand Up @@ -153,24 +153,19 @@ export class CppProperties {
private diagnosticCollection: vscode.DiagnosticCollection;
private prevSquiggleMetrics: Map<string, { [key: string]: number }> = new Map<string, { [key: string]: number }>();
private settingsPanel?: SettingsPanel;
private lastCustomBrowseConfiguration: PersistentFolderState<WorkspaceBrowseConfiguration | undefined> | undefined;
private lastCustomBrowseConfigurationProviderId: PersistentFolderState<string | undefined> | undefined;
private lastCustomBrowseConfigurationProviderVersion: PersistentFolderState<Version> | undefined;
private isWin32: boolean = os.platform() === "win32";

// Any time the default settings are parsed and assigned to `this.configurationJson`,
// we want to track when the default includes have been added to it.
private configurationIncomplete: boolean = true;
trustedCompilerFound: boolean = false;

constructor(rootUri?: vscode.Uri, workspaceFolder?: vscode.WorkspaceFolder) {
constructor(client: DefaultClient, rootUri?: vscode.Uri, workspaceFolder?: vscode.WorkspaceFolder) {
this.client = client;
this.rootUri = rootUri;
const rootPath: string = rootUri ? rootUri.fsPath : "";
if (workspaceFolder) {
this.currentConfigurationIndex = new PersistentFolderState<number>("CppProperties.currentConfigurationIndex", -1, workspaceFolder);
this.lastCustomBrowseConfiguration = new PersistentFolderState<WorkspaceBrowseConfiguration | undefined>("CPP.lastCustomBrowseConfiguration", undefined, workspaceFolder);
this.lastCustomBrowseConfigurationProviderId = new PersistentFolderState<string | undefined>("CPP.lastCustomBrowseConfigurationProviderId", undefined, workspaceFolder);
this.lastCustomBrowseConfigurationProviderVersion = new PersistentFolderState<Version>("CPP.lastCustomBrowseConfigurationProviderVersion", Version.v5, workspaceFolder);
}
this.configFolder = path.join(rootPath, ".vscode");
this.diagnosticCollection = vscode.languages.createDiagnosticCollection(rootPath);
Expand All @@ -190,10 +185,6 @@ export class CppProperties {
public get CurrentConfiguration(): Configuration | undefined { return this.Configurations ? this.Configurations[this.CurrentConfigurationIndex] : undefined; }
public get KnownCompiler(): KnownCompiler[] | undefined { return this.knownCompilers; }

public get LastCustomBrowseConfiguration(): PersistentFolderState<WorkspaceBrowseConfiguration | undefined> | undefined { return this.lastCustomBrowseConfiguration; }
public get LastCustomBrowseConfigurationProviderId(): PersistentFolderState<string | undefined> | undefined { return this.lastCustomBrowseConfigurationProviderId; }
public get LastCustomBrowseConfigurationProviderVersion(): PersistentFolderState<Version> | undefined { return this.lastCustomBrowseConfigurationProviderVersion; }

public get CurrentConfigurationProvider(): string | undefined {
if (this.CurrentConfiguration?.configurationProvider) {
return this.CurrentConfiguration.configurationProvider;
Expand Down Expand Up @@ -971,19 +962,19 @@ export class CppProperties {
if (hasEmptyConfiguration) {
if (providers.size === 1) {
providers.forEach(provider => { configuration.configurationProvider = provider.extensionId; });
if (this.lastCustomBrowseConfigurationProviderId !== undefined) {
keepCachedBrowseConfig = configuration.configurationProvider === this.lastCustomBrowseConfigurationProviderId.Value;
if (this.client.lastCustomBrowseConfigurationProviderId !== undefined) {
keepCachedBrowseConfig = configuration.configurationProvider === this.client.lastCustomBrowseConfigurationProviderId.Value;
}
} else if (this.lastCustomBrowseConfigurationProviderId !== undefined
&& !!this.lastCustomBrowseConfigurationProviderId.Value) {
} else if (this.client.lastCustomBrowseConfigurationProviderId !== undefined
&& !!this.client.lastCustomBrowseConfigurationProviderId.Value) {
// Use the last configuration provider we received a browse config from as the provider ID.
configuration.configurationProvider = this.lastCustomBrowseConfigurationProviderId.Value;
configuration.configurationProvider = this.client.lastCustomBrowseConfigurationProviderId.Value;
}
} else if (this.lastCustomBrowseConfigurationProviderId !== undefined) {
keepCachedBrowseConfig = configuration.configurationProvider === this.lastCustomBrowseConfigurationProviderId.Value;
} else if (this.client.lastCustomBrowseConfigurationProviderId !== undefined) {
keepCachedBrowseConfig = configuration.configurationProvider === this.client.lastCustomBrowseConfigurationProviderId.Value;
}
if (!keepCachedBrowseConfig && this.lastCustomBrowseConfiguration !== undefined) {
this.lastCustomBrowseConfiguration.Value = undefined;
if (!keepCachedBrowseConfig && this.client.lastCustomBrowseConfiguration !== undefined) {
this.client.lastCustomBrowseConfiguration.Value = undefined;
}
}

Expand Down Expand Up @@ -2134,16 +2125,6 @@ export class CppProperties {
}

dispose(): void {
if (this.lastCustomBrowseConfigurationProviderId !== undefined) {
const config: Configuration | undefined = this.CurrentConfiguration;
if (config !== undefined && config.configurationProvider !== this.lastCustomBrowseConfigurationProviderId.Value) {
this.lastCustomBrowseConfigurationProviderId.Value = undefined;
if (this.lastCustomBrowseConfiguration !== undefined) {
this.lastCustomBrowseConfiguration.Value = undefined;
}
}
}

this.disposables.forEach((d) => d.dispose());
this.disposables = [];

Expand Down

0 comments on commit 2ad0a95

Please sign in to comment.