@@ -170,43 +170,37 @@ suite('Integration: Python Projects', function () {
170170 const project = projects [ 0 ] ;
171171 const env = environments [ 0 ] ;
172172
173- // Diagnostic logging for CI debugging
174- console . log ( `[TEST DEBUG] Project URI: ${ project . uri . fsPath } ` ) ;
175- console . log ( `[TEST DEBUG] Setting environment with envId: ${ env . envId . id } ` ) ;
176- console . log ( `[TEST DEBUG] Environment path: ${ env . environmentPath ?. fsPath } ` ) ;
177- console . log ( `[TEST DEBUG] Total environments available: ${ environments . length } ` ) ;
178- environments . forEach ( ( e , i ) => {
179- console . log ( `[TEST DEBUG] env[${ i } ]: ${ e . envId . id } (${ e . displayName } )` ) ;
180- } ) ;
181-
182- // Set environment for project
183- await api . setEnvironment ( project . uri , env ) ;
184-
185- // Track what getEnvironment returns during polling for diagnostics
186- let pollCount = 0 ;
187- let lastRetrievedId : string | undefined ;
188-
189- // Wait for the environment to be retrievable with the correct ID
190- // This handles async persistence across platforms
191- // Use 15s timeout - CI runners (especially macos) can be slow with settings persistence
192- await waitForCondition (
193- async ( ) => {
194- const retrieved = await api . getEnvironment ( project . uri ) ;
195- pollCount ++ ;
196- const retrievedId = retrieved ?. envId ?. id ;
197- if ( retrievedId !== lastRetrievedId ) {
198- console . log (
199- `[TEST DEBUG] Poll #${ pollCount } : getEnvironment returned envId=${ retrievedId ?? 'undefined' } ` ,
200- ) ;
201- lastRetrievedId = retrievedId ;
173+ // Wait for the change event, then verify getEnvironment.
174+ // Using an event-driven approach instead of polling avoids a race condition where
175+ // setEnvironment's async settings write hasn't landed by the time getEnvironment
176+ // reads back the manager from settings.
177+ await new Promise < void > ( ( resolve , reject ) => {
178+ const timeout = setTimeout ( ( ) => {
179+ subscription . dispose ( ) ;
180+ reject (
181+ new Error (
182+ `onDidChangeEnvironment did not fire for project within 15s. Expected envId: ${ env . envId . id } ` ,
183+ ) ,
184+ ) ;
185+ } , 15_000 ) ;
186+
187+ const subscription = api . onDidChangeEnvironment ( ( e ) => {
188+ if ( e . uri ?. toString ( ) === project . uri . toString ( ) && e . new ?. envId . id === env . envId . id ) {
189+ clearTimeout ( timeout ) ;
190+ subscription . dispose ( ) ;
191+ resolve ( ) ;
202192 }
203- return retrieved !== undefined && retrieved . envId . id === env . envId . id ;
204- } ,
205- 15_000 ,
206- `Environment was not set correctly. Expected envId: ${ env . envId . id } , last retrieved: ${ lastRetrievedId } ` ,
207- ) ;
193+ } ) ;
194+
195+ // Set environment after subscribing so we don't miss the event
196+ api . setEnvironment ( project . uri , env ) . catch ( ( err ) => {
197+ clearTimeout ( timeout ) ;
198+ subscription . dispose ( ) ;
199+ reject ( err ) ;
200+ } ) ;
201+ } ) ;
208202
209- // Final verification
203+ // Verify getEnvironment returns the correct value now that setEnvironment has fully completed
210204 const retrievedEnv = await api . getEnvironment ( project . uri ) ;
211205 assert . ok ( retrievedEnv , 'Should get environment after setting' ) ;
212206 assert . strictEqual ( retrievedEnv . envId . id , env . envId . id , 'Retrieved environment should match set environment' ) ;
0 commit comments