Skip to content

Commit 676ef37

Browse files
authored
Ensure prefix conda envs does not set name (#15859)
* Ensure prefix conda envs does not set name * Update unit tests * Add news * Add message to assert * Ensure pyvenv.cfg is created for the dummy environment * Tweak tests * Skip unknown global virtual environments * Set kind for global virtual env tests * Ensure unknowns are skipped in global virtual env resolver * Tweak tests * Remove locating when testing for updates * Restore expected environment type in tests. * Remove environment type all together * Clean up
1 parent a30900f commit 676ef37

File tree

11 files changed

+80
-62
lines changed

11 files changed

+80
-62
lines changed

news/2 Fixes/15823.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes activation of prefixed conda environments.

src/client/pythonEnvironments/base/locators/common/resourceBasedLocator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export abstract class LazyResourceBasedLocator extends Locator implements IDispo
5151
/**
5252
* The subclass implementation of resolveEnv().
5353
*/
54-
protected abstract async doResolveEnv(_env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined>;
54+
protected abstract doResolveEnv(_env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined>;
5555

5656
/**
5757
* This is where subclasses get their resources ready.

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,14 @@ export class WorkspaceVirtualEnvironmentLocator extends FSWatchingLocator {
121121
// check multiple times. Those checks are file system heavy and
122122
// we can use the kind to determine this anyway.
123123
const kind = await getVirtualEnvKind(filename);
124-
yield buildSimpleVirtualEnvInfo(filename, kind);
125-
traceVerbose(`Workspace Virtual Environment: [added] ${filename}`);
124+
125+
if (kind === PythonEnvKind.Unknown) {
126+
// We don't know the environment type so skip this one.
127+
traceVerbose(`Workspace Virtual Environment: [skipped] ${filename}`);
128+
} else {
129+
yield buildSimpleVirtualEnvInfo(filename, kind);
130+
traceVerbose(`Workspace Virtual Environment: [added] ${filename}`);
131+
}
126132
} else {
127133
traceVerbose(`Workspace Virtual Environment: [skipped] ${filename}`);
128134
}
@@ -146,6 +152,9 @@ export class WorkspaceVirtualEnvironmentLocator extends FSWatchingLocator {
146152
// check multiple times. Those checks are file system heavy and
147153
// we can use the kind to determine this anyway.
148154
const kind = await getVirtualEnvKind(executablePath);
155+
if (kind === PythonEnvKind.Unknown) {
156+
return undefined;
157+
}
149158
return buildSimpleVirtualEnvInfo(executablePath, kind, source);
150159
}
151160
return undefined;

src/client/pythonEnvironments/discovery/locators/services/condaLocator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import '../../../../common/extensions';
44
import { PythonEnvInfo, PythonEnvKind, PythonEnvSource } from '../../../base/info';
55
import { buildEnvInfo } from '../../../base/info/env';
66
import { IPythonEnvsIterator, Locator } from '../../../base/locator';
7-
import { getInterpreterPathFromDir } from '../../../common/commonUtils';
7+
import { getInterpreterPathFromDir, getPythonVersionFromPath } from '../../../common/commonUtils';
88
import { AnacondaCompanyName, Conda } from './conda';
99
import { resolveEnvFromIterator } from '../../../base/locatorUtils';
1010
import { traceVerbose } from '../../../../common/logger';
@@ -49,6 +49,7 @@ export class CondaEnvironmentLocator extends Locator {
4949
org: AnacondaCompanyName,
5050
location: prefix,
5151
source: [PythonEnvSource.Conda],
52+
version: await getPythonVersionFromPath(executable),
5253
});
5354
if (name) {
5455
info.name = name;

src/client/pythonEnvironments/discovery/locators/services/globalVirtualEnvronmentLocator.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,13 @@ export class GlobalVirtualEnvironmentLocator extends FSWatchingLocator {
134134
// check multiple times. Those checks are file system heavy and
135135
// we can use the kind to determine this anyway.
136136
const kind = await getVirtualEnvKind(filename);
137-
yield buildSimpleVirtualEnvInfo(filename, kind);
138-
traceVerbose(`Global Virtual Environment: [added] ${filename}`);
137+
if (kind === PythonEnvKind.Unknown) {
138+
// We don't know the environment type so skip this one.
139+
traceVerbose(`Global Virtual Environment: [skipped] ${filename}`);
140+
} else {
141+
yield buildSimpleVirtualEnvInfo(filename, kind);
142+
traceVerbose(`Global Virtual Environment: [added] ${filename}`);
143+
}
139144
} else {
140145
traceVerbose(`Global Virtual Environment: [skipped] ${filename}`);
141146
}
@@ -158,6 +163,10 @@ export class GlobalVirtualEnvironmentLocator extends FSWatchingLocator {
158163
// check multiple times. Those checks are file system heavy and
159164
// we can use the kind to determine this anyway.
160165
const kind = await getVirtualEnvKind(executablePath);
166+
if (kind === PythonEnvKind.Unknown) {
167+
// We don't know the environment type so skip this one.
168+
return undefined;
169+
}
161170
return buildSimpleVirtualEnvInfo(executablePath, kind);
162171
}
163172
return undefined;

src/client/pythonEnvironments/discovery/locators/services/poetryLocator.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ async function getRootVirtualEnvDir(root: string): Promise<string[]> {
5555
return rootDirs;
5656
}
5757

58-
async function getVirtualEnvKind(interpreterPath: string): Promise<PythonEnvKind> {
59-
return (await isPoetryEnvironment(interpreterPath)) ? PythonEnvKind.Poetry : PythonEnvKind.Unknown;
60-
}
61-
6258
async function buildVirtualEnvInfo(
6359
executablePath: string,
6460
kind: PythonEnvKind,
@@ -143,8 +139,7 @@ export class PoetryLocator extends FSWatchingLocator {
143139
protected async doResolveEnv(env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined> {
144140
const executablePath = typeof env === 'string' ? env : env.executable.filename;
145141
const source = typeof env === 'string' ? [PythonEnvSource.Other] : uniq([PythonEnvSource.Other, ...env.source]);
146-
const kind = await getVirtualEnvKind(executablePath);
147-
if (kind === PythonEnvKind.Poetry) {
142+
if (await isPoetryEnvironment(executablePath)) {
148143
const isLocal = isParentPath(executablePath, this.root);
149144
return buildVirtualEnvInfo(executablePath, PythonEnvKind.Poetry, source, isLocal);
150145
}

src/client/pythonEnvironments/index.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,27 +119,29 @@ async function createLocators(
119119
}
120120

121121
function createNonWorkspaceLocators(ext: ExtensionState): ILocator[] {
122-
let locators: (ILocator & Partial<IDisposable>)[];
122+
const locators: (ILocator & Partial<IDisposable>)[] = [];
123+
locators.push(
124+
// OS-independent locators go here.
125+
new PyenvLocator(),
126+
new CondaEnvironmentLocator(),
127+
new GlobalVirtualEnvironmentLocator(),
128+
new CustomVirtualEnvironmentLocator(),
129+
);
130+
123131
if (getOSType() === OSType.Windows) {
124-
locators = [
132+
locators.push(
125133
// Windows specific locators go here.
126134
new WindowsRegistryLocator(),
127135
new WindowsStoreLocator(),
128136
new WindowsPathEnvVarLocator(),
129-
];
137+
);
130138
} else {
131-
locators = [
139+
locators.push(
132140
// Linux/Mac locators go here.
133141
new PosixKnownPathsLocator(),
134-
];
142+
);
135143
}
136-
locators.push(
137-
// OS-independent locators go here.
138-
new GlobalVirtualEnvironmentLocator(),
139-
new PyenvLocator(),
140-
new CustomVirtualEnvironmentLocator(),
141-
new CondaEnvironmentLocator(),
142-
);
144+
143145
const disposables = locators.filter((d) => d.dispose !== undefined) as IDisposable[];
144146
ext.disposables.push(...disposables);
145147
return locators;

src/test/pythonEnvironments/base/locators/lowLevel/workspaceVirtualEnvLocator.testvirtualenvs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
import * as path from 'path';
5+
import { PythonEnvKind } from '../../../../../client/pythonEnvironments/base/info';
56
import { WorkspaceVirtualEnvironmentLocator } from '../../../../../client/pythonEnvironments/base/locators/lowLevel/workspaceVirtualEnvLocator';
67
import { TEST_LAYOUT_ROOT } from '../../../common/commonTestConstants';
78
import { testLocatorWatcher } from '../../../discovery/locators/watcherTestUtils';
@@ -10,5 +11,6 @@ suite('WorkspaceVirtualEnvironment Locator', async () => {
1011
const testWorkspaceFolder = path.join(TEST_LAYOUT_ROOT, 'workspace', 'folder1');
1112
testLocatorWatcher(testWorkspaceFolder, async (root?: string) => new WorkspaceVirtualEnvironmentLocator(root!), {
1213
arg: testWorkspaceFolder,
14+
kind: PythonEnvKind.Venv,
1315
});
1416
});

src/test/pythonEnvironments/base/locators/lowLevel/workspaceVirtualEnvLocator.unit.test.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,13 @@ suite('WorkspaceVirtualEnvironment Locator', () => {
122122

123123
test('iterEnvs(): Non-Windows', async () => {
124124
const expectedEnvs = [
125-
createExpectedEnvInfo(
126-
path.join(testWorkspaceFolder, 'posix2conda', 'python'),
127-
PythonEnvKind.Unknown,
128-
{ major: 3, minor: 8, micro: 5 },
129-
'posix2conda',
130-
),
131125
createExpectedEnvInfo(
132126
path.join(testWorkspaceFolder, '.direnv', 'posix1virtualenv', 'bin', 'python'),
133127
PythonEnvKind.VirtualEnv,
134128
{ major: 3, minor: 8, micro: -1 },
135129
'posix1virtualenv',
136130
path.join(testWorkspaceFolder, '.direnv', 'posix1virtualenv'),
137131
),
138-
createExpectedEnvInfo(
139-
path.join(testWorkspaceFolder, 'posix3custom', 'bin', 'python'),
140-
PythonEnvKind.Unknown,
141-
undefined,
142-
'posix3custom',
143-
),
144132
].sort((a, b) => a.executable.filename.localeCompare(b.executable.filename));
145133

146134
const iterator = locator.iterEnvs();
@@ -153,12 +141,13 @@ suite('WorkspaceVirtualEnvironment Locator', () => {
153141
});
154142

155143
test('resolveEnv(string)', async () => {
156-
const interpreterPath = path.join(testWorkspaceFolder, 'posix2conda', 'python');
144+
const interpreterPath = path.join(testWorkspaceFolder, '.direnv', 'posix1virtualenv', 'bin', 'python');
157145
const expected = createExpectedEnvInfo(
158-
path.join(testWorkspaceFolder, 'posix2conda', 'python'),
159-
PythonEnvKind.Unknown,
160-
{ major: 3, minor: 8, micro: 5 },
161-
'posix2conda',
146+
path.join(testWorkspaceFolder, '.direnv', 'posix1virtualenv', 'bin', 'python'),
147+
PythonEnvKind.VirtualEnv,
148+
{ major: 3, minor: 8, micro: -1 },
149+
'posix1virtualenv',
150+
path.join(testWorkspaceFolder, '.direnv', 'posix1virtualenv'),
162151
);
163152

164153
const actual = await locator.resolveEnv(interpreterPath);
@@ -167,12 +156,13 @@ suite('WorkspaceVirtualEnvironment Locator', () => {
167156
});
168157

169158
test('resolveEnv(PythonEnvInfo)', async () => {
170-
const interpreterPath = path.join(testWorkspaceFolder, 'posix2conda', 'python');
159+
const interpreterPath = path.join(testWorkspaceFolder, '.direnv', 'posix1virtualenv', 'bin', 'python');
171160
const expected = createExpectedEnvInfo(
172-
path.join(testWorkspaceFolder, 'posix2conda', 'python'),
173-
PythonEnvKind.Unknown,
174-
{ major: 3, minor: 8, micro: 5 },
175-
'posix2conda',
161+
path.join(testWorkspaceFolder, '.direnv', 'posix1virtualenv', 'bin', 'python'),
162+
PythonEnvKind.VirtualEnv,
163+
{ major: 3, minor: 8, micro: -1 },
164+
'posix1virtualenv',
165+
path.join(testWorkspaceFolder, '.direnv', 'posix1virtualenv'),
176166
);
177167

178168
// Partially filled in env info object

src/test/pythonEnvironments/discovery/locators/condaHelper.unit.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,13 @@ suite('Conda and its environments are located correctly', () => {
638638
display: undefined,
639639
searchLocation: undefined,
640640
distro: { org: AnacondaCompanyName },
641-
version: { major: -1, minor: -1, micro: -1, release: { level: 'final', serial: 0 } },
641+
version: {
642+
major: -1,
643+
minor: -1,
644+
micro: -1,
645+
release: { level: 'final', serial: -1 },
646+
sysVersion: undefined,
647+
},
642648
location: prefix,
643649
executable: {
644650
filename: path.join(prefix, 'bin', 'python'),

0 commit comments

Comments
 (0)