1+ import * as path from 'path' ;
12import { EventEmitter , LogOutputChannel , MarkdownString , ProgressLocation , ThemeIcon , Uri , window } from 'vscode' ;
23
34import {
@@ -12,11 +13,13 @@ import {
1213 IconPath ,
1314 PythonEnvironment ,
1415 PythonEnvironmentApi ,
16+ PythonProject ,
1517 QuickCreateConfig ,
1618 RefreshEnvironmentsScope ,
1719 ResolveEnvironmentContext ,
1820 SetEnvironmentScope ,
1921} from '../api' ;
22+ import { getDefaultInterpreterPath } from '../common/defaultInterpreter' ;
2023import { createDeferred , Deferred } from '../common/deferred' ;
2124import { traceVerbose } from '../common/logging' ;
2225import { EXTENSION_ID } from '../common/utils' ;
@@ -140,7 +143,7 @@ export class PixiEnvManager implements EnvironmentManager {
140143 return this . projectToEnvs . get ( project . uri . fsPath ) || [ ] ;
141144 }
142145
143- // Skip 'global'. Pixi does not have the notion of global or base environments like Conda
146+ // Skip 'global'. Pixi does not have global or base environments like Conda
144147 return [ ] ;
145148 }
146149
@@ -227,6 +230,9 @@ export class PixiEnvManager implements EnvironmentManager {
227230 const projectPath = project . uri . fsPath ;
228231
229232 const newEnvs = await refreshPixi ( projectPath ) ;
233+ const defaultEnvs = await this . getDefaultPathEnvironments ( project ) ;
234+ newEnvs . push ( ...defaultEnvs ) ;
235+
230236 const oldEnvs = oldProjectToEnvs . get ( projectPath ) || [ ] ;
231237
232238 const oldEnvIds = new Set ( oldEnvs . map ( ( env ) => env . envId . id ) ) ;
@@ -264,9 +270,12 @@ export class PixiEnvManager implements EnvironmentManager {
264270 const projectPath = project . uri . fsPath ;
265271
266272 const envId = await getProjectEnvId ( projectPath ) ;
267- const env = envId ? envIdToEnv . get ( envId ) : undefined ;
273+ let env = envId ? envIdToEnv . get ( envId ) : undefined ;
274+
268275 if ( env ) {
269276 this . activeEnv . set ( projectPath , env ) ;
277+ } else {
278+ env = await this . trySetActiveFromDefault ( project , projectPath ) ;
270279 }
271280
272281 this . triggerDidChangeEnvironment ( project . uri , oldActiveEnv . get ( projectPath ) , env ) ;
@@ -285,6 +294,8 @@ export class PixiEnvManager implements EnvironmentManager {
285294
286295 const oldEnvs = this . projectToEnvs . get ( projectPath ) || [ ] ;
287296 const newEnvs = await refreshPixi ( projectPath ) ;
297+ const defaultEnvs = await this . getDefaultPathEnvironments ( project ) ;
298+ newEnvs . push ( ...defaultEnvs ) ;
288299
289300 const oldEnvIds = new Set ( oldEnvs . map ( ( env ) => env . envId . id ) ) ;
290301 const newEnvIds = new Set ( newEnvs . map ( ( env ) => env . envId . id ) ) ;
@@ -309,11 +320,51 @@ export class PixiEnvManager implements EnvironmentManager {
309320 ) ;
310321
311322 const envId = await getProjectEnvId ( projectPath ) ;
312- const env = envId ? envIdToEnv . get ( envId ) : undefined ;
323+ let env = envId ? envIdToEnv . get ( envId ) : undefined ;
313324 this . triggerDidChangeEnvironment ( project . uri , this . activeEnv . get ( projectPath ) , env ) ;
325+
314326 if ( env ) {
315327 this . activeEnv . set ( projectPath , env ) ;
328+ } else {
329+ env = await this . trySetActiveFromDefault ( project , projectPath ) ;
330+ }
331+ }
332+
333+ private async getDefaultPathEnvironments ( project : PythonProject ) : Promise < PixiEnvironment [ ] > {
334+ const defaultInterpreterPath = getDefaultInterpreterPath ( project ) ;
335+
336+ if ( ! defaultInterpreterPath ) {
337+ return [ ] ;
316338 }
339+
340+ const binPath = path . dirname ( defaultInterpreterPath ) ;
341+ traceVerbose ( `Also refreshing Pixi environments using defaultInterpreterPath: ${ binPath } ` ) ;
342+ return await refreshPixi ( binPath ) ;
343+ }
344+
345+ private async trySetActiveFromDefault (
346+ project : PythonProject ,
347+ projectPath : string ,
348+ ) : Promise < PixiEnvironment | undefined > {
349+ const defaultInterpreterPath = getDefaultInterpreterPath ( project ) ;
350+
351+ if ( defaultInterpreterPath ) {
352+ const projectEnvs = this . projectToEnvs . get ( projectPath ) || [ ] ;
353+ const matchingEnv = projectEnvs . find ( ( env ) =>
354+ defaultInterpreterPath . startsWith ( env . environmentPath . fsPath ) ,
355+ ) ;
356+
357+ if ( matchingEnv ) {
358+ traceVerbose (
359+ `Setting active environment for project ${ projectPath } based on default interpreter path ${ defaultInterpreterPath } ` ,
360+ ) ;
361+ this . activeEnv . set ( projectPath , matchingEnv ) ;
362+ await setProjectEnvId ( projectPath , matchingEnv . envId . id ) ;
363+ return matchingEnv ;
364+ }
365+ }
366+
367+ return undefined ;
317368 }
318369
319370 private triggerDidChangeEnvironment (
0 commit comments