@@ -27,7 +27,7 @@ import { traceError, traceLog, traceWarn } from '../logging';
2727import { StopWatch } from '../common/utils/stopWatch' ;
2828import { FileChangeType } from '../common/platform/fileSystemWatcher' ;
2929import { categoryToKind , NativePythonEnvironmentKind } from './base/locators/common/nativePythonUtils' ;
30- import { setCondaBinary } from './common/environmentManagers/conda' ;
30+ import { getCondaEnvDirs , setCondaBinary } from './common/environmentManagers/conda' ;
3131import { setPyEnvBinary } from './common/environmentManagers/pyenv' ;
3232import {
3333 createPythonWatcher ,
@@ -157,26 +157,40 @@ function getEnvType(kind: PythonEnvKind): PythonEnvType | undefined {
157157 }
158158}
159159
160- function getName ( nativeEnv : NativeEnvInfo , kind : PythonEnvKind ) : string {
160+ function getName ( nativeEnv : NativeEnvInfo , kind : PythonEnvKind , condaEnvDirs : string [ ] ) : string {
161161 if ( nativeEnv . name ) {
162162 return nativeEnv . name ;
163163 }
164164
165165 const envType = getEnvType ( kind ) ;
166- if ( nativeEnv . prefix && ( envType === PythonEnvType . Conda || envType === PythonEnvType . Virtual ) ) {
166+ if ( nativeEnv . prefix && envType === PythonEnvType . Virtual ) {
167167 return path . basename ( nativeEnv . prefix ) ;
168168 }
169+
170+ if ( nativeEnv . prefix && envType === PythonEnvType . Conda ) {
171+ if (
172+ condaEnvDirs . some ( ( dir ) => {
173+ if ( nativeEnv . prefix ) {
174+ return path . normalize ( nativeEnv . prefix ) . startsWith ( path . normalize ( dir ) ) ;
175+ }
176+ return false ;
177+ } )
178+ ) {
179+ return path . basename ( nativeEnv . prefix ) ;
180+ }
181+ }
182+
169183 return '' ;
170184}
171185
172- function toPythonEnvInfo ( nativeEnv : NativeEnvInfo ) : PythonEnvInfo | undefined {
186+ function toPythonEnvInfo ( nativeEnv : NativeEnvInfo , condaEnvDirs : string [ ] ) : PythonEnvInfo | undefined {
173187 if ( ! validEnv ( nativeEnv ) ) {
174188 return undefined ;
175189 }
176190 const kind = categoryToKind ( nativeEnv . kind ) ;
177191 const arch = toArch ( nativeEnv . arch ) ;
178192 const version : PythonVersion = parseVersion ( nativeEnv . version ?? '' ) ;
179- const name = getName ( nativeEnv , kind ) ;
193+ const name = getName ( nativeEnv , kind , condaEnvDirs ) ;
180194 const displayName = nativeEnv . version
181195 ? getDisplayName ( version , kind , arch , name )
182196 : nativeEnv . displayName ?? 'Python' ;
@@ -211,6 +225,9 @@ function toPythonEnvInfo(nativeEnv: NativeEnvInfo): PythonEnvInfo | undefined {
211225}
212226
213227function hasChanged ( old : PythonEnvInfo , newEnv : PythonEnvInfo ) : boolean {
228+ if ( old . name !== newEnv . name ) {
229+ return true ;
230+ }
214231 if ( old . executable . filename !== newEnv . executable . filename ) {
215232 return true ;
216233 }
@@ -247,6 +264,8 @@ class NativePythonEnvironments implements IDiscoveryAPI, Disposable {
247264
248265 private _disposables : Disposable [ ] = [ ] ;
249266
267+ private _condaEnvDirs : string [ ] = [ ] ;
268+
250269 constructor ( private readonly finder : NativePythonFinder ) {
251270 this . _onProgress = new EventEmitter < ProgressNotificationEvent > ( ) ;
252271 this . _onChanged = new EventEmitter < PythonEnvCollectionChangedEvent > ( ) ;
@@ -381,7 +400,7 @@ class NativePythonEnvironments implements IDiscoveryAPI, Disposable {
381400 }
382401
383402 private addEnv ( native : NativeEnvInfo , searchLocation ?: Uri ) : PythonEnvInfo | undefined {
384- const info = toPythonEnvInfo ( native ) ;
403+ const info = toPythonEnvInfo ( native , this . _condaEnvDirs ) ;
385404 if ( info ) {
386405 const old = this . _envs . find ( ( item ) => item . executable . filename === info . executable . filename ) ;
387406 if ( old ) {
@@ -417,6 +436,9 @@ class NativePythonEnvironments implements IDiscoveryAPI, Disposable {
417436 }
418437 const native = await this . finder . resolve ( envPath ) ;
419438 if ( native ) {
439+ if ( native . kind === NativePythonEnvironmentKind . Conda && this . _condaEnvDirs . length === 0 ) {
440+ this . _condaEnvDirs = ( await getCondaEnvDirs ( ) ) ?? [ ] ;
441+ }
420442 return this . addEnv ( native ) ;
421443 }
422444 return undefined ;
0 commit comments