Skip to content

Commit

Permalink
Ensure an environment is only reported after the final type of enviro…
Browse files Browse the repository at this point in the history
…nment is known (#19821)

For #19101
  • Loading branch information
Kartik Raj authored Sep 14, 2022
1 parent 2f4e9a0 commit fc45b1c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
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

0 comments on commit fc45b1c

Please sign in to comment.