@@ -23,11 +23,11 @@ import { createDeferred, Deferred } from '../common/utils/async';
23
23
import { Architecture , getUserHomeDir } from '../common/utils/platform' ;
24
24
import { parseVersion } from './base/info/pythonVersion' ;
25
25
import { cache } from '../common/utils/decorators' ;
26
- import { traceError , traceLog , traceWarn } from '../logging' ;
26
+ import { traceError , traceInfo , traceLog , traceWarn } from '../logging' ;
27
27
import { StopWatch } from '../common/utils/stopWatch' ;
28
28
import { FileChangeType } from '../common/platform/fileSystemWatcher' ;
29
29
import { categoryToKind , NativePythonEnvironmentKind } from './base/locators/common/nativePythonUtils' ;
30
- import { setCondaBinary } from './common/environmentManagers/conda' ;
30
+ import { getCondaEnvDirs , setCondaBinary } from './common/environmentManagers/conda' ;
31
31
import { setPyEnvBinary } from './common/environmentManagers/pyenv' ;
32
32
import {
33
33
createPythonWatcher ,
@@ -157,26 +157,53 @@ function getEnvType(kind: PythonEnvKind): PythonEnvType | undefined {
157
157
}
158
158
}
159
159
160
- function getName ( nativeEnv : NativeEnvInfo , kind : PythonEnvKind ) : string {
160
+ function isSubDir ( pathToCheck : string | undefined , parents : string [ ] ) : boolean {
161
+ return parents . some ( ( prefix ) => {
162
+ if ( pathToCheck ) {
163
+ return path . normalize ( pathToCheck ) . startsWith ( path . normalize ( prefix ) ) ;
164
+ }
165
+ return false ;
166
+ } ) ;
167
+ }
168
+
169
+ function getName ( nativeEnv : NativeEnvInfo , kind : PythonEnvKind , condaEnvDirs : string [ ] ) : string {
161
170
if ( nativeEnv . name ) {
162
171
return nativeEnv . name ;
163
172
}
164
173
165
174
const envType = getEnvType ( kind ) ;
166
- if ( nativeEnv . prefix && ( envType === PythonEnvType . Conda || envType === PythonEnvType . Virtual ) ) {
175
+ if ( nativeEnv . prefix && envType === PythonEnvType . Virtual ) {
167
176
return path . basename ( nativeEnv . prefix ) ;
168
177
}
178
+
179
+ if ( nativeEnv . prefix && envType === PythonEnvType . Conda ) {
180
+ if ( nativeEnv . name === 'base' ) {
181
+ return 'base' ;
182
+ }
183
+
184
+ const workspaces = ( getWorkspaceFolders ( ) ?? [ ] ) . map ( ( wf ) => wf . uri . fsPath ) ;
185
+ if ( isSubDir ( nativeEnv . prefix , workspaces ) ) {
186
+ traceInfo ( `Conda env is --prefix environment: ${ nativeEnv . prefix } ` ) ;
187
+ return '' ;
188
+ }
189
+
190
+ if ( condaEnvDirs . length > 0 && isSubDir ( nativeEnv . prefix , condaEnvDirs ) ) {
191
+ traceInfo ( `Conda env is --named environment: ${ nativeEnv . prefix } ` ) ;
192
+ return path . basename ( nativeEnv . prefix ) ;
193
+ }
194
+ }
195
+
169
196
return '' ;
170
197
}
171
198
172
- function toPythonEnvInfo ( nativeEnv : NativeEnvInfo ) : PythonEnvInfo | undefined {
199
+ function toPythonEnvInfo ( nativeEnv : NativeEnvInfo , condaEnvDirs : string [ ] ) : PythonEnvInfo | undefined {
173
200
if ( ! validEnv ( nativeEnv ) ) {
174
201
return undefined ;
175
202
}
176
203
const kind = categoryToKind ( nativeEnv . kind ) ;
177
204
const arch = toArch ( nativeEnv . arch ) ;
178
205
const version : PythonVersion = parseVersion ( nativeEnv . version ?? '' ) ;
179
- const name = getName ( nativeEnv , kind ) ;
206
+ const name = getName ( nativeEnv , kind , condaEnvDirs ) ;
180
207
const displayName = nativeEnv . version
181
208
? getDisplayName ( version , kind , arch , name )
182
209
: nativeEnv . displayName ?? 'Python' ;
@@ -211,6 +238,9 @@ function toPythonEnvInfo(nativeEnv: NativeEnvInfo): PythonEnvInfo | undefined {
211
238
}
212
239
213
240
function hasChanged ( old : PythonEnvInfo , newEnv : PythonEnvInfo ) : boolean {
241
+ if ( old . name !== newEnv . name ) {
242
+ return true ;
243
+ }
214
244
if ( old . executable . filename !== newEnv . executable . filename ) {
215
245
return true ;
216
246
}
@@ -247,6 +277,8 @@ class NativePythonEnvironments implements IDiscoveryAPI, Disposable {
247
277
248
278
private _disposables : Disposable [ ] = [ ] ;
249
279
280
+ private _condaEnvDirs : string [ ] = [ ] ;
281
+
250
282
constructor ( private readonly finder : NativePythonFinder ) {
251
283
this . _onProgress = new EventEmitter < ProgressNotificationEvent > ( ) ;
252
284
this . _onChanged = new EventEmitter < PythonEnvCollectionChangedEvent > ( ) ;
@@ -381,7 +413,7 @@ class NativePythonEnvironments implements IDiscoveryAPI, Disposable {
381
413
}
382
414
383
415
private addEnv ( native : NativeEnvInfo , searchLocation ?: Uri ) : PythonEnvInfo | undefined {
384
- const info = toPythonEnvInfo ( native ) ;
416
+ const info = toPythonEnvInfo ( native , this . _condaEnvDirs ) ;
385
417
if ( info ) {
386
418
const old = this . _envs . find ( ( item ) => item . executable . filename === info . executable . filename ) ;
387
419
if ( old ) {
@@ -417,6 +449,9 @@ class NativePythonEnvironments implements IDiscoveryAPI, Disposable {
417
449
}
418
450
const native = await this . finder . resolve ( envPath ) ;
419
451
if ( native ) {
452
+ if ( native . kind === NativePythonEnvironmentKind . Conda && this . _condaEnvDirs . length === 0 ) {
453
+ this . _condaEnvDirs = ( await getCondaEnvDirs ( ) ) ?? [ ] ;
454
+ }
420
455
return this . addEnv ( native ) ;
421
456
}
422
457
return undefined ;
0 commit comments