From fc45b1c604cd30c27039bf2addf02949ccaf5bba Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Tue, 13 Sep 2022 17:31:24 -0700 Subject: [PATCH] Ensure an environment is only reported after the final type of environment is known (#19821) For https://github.com/microsoft/vscode-python/issues/19101 --- package.nls.json | 2 +- .../base/locators/composite/envsResolver.ts | 15 ++++++++++++++- .../locators/composite/envsResolver.unit.test.ts | 8 ++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/package.nls.json b/package.nls.json index 54cb7a7fd9a4..611c98ed85e2 100644 --- a/package.nls.json +++ b/package.nls.json @@ -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", diff --git a/src/client/pythonEnvironments/base/locators/composite/envsResolver.ts b/src/client/pythonEnvironments/base/locators/composite/envsResolver.ts index 407d2fe12172..1baa3f36c993 100644 --- a/src/client/pythonEnvironments/base/locators/composite/envsResolver.ts +++ b/src/client/pythonEnvironments/base/locators/composite/envsResolver.ts @@ -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 { @@ -63,6 +63,7 @@ export class PythonEnvsResolver implements IResolvingLocator { iterator: IPythonEnvsIterator, didUpdate: EventEmitter, ): IPythonEnvsIterator { + const environmentKinds = new Map(); const state = { done: false, pending: 0, @@ -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(); @@ -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; @@ -139,6 +142,16 @@ export class PythonEnvsResolver implements IResolvingLocator { } } +async function setKind(env: BasicEnvInfo, environmentKinds: Map) { + 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 diff --git a/src/test/pythonEnvironments/base/locators/composite/envsResolver.unit.test.ts b/src/test/pythonEnvironments/base/locators/composite/envsResolver.unit.test.ts index 6cd6d53330a5..158dab04e875 100644 --- a/src/test/pythonEnvironments/base/locators/composite/envsResolver.unit.test.ts +++ b/src/test/pythonEnvironments/base/locators/composite/envsResolver.unit.test.ts @@ -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'), @@ -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(); });