Skip to content

Commit 5070c08

Browse files
author
Kartik Raj
authored
Identify base conda environments inside pyenv correctly (#18570)
* Assign env name * VSIX * Revert "VSIX" This reverts commit 7442e67. * Add tests * News entry
1 parent 84a5a04 commit 5070c08

6 files changed

Lines changed: 52 additions & 11 deletions

File tree

news/2 Fixes/18500.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Identify base conda environments inside pyenv correctly.

src/client/pythonEnvironments/base/locators/composite/resolverUtils.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
getPythonVersionFromPath,
1313
} from '../../../common/commonUtils';
1414
import { arePathsSame, getWorkspaceFolders, isParentPath } from '../../../common/externalDependencies';
15-
import { AnacondaCompanyName, Conda } from '../../../common/environmentManagers/conda';
16-
import { parsePyenvVersion } from '../../../common/environmentManagers/pyenv';
15+
import { AnacondaCompanyName, Conda, isCondaEnvironment } from '../../../common/environmentManagers/conda';
16+
import { getPyenvVersionsDir, parsePyenvVersion } from '../../../common/environmentManagers/pyenv';
1717
import { Architecture, getOSType, OSType } from '../../../../common/utils/platform';
1818
import { getPythonVersionFromPath as parsePythonVersionFromPath, parseVersion } from '../../info/pythonVersion';
1919
import { getRegistryInterpreters, getRegistryInterpretersSync } from '../../../common/windowsUtils';
@@ -195,10 +195,23 @@ async function resolvePyenvEnv(executablePath: string): Promise<PythonEnvInfo> {
195195
org: versionStrings && versionStrings.distro ? versionStrings.distro : '',
196196
});
197197

198-
envInfo.name = name;
198+
if (await isBaseCondaPyenvEnvironment(executablePath)) {
199+
envInfo.name = 'base';
200+
} else {
201+
envInfo.name = name;
202+
}
199203
return envInfo;
200204
}
201205

206+
async function isBaseCondaPyenvEnvironment(executablePath: string) {
207+
if (!(await isCondaEnvironment(executablePath))) {
208+
return false;
209+
}
210+
const location = getEnvironmentDirFromPath(executablePath);
211+
const pyenvVersionDir = getPyenvVersionsDir();
212+
return arePathsSame(path.dirname(location), pyenvVersionDir);
213+
}
214+
202215
async function resolveWindowsStoreEnv(executablePath: string): Promise<PythonEnvInfo> {
203216
return buildEnvInfo({
204217
kind: PythonEnvKind.WindowsStore,

src/client/pythonEnvironments/base/locators/lowLevel/pyenvLocator.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import * as path from 'path';
54
import { PythonEnvKind } from '../../info';
65
import { BasicEnvInfo, IPythonEnvsIterator } from '../../locator';
76
import { FSWatchingLocator } from './fsWatchingLocator';
87
import { getInterpreterPathFromDir } from '../../../common/commonUtils';
98
import { getSubDirs } from '../../../common/externalDependencies';
10-
import { getPyenvDir } from '../../../common/environmentManagers/pyenv';
9+
import { getPyenvVersionsDir } from '../../../common/environmentManagers/pyenv';
1110
import { traceError } from '../../../../logging';
1211

13-
function getPyenvVersionsDir(): string {
14-
return path.join(getPyenvDir(), 'versions');
15-
}
16-
1712
/**
1813
* Gets all the pyenv environments.
1914
*

src/client/pythonEnvironments/common/environmentManagers/pyenv.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export function getPyenvDir(): string {
1919

2020
return pyenvDir;
2121
}
22+
23+
export function getPyenvVersionsDir(): string {
24+
return path.join(getPyenvDir(), 'versions');
25+
}
26+
2227
/**
2328
* Checks if a given directory path is same as `pyenv` shims path. This checks
2429
* `~/.pyenv/shims` on posix and `~/.pyenv/pyenv-win/shims` on windows.

src/test/pythonEnvironments/base/locators/composite/resolverUtils.unit.test.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ suite('Resolver Utils', () => {
4848
teardown(() => {
4949
sinon.restore();
5050
});
51-
function getExpectedPyenvInfo(): PythonEnvInfo | undefined {
51+
function getExpectedPyenvInfo1(): PythonEnvInfo | undefined {
5252
const envInfo = buildEnvInfo({
5353
kind: PythonEnvKind.Pyenv,
5454
executable: path.join(testPyenvVersionsDir, '3.9.0', 'bin', 'python'),
@@ -65,9 +65,36 @@ suite('Resolver Utils', () => {
6565
return envInfo;
6666
}
6767

68+
function getExpectedPyenvInfo2(): PythonEnvInfo | undefined {
69+
const envInfo = buildEnvInfo({
70+
kind: PythonEnvKind.Pyenv,
71+
executable: path.join(testPyenvVersionsDir, 'miniconda3-4.7.12', 'bin', 'python'),
72+
version: {
73+
major: 3,
74+
minor: 7,
75+
micro: -1,
76+
},
77+
source: [],
78+
org: 'miniconda3',
79+
});
80+
envInfo.location = path.join(testPyenvVersionsDir, 'miniconda3-4.7.12');
81+
envInfo.name = 'base';
82+
setEnvDisplayString(envInfo);
83+
return envInfo;
84+
}
85+
6886
test('resolveEnv', async () => {
6987
const executablePath = path.join(testPyenvVersionsDir, '3.9.0', 'bin', 'python');
70-
const expected = getExpectedPyenvInfo();
88+
const expected = getExpectedPyenvInfo1();
89+
90+
const actual = await resolveBasicEnv({ executablePath, kind: PythonEnvKind.Pyenv });
91+
assertEnvEqual(actual, expected);
92+
});
93+
94+
test('resolveEnv (base conda env)', async () => {
95+
sinon.stub(platformApis, 'getOSType').callsFake(() => platformApis.OSType.Linux);
96+
const executablePath = path.join(testPyenvVersionsDir, 'miniconda3-4.7.12', 'bin', 'python');
97+
const expected = getExpectedPyenvInfo2();
7198

7299
const actual = await resolveBasicEnv({ executablePath, kind: PythonEnvKind.Pyenv });
73100
assertEnvEqual(actual, expected);

src/test/pythonEnvironments/common/envlayouts/pyenvhome/.pyenv/versions/miniconda3-4.7.12/conda-meta/history

Whitespace-only changes.

0 commit comments

Comments
 (0)