Skip to content

Conversation

@allancascante
Copy link
Contributor

@allancascante allancascante commented Oct 28, 2025

Description

When a user has access to the same Azure subscription through multiple accounts (e.g., personal and work accounts), the subscription filter dialog exhibited several issues, reported here:

Duplicate Selection Bug: Selecting one instance of a subscription would automatically select all instances across different accounts
Missing Subscriptions: Not all subscriptions were visible in the filter dropdown, even when no filtering was applied
Missing Tenants: Not all tenants for an account were displayed in the connection dialog
Error on Load: "Error loading Azure subscriptions" appeared when the same subscription existed under multiple accounts

Root Cause Analysis

Issue 1: Tenant Discovery
Problem: auth.getTenants(account) with an account parameter wasn't reliably returning all tenants for an account, particularly for newly added tenants where the user hadn't explicitly signed in through VS Code.

Fix: Changed from auth.getTenants(account) to auth.getTenants() (without account parameter) to fetch ALL tenants across all accounts, then filter by account ID client-side. This ensures all tenants are discovered regardless of authentication state.

Issue 2: Token Refresh
Problem: VS Code's Azure authentication system only returns subscriptions from tenants where it has active authentication tokens. Newly added tenants/subscriptions weren't discovered until explicit authentication.

Fix: Added refreshAzureTokensForAllAccounts() method that proactively signs into all tenants for all accounts when the connection dialog opens, ensuring all subscriptions are discoverable.

Issue 3: Subscription Storage Collisions
Problem: The subscription filter used a composite key format tenantId/subscriptionId, which is not unique when the same subscription is accessible from multiple accounts. This caused:

  • Subscriptions stored in a Map<subscriptionId, AzureSubscription> to overwrite each other
  • Filter configuration to store duplicate entries
  • UI to show both instances as selected when only one was chosen

Fix: Changed composite key format from tenantId/subscriptionId to account/tenantId/subscriptionId to ensure uniqueness. Updated all related code:

  • Subscription Map storage
  • Filter configuration format
  • getTenantFilters() and getSubscriptionFilters() parsing logic
  • Tenant ID population in subscription info

Changes Made

Core Logic Changes

  1. azureHelpers.ts
  • Modified getTenantsForAccount() to fetch all tenants then filter by account
  • Updated composite key generation: ${sub.account.label}/${sub.tenantId}/${sub.subscriptionId}
  • Updated promptForAzureSubscriptionFilter() to use new composite key format
  • Enhanced getSubscriptionQuickPickItems() to include account label in description for disambiguation
  1. connectionDialogWebviewController.ts
  • Added refreshAzureTokensForAllAccounts() method to proactively ensure tokens exist for all tenants
  • Modified loadAzureSubscriptions() to call token refresh before fetching subscriptions
  • Updated subscription Map key from subscriptionId to ${tenantId}/${subscriptionId}
  • Updated loadAzureServersForSubscription() to accept both tenantId and subscriptionId parameters
  • Modified reducer to extract tenantId from state when loading servers
  • Added tenant count to telemetry for tracking scenario where users have multiple tenants
  1. MssqlVSCodeAzureSubscriptionProvider.ts
  • Updated getTenantFilters() to parse index 1 from 3-part composite key
  • Updated getSubscriptionFilters() to parse index 2 from 3-part composite key
  1. connectionDialog.ts
  • Added tenantId: string field to AzureSubscriptionInfo interface
  1. azureBrowsePage.tsx
  • Changed server selection mode from SelectFirstIfAny to AlwaysSelectNone (prevents auto-selection)
  • Added proper cleanup of database selection when server selection becomes invalid

Testing

New Test File: MssqlVSCodeAzureSubscriptionProvider.test.ts:

  • getTenantFilters extracts tenant IDs from composite keys
  • getSubscriptionFilters extracts subscription IDs from composite keys
  • getTenantFilters handles empty configuration
  • getSubscriptionFilters handles empty configuration
  • filters correctly handle duplicate subscriptions from different accounts
  • filters correctly parse complex email addresses

Enhanced Test File: azureHelpers.test.ts:

  • creates pick items with account-based composite keys
  • respects previous selection with account-based composite keys
  • handles complex email addresses in composite keys

UI Updates

Before:

  • Subscription filter showed duplicate selections for shared subscriptions:
image image
  • Not all subscriptions visible even without filters
  • Auto-selected servers inappropriately
  • Error messages when loading subscriptions

After:

  • Each subscription+account combination is independently selectable
image - All subscriptions from all tenants are discoverable on dialog open image - Subscription filter accurately reflects user selection - No auto-selection of servers/databases unless explicitly chosen - Clear visual distinction in UI showing account email in subscription description

Breaking Changes

Configuration Format Change: Existing subscription filters stored as tenantId/subscriptionId will need to be reset. Users with active filters will see all subscriptions unfiltered on first use after update and will need to re-select their desired subscriptions. This is intentional to avoid migration complexity and ensure clean state.

The change maintains backward compatibility with the VS Code Azure SDK and doesn't affect users who:

  • Only have one Azure account
  • Don't have duplicate subscriptions across accounts
  • Haven't set up subscription filters
  • Related: This fix also resolves the server auto-selection issue mentioned in the original report where servers were being selected even when filters weren't specified.

Code Changes Checklist

  • New or updated unit tests added
  • All existing tests pass (npm run test)
  • Code follows contributing guidelines
  • Telemetry/logging updated if relevant
  • No regressions or UX breakage

Reviewers: Please read our reviewer guidelines

@github-actions
Copy link

github-actions bot commented Oct 28, 2025

PR Changes

Category Target Branch PR Branch Difference
Code Coverage 57.07% 57.14% ⚪ 0.00%
VSIX Size 4911 KB 4898 KB ⚪ -13 KB ( 0% )
Webview Bundle Size 4032 KB 4024 KB ⚪ -8 KB ( 0% )

@codecov-commenter
Copy link

codecov-commenter commented Oct 28, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.14%. Comparing base (3b09699) to head (75d7a88).
⚠️ Report is 11 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #20367      +/-   ##
==========================================
+ Coverage   54.94%   55.14%   +0.19%     
==========================================
  Files         200      200              
  Lines       17460    17466       +6     
  Branches     1142     1141       -1     
==========================================
+ Hits         9593     9631      +38     
+ Misses       7867     7835      -32     
Files with missing lines Coverage Δ
src/azure/MssqlVSCodeAzureSubscriptionProvider.ts 100.00% <100.00%> (+73.33%) ⬆️
src/connectionconfig/azureHelpers.ts 68.23% <100.00%> (+11.68%) ⬆️
...nectionconfig/connectionDialogWebviewController.ts 47.28% <100.00%> (+0.60%) ⬆️
src/sharedInterfaces/connectionDialog.ts 95.45% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@allancascante allancascante linked an issue Oct 29, 2025 that may be closed by this pull request
12 tasks
@allancascante allancascante requested a review from Benjin November 4, 2025 15:45
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.

[Bug]: Unable to list subscription from several tenants

4 participants