@@ -13,15 +13,22 @@ import {
1313 TriggerRefreshOptions ,
1414} from './base/locator' ;
1515import { PythonEnvCollectionChangedEvent } from './base/watcher' ;
16- import { isNativeEnvInfo , NativeEnvInfo , NativePythonFinder } from './base/locators/common/nativePythonFinder' ;
16+ import {
17+ isNativeEnvInfo ,
18+ NativeEnvInfo ,
19+ NativeEnvManagerInfo ,
20+ NativePythonFinder ,
21+ } from './base/locators/common/nativePythonFinder' ;
1722import { createDeferred , Deferred } from '../common/utils/async' ;
1823import { Architecture } from '../common/utils/platform' ;
1924import { parseVersion } from './base/info/pythonVersion' ;
2025import { cache } from '../common/utils/decorators' ;
21- import { traceError , traceLog } from '../logging' ;
26+ import { traceError , traceLog , traceWarn } from '../logging' ;
2227import { StopWatch } from '../common/utils/stopWatch' ;
2328import { FileChangeType } from '../common/platform/fileSystemWatcher' ;
2429import { categoryToKind } from './base/locators/common/nativePythonUtils' ;
30+ import { setCondaBinary } from './common/environmentManagers/conda' ;
31+ import { setPyEnvBinary } from './common/environmentManagers/pyenv' ;
2532
2633function makeExecutablePath ( prefix ?: string ) : string {
2734 if ( ! prefix ) {
@@ -232,44 +239,10 @@ class NativePythonEnvironments implements IDiscoveryAPI, Disposable {
232239 setImmediate ( async ( ) => {
233240 try {
234241 for await ( const native of this . finder . refresh ( ) ) {
235- if ( ! isNativeEnvInfo ( native ) || ! validEnv ( native ) ) {
236- // eslint-disable-next-line no-continue
237- continue ;
238- }
239- try {
240- const envPath = native . executable ?? native . prefix ;
241- const version = native . version ? parseVersion ( native . version ) : undefined ;
242-
243- if ( categoryToKind ( native . kind ) === PythonEnvKind . Conda && ! native . executable ) {
244- // This is a conda env without python, no point trying to resolve this.
245- // There is nothing to resolve
246- this . addEnv ( native ) ;
247- } else if (
248- envPath &&
249- ( ! version || version . major < 0 || version . minor < 0 || version . micro < 0 )
250- ) {
251- // We have a path, but no version info, try to resolve the environment.
252- this . finder
253- . resolve ( envPath )
254- . then ( ( env ) => {
255- if ( env ) {
256- this . addEnv ( env ) ;
257- }
258- } )
259- . ignoreErrors ( ) ;
260- } else if (
261- envPath &&
262- version &&
263- version . major >= 0 &&
264- version . minor >= 0 &&
265- version . micro >= 0
266- ) {
267- this . addEnv ( native ) ;
268- } else {
269- traceError ( `Failed to process environment: ${ JSON . stringify ( native ) } ` ) ;
270- }
271- } catch ( err ) {
272- traceError ( `Failed to process environment: ${ err } ` ) ;
242+ if ( isNativeEnvInfo ( native ) ) {
243+ this . processEnv ( native ) ;
244+ } else {
245+ this . processEnvManager ( native ) ;
273246 }
274247 }
275248 this . _refreshPromise ?. resolve ( ) ;
@@ -286,6 +259,57 @@ class NativePythonEnvironments implements IDiscoveryAPI, Disposable {
286259 return this . _refreshPromise ?. promise ;
287260 }
288261
262+ private processEnv ( native : NativeEnvInfo ) : void {
263+ if ( ! validEnv ( native ) ) {
264+ return ;
265+ }
266+
267+ try {
268+ const envPath = native . executable ?? native . prefix ;
269+ const version = native . version ? parseVersion ( native . version ) : undefined ;
270+
271+ if ( categoryToKind ( native . kind ) === PythonEnvKind . Conda && ! native . executable ) {
272+ // This is a conda env without python, no point trying to resolve this.
273+ // There is nothing to resolve
274+ this . addEnv ( native ) ;
275+ } else if ( envPath && ( ! version || version . major < 0 || version . minor < 0 || version . micro < 0 ) ) {
276+ // We have a path, but no version info, try to resolve the environment.
277+ this . finder
278+ . resolve ( envPath )
279+ . then ( ( env ) => {
280+ if ( env ) {
281+ this . addEnv ( env ) ;
282+ }
283+ } )
284+ . ignoreErrors ( ) ;
285+ } else if ( envPath && version && version . major >= 0 && version . minor >= 0 && version . micro >= 0 ) {
286+ this . addEnv ( native ) ;
287+ } else {
288+ traceError ( `Failed to process environment: ${ JSON . stringify ( native ) } ` ) ;
289+ }
290+ } catch ( err ) {
291+ traceError ( `Failed to process environment: ${ err } ` ) ;
292+ }
293+ }
294+
295+ // eslint-disable-next-line class-methods-use-this
296+ private processEnvManager ( native : NativeEnvManagerInfo ) {
297+ const tool = native . tool . toLowerCase ( ) ;
298+ switch ( tool ) {
299+ case 'conda' :
300+ traceLog ( `Conda environment manager found at: ${ native . executable } ` ) ;
301+ setCondaBinary ( native . executable ) ;
302+ break ;
303+ case 'pyenv' :
304+ traceLog ( `Pyenv environment manager found at: ${ native . executable } ` ) ;
305+ setPyEnvBinary ( native . executable ) ;
306+ break ;
307+ default :
308+ traceWarn ( `Unknown environment manager: ${ native . tool } ` ) ;
309+ break ;
310+ }
311+ }
312+
289313 getEnvs ( _query ?: PythonLocatorQuery ) : PythonEnvInfo [ ] {
290314 return this . _envs ;
291315 }
0 commit comments