From 6b377d9d6b4acff9609e499738fbac5985a00a98 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Sun, 17 Mar 2024 01:14:30 -0500 Subject: [PATCH] If type identifier for an environment is not registered, infer type from locator (microsoft/vscode-python#23083) For https://github.com/microsoft/vscode-python/issues/22810 ...so in case of Hatch, as an [explicit Hatch identifier is not available](https://github.com/microsoft/vscode-python/pull/22779#discussion_r1483341191), we infer an environment is Hatch if we get it from the Hatch locator: https://github.com/microsoft/vscode-python/pull/22779. --- extensions/positron-python/src/client/api/types.ts | 1 + .../positron-python/src/client/environmentApi.ts | 2 ++ .../configuration/environmentTypeComparer.ts | 1 + .../interpreterSelector/commands/setInterpreter.ts | 1 + .../base/locators/composite/envsResolver.ts | 6 +++++- .../common/environmentIdentifier.ts | 12 +++++++++++- .../src/client/pythonEnvironments/info/index.ts | 5 +++++ .../src/client/pythonEnvironments/legacyIOC.ts | 1 + 8 files changed, 27 insertions(+), 2 deletions(-) diff --git a/extensions/positron-python/src/client/api/types.ts b/extensions/positron-python/src/client/api/types.ts index dccbd78f6f0..873d5e802d5 100644 --- a/extensions/positron-python/src/client/api/types.ts +++ b/extensions/positron-python/src/client/api/types.ts @@ -268,6 +268,7 @@ export type KnownEnvironmentTools = | 'Venv' | 'VirtualEnvWrapper' | 'Pyenv' + | 'Hatch' | 'Unknown'; /** diff --git a/extensions/positron-python/src/client/environmentApi.ts b/extensions/positron-python/src/client/environmentApi.ts index 65430920d01..cf70e188981 100644 --- a/extensions/positron-python/src/client/environmentApi.ts +++ b/extensions/positron-python/src/client/environmentApi.ts @@ -338,6 +338,8 @@ function convertKind(kind: PythonEnvKind): EnvironmentTools | undefined { return 'Pipenv'; case PythonEnvKind.Poetry: return 'Poetry'; + case PythonEnvKind.Hatch: + return 'Hatch'; case PythonEnvKind.VirtualEnvWrapper: return 'VirtualEnvWrapper'; case PythonEnvKind.VirtualEnv: diff --git a/extensions/positron-python/src/client/interpreter/configuration/environmentTypeComparer.ts b/extensions/positron-python/src/client/interpreter/configuration/environmentTypeComparer.ts index 053f27d3a80..f0cc8f36237 100644 --- a/extensions/positron-python/src/client/interpreter/configuration/environmentTypeComparer.ts +++ b/extensions/positron-python/src/client/interpreter/configuration/environmentTypeComparer.ts @@ -281,6 +281,7 @@ function getPrioritizedEnvironmentType(): EnvironmentType[] { EnvironmentType.Poetry, EnvironmentType.Pipenv, EnvironmentType.VirtualEnvWrapper, + EnvironmentType.Hatch, EnvironmentType.Venv, EnvironmentType.VirtualEnv, EnvironmentType.ActiveState, diff --git a/extensions/positron-python/src/client/interpreter/configuration/interpreterSelector/commands/setInterpreter.ts b/extensions/positron-python/src/client/interpreter/configuration/interpreterSelector/commands/setInterpreter.ts index 5487f459a7a..5d01cbaafb7 100644 --- a/extensions/positron-python/src/client/interpreter/configuration/interpreterSelector/commands/setInterpreter.ts +++ b/extensions/positron-python/src/client/interpreter/configuration/interpreterSelector/commands/setInterpreter.ts @@ -74,6 +74,7 @@ export namespace EnvGroups { export const Pyenv = 'Pyenv'; export const Venv = 'Venv'; export const Poetry = 'Poetry'; + export const Hatch = 'Hatch'; export const VirtualEnvWrapper = 'VirtualEnvWrapper'; export const ActiveState = 'ActiveState'; export const Recommended = Common.recommended; diff --git a/extensions/positron-python/src/client/pythonEnvironments/base/locators/composite/envsResolver.ts b/extensions/positron-python/src/client/pythonEnvironments/base/locators/composite/envsResolver.ts index fa998557deb..a7a25cc41c5 100644 --- a/extensions/positron-python/src/client/pythonEnvironments/base/locators/composite/envsResolver.ts +++ b/extensions/positron-python/src/client/pythonEnvironments/base/locators/composite/envsResolver.ts @@ -3,7 +3,7 @@ import { cloneDeep } from 'lodash'; import { Event, EventEmitter } from 'vscode'; -import { identifyEnvironment } from '../../../common/environmentIdentifier'; +import { isIdentifierRegistered, identifyEnvironment } from '../../../common/environmentIdentifier'; import { IEnvironmentInfoService } from '../../info/environmentInfoService'; import { PythonEnvInfo, PythonEnvKind } from '../../info'; import { getEnvPath, setEnvDisplayString } from '../../info/env'; @@ -156,6 +156,10 @@ async function setKind(env: BasicEnvInfo, environmentKinds: Map Promise.resolve(false); + function getIdentifiers(): Map Promise> { - const notImplemented = () => Promise.resolve(false); const defaultTrue = () => Promise.resolve(true); const identifier: Map Promise> = new Map(); Object.values(PythonEnvKind).forEach((k) => { @@ -39,6 +40,15 @@ function getIdentifiers(): Map Promise return identifier; } +export function isIdentifierRegistered(kind: PythonEnvKind): boolean { + const identifiers = getIdentifiers(); + const identifier = identifiers.get(kind); + if (identifier === notImplemented) { + return false; + } + return true; +} + /** * Returns environment type. * @param {string} path : Absolute path to the python interpreter binary or path to environment. diff --git a/extensions/positron-python/src/client/pythonEnvironments/info/index.ts b/extensions/positron-python/src/client/pythonEnvironments/info/index.ts index 17e8958f631..607a1c7360a 100644 --- a/extensions/positron-python/src/client/pythonEnvironments/info/index.ts +++ b/extensions/positron-python/src/client/pythonEnvironments/info/index.ts @@ -19,6 +19,7 @@ export enum EnvironmentType { Venv = 'Venv', MicrosoftStore = 'MicrosoftStore', Poetry = 'Poetry', + Hatch = 'Hatch', VirtualEnvWrapper = 'VirtualEnvWrapper', ActiveState = 'ActiveState', Global = 'Global', @@ -28,6 +29,7 @@ export enum EnvironmentType { export const virtualEnvTypes = [ EnvironmentType.Poetry, EnvironmentType.Pipenv, + EnvironmentType.Hatch, EnvironmentType.Venv, EnvironmentType.VirtualEnvWrapper, EnvironmentType.Conda, @@ -115,6 +117,9 @@ export function getEnvironmentTypeName(environmentType: EnvironmentType): string case EnvironmentType.Poetry: { return 'Poetry'; } + case EnvironmentType.Hatch: { + return 'Hatch'; + } case EnvironmentType.VirtualEnvWrapper: { return 'virtualenvwrapper'; } diff --git a/extensions/positron-python/src/client/pythonEnvironments/legacyIOC.ts b/extensions/positron-python/src/client/pythonEnvironments/legacyIOC.ts index 31de503a0f3..4ef0894a470 100644 --- a/extensions/positron-python/src/client/pythonEnvironments/legacyIOC.ts +++ b/extensions/positron-python/src/client/pythonEnvironments/legacyIOC.ts @@ -38,6 +38,7 @@ const convertedKinds = new Map( [PythonEnvKind.VirtualEnv]: EnvironmentType.VirtualEnv, [PythonEnvKind.Pipenv]: EnvironmentType.Pipenv, [PythonEnvKind.Poetry]: EnvironmentType.Poetry, + [PythonEnvKind.Hatch]: EnvironmentType.Hatch, [PythonEnvKind.Venv]: EnvironmentType.Venv, [PythonEnvKind.VirtualEnvWrapper]: EnvironmentType.VirtualEnvWrapper, [PythonEnvKind.ActiveState]: EnvironmentType.ActiveState,