diff --git a/.npmrc b/.npmrc index 6d6c884..8e6d762 100644 --- a/.npmrc +++ b/.npmrc @@ -1,3 +1,4 @@ +@vscode:registry=https://pkgs.dev.azure.com/azure-public/vside/_packaging/python-environments/npm/registry/ # Force public npm registry to avoid CI auth (E401) when no token is provided registry=https://registry.npmjs.org/ # Do not require auth for public installs diff --git a/package-lock.json b/package-lock.json index 57d64e9..ce2f2c1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "2025.3.0", "license": "MIT", "dependencies": { + "@vscode/python-environments": "^1.0.0", "@vscode/python-extension": "^1.0.6", "fs-extra": "^11.3.4", "semver": "^7.7.4", @@ -1177,6 +1178,16 @@ "integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==", "dev": true }, + "node_modules/@vscode/python-environments": { + "version": "1.0.0", + "resolved": "https://pkgs.dev.azure.com/azure-public/vside/_packaging/python-environments/npm/registry/@vscode/python-environments/-/python-environments-1.0.0.tgz", + "integrity": "sha1-AQxJi6ysjdysdHVWlc/H3XEbHfU=", + "license": "MIT", + "engines": { + "node": ">=22.21.1", + "vscode": "^1.110.0" + } + }, "node_modules/@vscode/python-extension": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/@vscode/python-extension/-/python-extension-1.0.6.tgz", @@ -8414,6 +8425,11 @@ "integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==", "dev": true }, + "@vscode/python-environments": { + "version": "1.0.0", + "resolved": "https://pkgs.dev.azure.com/azure-public/vside/_packaging/python-environments/npm/registry/@vscode/python-environments/-/python-environments-1.0.0.tgz", + "integrity": "sha1-AQxJi6ysjdysdHVWlc/H3XEbHfU=" + }, "@vscode/python-extension": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/@vscode/python-extension/-/python-extension-1.0.6.tgz", diff --git a/package.json b/package.json index 3e5ce0c..6d2c16d 100644 --- a/package.json +++ b/package.json @@ -208,6 +208,7 @@ ] }, "dependencies": { + "@vscode/python-environments": "^1.0.0", "@vscode/python-extension": "^1.0.6", "fs-extra": "^11.3.4", "semver": "^7.7.4", diff --git a/src/common/python.ts b/src/common/python.ts index c67c1e9..5613ebc 100644 --- a/src/common/python.ts +++ b/src/common/python.ts @@ -2,13 +2,13 @@ // Licensed under the MIT License. /* eslint-disable @typescript-eslint/naming-convention */ +import { PythonEnvironmentApi, PythonEnvironment, PythonEnvironments } from '@vscode/python-environments'; import { PythonExtension, ResolvedEnvironment } from '@vscode/python-extension'; import * as semver from 'semver'; -import { commands, Disposable, Event, EventEmitter, extensions, Uri } from 'vscode'; +import { commands, Disposable, Event, EventEmitter, Uri } from 'vscode'; import { PYTHON_MAJOR, PYTHON_MINOR, PYTHON_VERSION } from './constants'; import { traceError, traceLog } from './logging'; import { getProjectRoot } from './utilities'; -import type { PythonEnvironment, PythonEnvironmentsAPI } from '../typings/pythonEnvironments'; export interface IInterpreterDetails { path?: string[]; @@ -56,32 +56,17 @@ async function getPythonExtensionAPI(): Promise { return _api; } -const PYTHON_ENVIRONMENTS_EXTENSION_ID = 'ms-python.vscode-python-envs'; - -let _envsApi: PythonEnvironmentsAPI | undefined; -async function getEnvironmentsExtensionAPI(): Promise { +let _envsApi: PythonEnvironmentApi | undefined; +async function getEnvironmentsExtensionAPI(): Promise { if (_envsApi) { return _envsApi; } - const extension = extensions.getExtension(PYTHON_ENVIRONMENTS_EXTENSION_ID); - if (!extension) { - return undefined; - } try { - if (!extension.isActive) { - await extension.activate(); - } - const api = extension.exports; - if (!api) { - traceError('Python environments extension did not provide any exports.'); - return undefined; - } - _envsApi = api as PythonEnvironmentsAPI; - return _envsApi; - } catch (ex) { - traceError('Failed to activate or retrieve API from Python environments extension.', ex as Error); + _envsApi = await PythonEnvironments.api(); + } catch { return undefined; } + return _envsApi; } function sameInterpreter(a: string[], b: string[]): boolean { diff --git a/src/typings/pythonEnvironments.d.ts b/src/typings/pythonEnvironments.d.ts deleted file mode 100644 index ad98a8c..0000000 --- a/src/typings/pythonEnvironments.d.ts +++ /dev/null @@ -1,145 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -/** - * Type declarations for the ms-python.vscode-python-envs extension API. - * Subset of types from https://github.com/microsoft/vscode-python-environments/blob/main/src/api.ts - * covering only the API surface used by this extension. - */ - -import { Event, Uri } from 'vscode'; - -/** - * Options for executing a Python executable. - */ -export interface PythonCommandRunConfiguration { - /** - * Path to the binary like `python.exe` or `python3` to execute. - */ - executable: string; - - /** - * Arguments to pass to the python executable. - */ - args?: string[]; -} - -/** - * Contains details on how to use a particular python environment. - */ -export interface PythonEnvironmentExecutionInfo { - /** - * Details on how to run the python executable. - */ - run: PythonCommandRunConfiguration; - - /** - * Details on how to run the python executable after activating the environment. - */ - activatedRun?: PythonCommandRunConfiguration; - - /** - * Details on how to activate an environment. - */ - activation?: PythonCommandRunConfiguration[]; -} - -/** - * Interface representing information about a Python environment. - */ -export interface PythonEnvironmentInfo { - /** - * The name of the Python environment. - */ - readonly name: string; - - /** - * The display name of the Python environment. - */ - readonly displayName: string; - - /** - * The version of the Python environment. - */ - readonly version: string; - - /** - * Path to the python binary or environment folder. - */ - readonly environmentPath: Uri; - - /** - * Information on how to execute the Python environment. - */ - readonly execInfo: PythonEnvironmentExecutionInfo; - - /** - * `sys.prefix` path for the Python installation. - */ - readonly sysPrefix: string; -} - -/** - * Interface representing the ID of a Python environment. - */ -export interface PythonEnvironmentId { - /** - * The unique identifier of the Python environment. - */ - id: string; - - /** - * The ID of the manager responsible for the Python environment. - */ - managerId: string; -} - -/** - * Interface representing a Python environment. - */ -export interface PythonEnvironment extends PythonEnvironmentInfo { - /** - * The ID of the Python environment. - */ - readonly envId: PythonEnvironmentId; -} - -/** - * Type representing the scope for getting a Python environment. - */ -export type GetEnvironmentScope = undefined | Uri; - -/** - * Event arguments for when the current Python environment changes. - */ -export interface DidChangeEnvironmentEventArgs { - readonly uri: Uri | undefined; - readonly old: PythonEnvironment | undefined; - readonly new: PythonEnvironment | undefined; -} - -/** - * Type representing the context for resolving a Python environment. - */ -export type ResolveEnvironmentContext = Uri; - -/** - * The API exported by the ms-python.vscode-python-envs extension. - * This is the subset of PythonEnvironmentApi used by this extension. - */ -export interface PythonEnvironmentsAPI { - /** - * Retrieves the current Python environment within the specified scope. - */ - getEnvironment(scope: GetEnvironmentScope): Promise; - - /** - * Resolves a Python environment from a Uri context. - */ - resolveEnvironment(context: ResolveEnvironmentContext): Promise; - - /** - * Event that is fired when the selected Python environment changes. - */ - onDidChangeEnvironment: Event; -}