Skip to content

Commit ed63835

Browse files
author
Kartik Raj
committed
Ensure an environment is only reported after the final type of environment is known
1 parent 00316f3 commit ed63835

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { cloneDeep } from 'lodash';
55
import { Event, EventEmitter } from 'vscode';
66
import { identifyEnvironment } from '../../../common/environmentIdentifier';
77
import { IEnvironmentInfoService } from '../../info/environmentInfoService';
8-
import { PythonEnvInfo } from '../../info';
8+
import { PythonEnvInfo, PythonEnvKind } from '../../info';
99
import { getEnvPath, setEnvDisplayString } from '../../info/env';
1010
import { InterpreterInformation } from '../../info/interpreter';
1111
import {
@@ -63,6 +63,7 @@ export class PythonEnvsResolver implements IResolvingLocator {
6363
iterator: IPythonEnvsIterator<BasicEnvInfo>,
6464
didUpdate: EventEmitter<PythonEnvUpdatedEvent | ProgressNotificationEvent>,
6565
): IPythonEnvsIterator {
66+
const environmentKinds = new Map<string, PythonEnvKind>();
6667
const state = {
6768
done: false,
6869
pending: 0,
@@ -86,6 +87,7 @@ export class PythonEnvsResolver implements IResolvingLocator {
8687
);
8788
} else if (seen[event.index] !== undefined) {
8889
const old = seen[event.index];
90+
await setKind(event.update, environmentKinds);
8991
seen[event.index] = await resolveBasicEnv(event.update, true);
9092
didUpdate.fire({ old, index: event.index, update: seen[event.index] });
9193
this.resolveInBackground(event.index, state, didUpdate, seen).ignoreErrors();
@@ -103,6 +105,7 @@ export class PythonEnvsResolver implements IResolvingLocator {
103105
let result = await iterator.next();
104106
while (!result.done) {
105107
// Use cache from the current refresh where possible.
108+
await setKind(result.value, environmentKinds);
106109
const currEnv = await resolveBasicEnv(result.value, true);
107110
seen.push(currEnv);
108111
yield currEnv;
@@ -139,6 +142,16 @@ export class PythonEnvsResolver implements IResolvingLocator {
139142
}
140143
}
141144

145+
async function setKind(env: BasicEnvInfo, environmentKinds: Map<string, PythonEnvKind>) {
146+
const { path } = getEnvPath(env.executablePath, env.envPath);
147+
let kind = environmentKinds.get(path);
148+
if (!kind) {
149+
kind = await identifyEnvironment(path);
150+
environmentKinds.set(path, kind);
151+
}
152+
env.kind = kind;
153+
}
154+
142155
/**
143156
* When all info from incoming iterator has been received and all background calls finishes, notify that we're done
144157
* @param state Carries the current state of progress

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,16 @@ suite('Python envs locator - Environments Resolver', () => {
193193
test('Updates to environments from the incoming iterator are applied properly', async () => {
194194
// Arrange
195195
const env = createBasicEnv(
196-
PythonEnvKind.Venv,
196+
PythonEnvKind.Unknown,
197197
path.join(testVirtualHomeDir, '.venvs', 'win1', 'python.exe'),
198198
);
199199
const updatedEnv = createBasicEnv(
200-
PythonEnvKind.Poetry,
200+
PythonEnvKind.VirtualEnv, // Ensure this type is discarded.
201201
path.join(testVirtualHomeDir, '.venvs', 'win1', 'python.exe'),
202202
);
203203
const resolvedUpdatedEnvReturnedByBasicResolver = createExpectedResolvedEnvInfo(
204204
path.join(testVirtualHomeDir, '.venvs', 'win1', 'python.exe'),
205-
PythonEnvKind.Poetry,
205+
PythonEnvKind.Venv,
206206
undefined,
207207
'win1',
208208
path.join(testVirtualHomeDir, '.venvs', 'win1'),
@@ -225,7 +225,7 @@ suite('Python envs locator - Environments Resolver', () => {
225225

226226
// Assert
227227
assertEnvsEqual(envs, [
228-
createExpectedEnvInfo(resolvedUpdatedEnvReturnedByBasicResolver, "Python 3.8.3 ('win1': poetry)"),
228+
createExpectedEnvInfo(resolvedUpdatedEnvReturnedByBasicResolver, "Python 3.8.3 ('win1': venv)"),
229229
]);
230230
didUpdate.dispose();
231231
});

0 commit comments

Comments
 (0)