Skip to content

Add per-tree cache invalidation to fix #1413#1416

Merged
alexweininger merged 1 commit intomainfrom
fix/1413-per-tree-cache-flags
Mar 26, 2026
Merged

Add per-tree cache invalidation to fix #1413#1416
alexweininger merged 1 commit intomainfrom
fix/1413-per-tree-cache-flags

Conversation

@alexweininger
Copy link
Copy Markdown
Member

@alexweininger alexweininger commented Mar 26, 2026

Bug Fix

#1413 — Accounts & Tenants view does not auto-refresh after adding a second account

The cache-clear mechanism was a shared one-shot boolean. Whichever tree called consumeClearCacheFlag() first got noCache: true; subsequent trees got false and loaded from stale cache (missing the newly added account).

Fix

Replace the shared boolean with per-tree flags (azure | tenant | focus). Each tree has its own independent flag via a TreeViewId-keyed record, so:

  • Trees can't consume each other's cache-clear signals
  • A collapsed tree won't leave a stale flag for another tree to accidentally consume later

Additional improvements

  • selectSubscriptions: Set cache-clear flag before updating the setting, so the provider-triggered refresh (via onDidChangeConfiguration) already sees noCache: true — avoids a redundant second refresh
  • Tenant tree: Refresh on any onRefreshSuggested reason except subscriptionFilterChange, making it forward-compatible with new reasons (e.g. cloudChange from auth: Watch sovereign cloud config and fire onRefreshSuggested vscode-azuretools#2248)

Root cause

Regression introduced by PR #1383 ("Performance improvements", commit b76bfd8).

Fixes #1413

@alexweininger alexweininger requested a review from a team as a code owner March 26, 2026 19:22
Copilot AI review requested due to automatic review settings March 26, 2026 19:22
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes regressions introduced in #1383 by updating the cache-invalidation mechanism so cache clears are scoped per tree (Azure Resources, Accounts & Tenants, Focus), and by restoring an explicit cache-clear + refresh when subscription selection changes.

Changes:

  • Replace shared one-shot cache-clear boolean with per-tree cache-clear flags keyed by TreeViewId (azure | tenant | focus).
  • Update Azure, Tenants, and Focus tree data providers to consume the per-tree cache-clear flag.
  • Restore explicit cache-clear + refreshAzureTree() after updating the selected subscriptions setting.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/extensionVariables.ts Introduces TreeViewId and per-tree cache-clear flag storage + APIs.
src/tree/azure/AzureResourceTreeDataProvider.ts Consumes the cache-clear flag for the Azure tree via consumeClearCacheFlag('azure').
src/tree/tenants/TenantResourceTreeDataProvider.ts Consumes the cache-clear flag for the Tenants tree via consumeClearCacheFlag('tenant').
src/tree/azure/FocusViewTreeDataProvider.ts Consumes the cache-clear flag for the Focus tree via consumeClearCacheFlag('focus').
src/commands/accounts/selectSubscriptions.ts Re-adds explicit cache-clear + Azure tree refresh after subscription selection updates.
src/commands/accounts/logIn.ts Clears cache + refreshes Azure and Tenants trees after sign-in using per-tree flags.
src/commands/registerCommands.ts Updates refresh/sign-in commands to set per-tree cache-clear flags.
src/commands/sovereignCloud/configureSovereignCloud.ts Clears cache + refreshes Azure and Tenants views after sovereign cloud changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

ext.setClearCacheOnNextLoad('azure');
ext.actions.refreshAzureTree(); // Refresh now that sign in is complete
ext.setClearCacheOnNextLoad('tenant');
ext.actions.refreshTenantTree(); // Refresh now that sign in is complete
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After sign-in you clear/refresh Azure and Tenants, but the Focus view uses its own subscription provider + per-tree cache flag (consumeClearCacheFlag('focus')). If the Focus view was previously loaded, it can still refresh using stale cached subscriptions unless you also set the Focus cache-clear flag (and refresh the focus tree if appropriate).

Suggested change
ext.actions.refreshTenantTree(); // Refresh now that sign in is complete
ext.actions.refreshTenantTree(); // Refresh now that sign in is complete
ext.setClearCacheOnNextLoad('focus');
ext.actions.refreshFocusTree?.(); // Refresh Focus view to ensure it uses fresh subscriptions

Copilot uses AI. Check for mistakes.
ext.setClearCacheOnNextLoad('azure');
ext.actions.refreshAzureTree();
ext.setClearCacheOnNextLoad('tenant');
ext.actions.refreshTenantTree();
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command clears/refreshes Azure Resources and Accounts & Tenants, but the Focus view has an independent subscription cache (per-tree cache flag). If users have a focused group, the Focus view may continue showing resources from the previous cloud unless you also clear the Focus cache (and refresh the Focus tree).

Suggested change
ext.actions.refreshTenantTree();
ext.actions.refreshTenantTree();
ext.setClearCacheOnNextLoad('focus');
ext.actions.refreshFocusTree();

Copilot uses AI. Check for mistakes.
Comment on lines 89 to 94
registerCommand('azureTenantsView.signInToTenant', async (_context, node: TenantTreeItem) => {
await (await ext.subscriptionProviderFactory()).signIn(node);
ext.setClearCacheOnNextLoad();
ext.setClearCacheOnNextLoad('tenant');
ext.actions.refreshTenantTree();
ext.setClearCacheOnNextLoad('azure');
ext.actions.refreshAzureTree();
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After signing in to a tenant, caches are cleared for the Tenants and Azure trees but not for the Focus view. Since each tree now has an independent cache-clear flag, the Focus view can refresh on onRefreshSuggested yet still read stale cached subscriptions unless you also set setClearCacheOnNextLoad('focus') (and refresh the focus tree when needed).

Copilot uses AI. Check for mistakes.
Comment on lines 103 to 108
registerCommand('azureResourceGroups.signInToTenant', async () => {
await signInToTenant(await ext.subscriptionProviderFactory());
ext.setClearCacheOnNextLoad();
ext.setClearCacheOnNextLoad('tenant');
ext.actions.refreshTenantTree();
ext.setClearCacheOnNextLoad('azure');
ext.actions.refreshAzureTree();
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above for the command-palette azureResourceGroups.signInToTenant: it clears/refreshes tenant+azure trees but not focus. With per-tree cache flags, consider also clearing the Focus view cache (and refreshing focus) so all views reflect the new tenant auth state consistently.

Copilot uses AI. Check for mistakes.
@alexweininger alexweininger changed the title Fix per-tree cache invalidation (#1413, #1412) Fix per-tree cache invalidation (#1413) Mar 26, 2026
@alexweininger alexweininger force-pushed the fix/1413-per-tree-cache-flags branch from 930efba to c496bde Compare March 26, 2026 19:35
bwateratmsft
bwateratmsft previously approved these changes Mar 26, 2026
@alexweininger alexweininger force-pushed the fix/1413-per-tree-cache-flags branch from c496bde to ed3a9d7 Compare March 26, 2026 19:42
@alexweininger alexweininger force-pushed the fix/1413-per-tree-cache-flags branch from ed3a9d7 to c956267 Compare March 26, 2026 19:53
@alexweininger alexweininger changed the title Fix per-tree cache invalidation (#1413) Add per-tree cache invalidation Mar 26, 2026
@alexweininger alexweininger changed the title Add per-tree cache invalidation Add per-tree cache invalidation to fix #1413 Mar 26, 2026
Replace the shared one-shot cache-clear boolean with per-tree flags
(azure, tenant, focus). The old boolean was consumed by whichever tree
loaded first, leaving other trees to read stale cached data.

- Convert clearCacheOnNextLoadFlag boolean to per-tree flag map
- setClearCacheOnNextLoad/consumeClearCacheFlag now take a TreeViewId
- Add cache-clear per tree in signInToTenant and configureSovereignCloud
- Include Focus view in cache-clear + refresh for sign-in, sign-in-to-
  tenant, and sovereign cloud commands
- Set cache-clear flag before updating subscription setting so the
  provider-triggered refresh already sees noCache: true
- Tenant tree now refreshes on any reason except subscriptionFilterChange,
  making it forward-compatible with new reasons (e.g. cloudChange)

Fixes #1413

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@alexweininger alexweininger force-pushed the fix/1413-per-tree-cache-flags branch from c956267 to bff1bfd Compare March 26, 2026 20:18
@alexweininger alexweininger merged commit a0a6be1 into main Mar 26, 2026
3 checks passed
@alexweininger alexweininger deleted the fix/1413-per-tree-cache-flags branch March 26, 2026 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The “Accounts & Tenants” view does not auto‑refresh after adding a second account

4 participants