@@ -168,48 +168,47 @@ suite('Integration: Python Projects', function () {
168168 }
169169
170170 const project = projects [ 0 ] ;
171- const env = environments [ 0 ] ;
172-
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- } ) ;
181171
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- let lastRetrievedManagerId : string | undefined ;
172+ // Pick an environment different from the current one so setEnvironment
173+ // always triggers onDidChangeEnvironment (the event is suppressed when
174+ // old and new have the same envId).
175+ const currentEnv = await api . getEnvironment ( project . uri ) ;
176+ let env = environments [ 0 ] ;
177+ if ( currentEnv && currentEnv . envId . id === env . envId . id && environments . length > 1 ) {
178+ env = environments [ 1 ] ;
179+ }
189180
190- // Wait for the environment to be retrievable with the correct ID
191- // This handles async persistence across platforms
192- // Use 15s timeout - CI runners (especially macos) can be slow with settings persistence
193- await waitForCondition (
194- async ( ) => {
195- const retrieved = await api . getEnvironment ( project . uri ) ;
196- pollCount ++ ;
197- const retrievedId = retrieved ?. envId ?. id ;
198- lastRetrievedManagerId = retrieved ?. envId ?. managerId ;
199- if ( retrievedId !== lastRetrievedId ) {
200- console . log (
201- `[TEST DEBUG] Poll #${ pollCount } : getEnvironment returned envId=${ retrievedId ?? 'undefined' } , managerId=${ lastRetrievedManagerId ?? 'undefined' } ` ,
202- ) ;
203- lastRetrievedId = retrievedId ;
181+ // Wait for the change event, then verify getEnvironment.
182+ // Using an event-driven approach instead of polling avoids a race condition where
183+ // setEnvironment's async settings write hasn't landed by the time getEnvironment
184+ // reads back the manager from settings.
185+ await new Promise < void > ( ( resolve , reject ) => {
186+ const timeout = setTimeout ( ( ) => {
187+ subscription . dispose ( ) ;
188+ reject (
189+ new Error (
190+ `onDidChangeEnvironment did not fire for project within 15s. Expected envId: ${ env . envId . id } ` ,
191+ ) ,
192+ ) ;
193+ } , 15_000 ) ;
194+
195+ const subscription = api . onDidChangeEnvironment ( ( e ) => {
196+ if ( e . uri ?. toString ( ) === project . uri . toString ( ) && e . new ?. envId . id === env . envId . id ) {
197+ clearTimeout ( timeout ) ;
198+ subscription . dispose ( ) ;
199+ resolve ( ) ;
204200 }
205- return retrieved !== undefined && retrieved . envId . id === env . envId . id ;
206- } ,
207- 15_000 ,
208- ( ) =>
209- `Environment was not set correctly. Expected envId: ${ env . envId . id } (manager: ${ env . envId . managerId } ), last retrieved: ${ lastRetrievedId ?? 'undefined' } (manager: ${ lastRetrievedManagerId ?? 'undefined' } ) after ${ pollCount } polls` ,
210- ) ;
201+ } ) ;
202+
203+ // Set environment after subscribing so we don't miss the event
204+ api . setEnvironment ( project . uri , env ) . catch ( ( err ) => {
205+ clearTimeout ( timeout ) ;
206+ subscription . dispose ( ) ;
207+ reject ( err ) ;
208+ } ) ;
209+ } ) ;
211210
212- // Final verification
211+ // Verify getEnvironment returns the correct value now that setEnvironment has fully completed
213212 const retrievedEnv = await api . getEnvironment ( project . uri ) ;
214213 assert . ok ( retrievedEnv , 'Should get environment after setting' ) ;
215214 assert . strictEqual ( retrievedEnv . envId . id , env . envId . id , 'Retrieved environment should match set environment' ) ;
0 commit comments