@@ -868,6 +868,16 @@ suite('Interpreter Selection - applyInitialEnvironmentSelection', () => {
868868
869869 const showWarnStub = sandbox . stub ( windowApis , 'showWarningMessage' ) . resolves ( undefined ) ;
870870
871+ // Use a deferred promise to deterministically wait for the background global scope
872+ let resolveGlobalDone ! : ( ) => void ;
873+ const globalDone = new Promise < void > ( ( resolve ) => {
874+ resolveGlobalDone = resolve ;
875+ } ) ;
876+ const origSetEnvironments = mockEnvManagers . setEnvironments ;
877+ origSetEnvironments . callsFake ( async ( ...args : unknown [ ] ) => {
878+ resolveGlobalDone ( ) ;
879+ } ) ;
880+
871881 await applyInitialEnvironmentSelection (
872882 mockEnvManagers as unknown as EnvironmentManagers ,
873883 mockProjectManager as unknown as PythonProjectManager ,
@@ -878,8 +888,10 @@ suite('Interpreter Selection - applyInitialEnvironmentSelection', () => {
878888 // Workspace folder should resolve (venv found)
879889 assert . ok ( mockEnvManagers . setEnvironment . called , 'setEnvironment should be called for workspace folder' ) ;
880890
881- // Wait a tick for the background global scope to complete
882- await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) ) ;
891+ // Wait for the background global scope to call setEnvironments
892+ await globalDone ;
893+ // Flush microtasks so the .then() handler for notifyUserOfSettingErrors runs
894+ await new Promise < void > ( ( resolve ) => process . nextTick ( resolve ) ) ;
883895
884896 // Global scope should still resolve (falls to auto-discovery) and show warning
885897 assert . ok ( mockEnvManagers . setEnvironments . called , 'setEnvironments should be called for global scope' ) ;
@@ -896,8 +908,17 @@ suite('Interpreter Selection - applyInitialEnvironmentSelection', () => {
896908 sandbox . stub ( workspaceApis , 'getConfiguration' ) . returns ( createMockConfig ( [ ] ) as WorkspaceConfiguration ) ;
897909 sandbox . stub ( helpers , 'getUserConfiguredSetting' ) . returns ( undefined ) ;
898910
911+ // Use a deferred promise to deterministically wait for the background global scope
912+ let resolveGlobalDone ! : ( ) => void ;
913+ const globalDone = new Promise < void > ( ( resolve ) => {
914+ resolveGlobalDone = resolve ;
915+ } ) ;
916+
899917 // Make setEnvironments throw — simulating a crash in global scope
900- mockEnvManagers . setEnvironments . rejects ( new Error ( 'Simulated global scope crash' ) ) ;
918+ mockEnvManagers . setEnvironments . callsFake ( async ( ) => {
919+ resolveGlobalDone ( ) ;
920+ throw new Error ( 'Simulated global scope crash' ) ;
921+ } ) ;
901922
902923 // Should NOT throw — errors are caught inside resolveGlobalScope
903924 await applyInitialEnvironmentSelection (
@@ -907,8 +928,10 @@ suite('Interpreter Selection - applyInitialEnvironmentSelection', () => {
907928 mockApi as unknown as PythonEnvironmentApi ,
908929 ) ;
909930
910- // Wait a tick for the background global scope to complete
911- await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) ) ;
931+ // Wait for the background global scope to call setEnvironments
932+ await globalDone ;
933+ // Flush microtasks so the catch handler runs
934+ await new Promise < void > ( ( resolve ) => process . nextTick ( resolve ) ) ;
912935
913936 // Workspace folder should still have resolved
914937 assert . ok ( mockEnvManagers . setEnvironment . called , 'setEnvironment should be called for workspace folder' ) ;
0 commit comments