Skip to content

Ensure an environment is only reported after the final type of environment is known #19821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"python.command.python.enableLinting.title": "Enable/Disable Linting",
"python.command.python.runLinting.title": "Run Linting",
"python.command.python.enableSourceMapSupport.title": "Enable Source Map Support For Extension Debugging",
"python.command.python.clearCacheAndReload.title": "Clear Internal Cache and Reload Window",
"python.command.python.clearCacheAndReload.title": "Clear Cache and Reload Window",
"python.command.python.analysis.restartLanguageServer.title": "Restart Language Server",
"python.command.python.launchTensorBoard.title": "Launch TensorBoard",
"python.command.python.refreshTensorBoard.title": "Refresh TensorBoard",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { cloneDeep } from 'lodash';
import { Event, EventEmitter } from 'vscode';
import { identifyEnvironment } from '../../../common/environmentIdentifier';
import { IEnvironmentInfoService } from '../../info/environmentInfoService';
import { PythonEnvInfo } from '../../info';
import { PythonEnvInfo, PythonEnvKind } from '../../info';
import { getEnvPath, setEnvDisplayString } from '../../info/env';
import { InterpreterInformation } from '../../info/interpreter';
import {
Expand Down Expand Up @@ -63,6 +63,7 @@ export class PythonEnvsResolver implements IResolvingLocator {
iterator: IPythonEnvsIterator<BasicEnvInfo>,
didUpdate: EventEmitter<PythonEnvUpdatedEvent | ProgressNotificationEvent>,
): IPythonEnvsIterator {
const environmentKinds = new Map<string, PythonEnvKind>();
const state = {
done: false,
pending: 0,
Expand All @@ -86,6 +87,7 @@ export class PythonEnvsResolver implements IResolvingLocator {
);
} else if (seen[event.index] !== undefined) {
const old = seen[event.index];
await setKind(event.update, environmentKinds);
seen[event.index] = await resolveBasicEnv(event.update, true);
didUpdate.fire({ old, index: event.index, update: seen[event.index] });
this.resolveInBackground(event.index, state, didUpdate, seen).ignoreErrors();
Expand All @@ -103,6 +105,7 @@ export class PythonEnvsResolver implements IResolvingLocator {
let result = await iterator.next();
while (!result.done) {
// Use cache from the current refresh where possible.
await setKind(result.value, environmentKinds);
const currEnv = await resolveBasicEnv(result.value, true);
seen.push(currEnv);
yield currEnv;
Expand Down Expand Up @@ -139,6 +142,16 @@ export class PythonEnvsResolver implements IResolvingLocator {
}
}

async function setKind(env: BasicEnvInfo, environmentKinds: Map<string, PythonEnvKind>) {
const { path } = getEnvPath(env.executablePath, env.envPath);
let kind = environmentKinds.get(path);
if (!kind) {
kind = await identifyEnvironment(path);
environmentKinds.set(path, kind);
}
env.kind = kind;
}

/**
* When all info from incoming iterator has been received and all background calls finishes, notify that we're done
* @param state Carries the current state of progress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,16 @@ suite('Python envs locator - Environments Resolver', () => {
test('Updates to environments from the incoming iterator are applied properly', async () => {
// Arrange
const env = createBasicEnv(
PythonEnvKind.Venv,
PythonEnvKind.Unknown,
path.join(testVirtualHomeDir, '.venvs', 'win1', 'python.exe'),
);
const updatedEnv = createBasicEnv(
PythonEnvKind.Poetry,
PythonEnvKind.VirtualEnv, // Ensure this type is discarded.
path.join(testVirtualHomeDir, '.venvs', 'win1', 'python.exe'),
);
const resolvedUpdatedEnvReturnedByBasicResolver = createExpectedResolvedEnvInfo(
path.join(testVirtualHomeDir, '.venvs', 'win1', 'python.exe'),
PythonEnvKind.Poetry,
PythonEnvKind.Venv,
undefined,
'win1',
path.join(testVirtualHomeDir, '.venvs', 'win1'),
Expand All @@ -225,7 +225,7 @@ suite('Python envs locator - Environments Resolver', () => {

// Assert
assertEnvsEqual(envs, [
createExpectedEnvInfo(resolvedUpdatedEnvReturnedByBasicResolver, "Python 3.8.3 ('win1': poetry)"),
createExpectedEnvInfo(resolvedUpdatedEnvReturnedByBasicResolver, "Python 3.8.3 ('win1': venv)"),
]);
didUpdate.dispose();
});
Expand Down