Skip to content

Commit fbecd1c

Browse files
authored
Use @vscode/python-extension package (#144)
* Use `@vscode/python-extension` package * audit fixes
1 parent 4943c2c commit fbecd1c

File tree

3 files changed

+43
-220
lines changed

3 files changed

+43
-220
lines changed

package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
]
135135
},
136136
"dependencies": {
137+
"@vscode/python-extension": "1.0.1",
137138
"fs-extra": "^11.1.1",
138139
"vscode-languageclient": "^8.1.0"
139140
},

src/common/python.ts

Lines changed: 9 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -2,214 +2,9 @@
22
// Licensed under the MIT License.
33

44
/* eslint-disable @typescript-eslint/naming-convention */
5-
import { commands, Disposable, Event, EventEmitter, extensions, Uri, WorkspaceFolder } from 'vscode';
5+
import { commands, Disposable, Event, EventEmitter, Uri } from 'vscode';
66
import { traceError, traceLog } from './log/logging';
7-
8-
type Environment = EnvironmentPath & {
9-
/**
10-
* Carries details about python executable.
11-
*/
12-
readonly executable: {
13-
/**
14-
* Uri of the python interpreter/executable. Carries `undefined` in case an executable does not belong to
15-
* the environment.
16-
*/
17-
readonly uri: Uri | undefined;
18-
/**
19-
* Bitness if known at this moment.
20-
*/
21-
readonly bitness: Bitness | undefined;
22-
/**
23-
* Value of `sys.prefix` in sys module if known at this moment.
24-
*/
25-
readonly sysPrefix: string | undefined;
26-
};
27-
/**
28-
* Carries details if it is an environment, otherwise `undefined` in case of global interpreters and others.
29-
*/
30-
readonly environment:
31-
| {
32-
/**
33-
* Type of the environment.
34-
*/
35-
readonly type: EnvironmentType;
36-
/**
37-
* Name to the environment if any.
38-
*/
39-
readonly name: string | undefined;
40-
/**
41-
* Uri of the environment folder.
42-
*/
43-
readonly folderUri: Uri;
44-
/**
45-
* Any specific workspace folder this environment is created for.
46-
*/
47-
readonly workspaceFolder: Uri | undefined;
48-
}
49-
| undefined;
50-
/**
51-
* Carries Python version information known at this moment.
52-
*/
53-
readonly version: VersionInfo & {
54-
/**
55-
* Value of `sys.version` in sys module if known at this moment.
56-
*/
57-
readonly sysVersion: string | undefined;
58-
};
59-
/**
60-
* Tools/plugins which created the environment or where it came from. First value in array corresponds
61-
* to the primary tool which manages the environment, which never changes over time.
62-
*
63-
* Array is empty if no tool is responsible for creating/managing the environment. Usually the case for
64-
* global interpreters.
65-
*/
66-
readonly tools: readonly EnvironmentTools[];
67-
};
68-
69-
/**
70-
* Derived form of {@link Environment} where certain properties can no longer be `undefined`. Meant to represent an
71-
* {@link Environment} with complete information.
72-
*/
73-
type ResolvedEnvironment = Environment & {
74-
/**
75-
* Carries complete details about python executable.
76-
*/
77-
readonly executable: {
78-
/**
79-
* Uri of the python interpreter/executable. Carries `undefined` in case an executable does not belong to
80-
* the environment.
81-
*/
82-
readonly uri: Uri | undefined;
83-
/**
84-
* Bitness of the environment.
85-
*/
86-
readonly bitness: Bitness;
87-
/**
88-
* Value of `sys.prefix` in sys module.
89-
*/
90-
readonly sysPrefix: string;
91-
};
92-
/**
93-
* Carries complete Python version information.
94-
*/
95-
readonly version: ResolvedVersionInfo & {
96-
/**
97-
* Value of `sys.version` in sys module if known at this moment.
98-
*/
99-
readonly sysVersion: string;
100-
};
101-
};
102-
103-
type EnvironmentsChangeEvent = {
104-
readonly env: Environment;
105-
/**
106-
* * "add": New environment is added.
107-
* * "remove": Existing environment in the list is removed.
108-
* * "update": New information found about existing environment.
109-
*/
110-
readonly type: 'add' | 'remove' | 'update';
111-
};
112-
113-
type ActiveEnvironmentPathChangeEvent = EnvironmentPath & {
114-
/**
115-
* Workspace folder the environment changed for.
116-
*/
117-
readonly resource: WorkspaceFolder | undefined;
118-
};
119-
120-
/**
121-
* Uri of a file inside a workspace or workspace folder itself.
122-
*/
123-
type Resource = Uri | WorkspaceFolder;
124-
125-
type EnvironmentPath = {
126-
/**
127-
* The ID of the environment.
128-
*/
129-
readonly id: string;
130-
/**
131-
* Path to environment folder or path to python executable that uniquely identifies an environment. Environments
132-
* lacking a python executable are identified by environment folder paths, whereas other envs can be identified
133-
* using python executable path.
134-
*/
135-
readonly path: string;
136-
};
137-
138-
/**
139-
* Tool/plugin where the environment came from. It can be {@link KnownEnvironmentTools} or custom string which
140-
* was contributed.
141-
*/
142-
type EnvironmentTools = KnownEnvironmentTools | string;
143-
/**
144-
* Tools or plugins the Python extension currently has built-in support for. Note this list is expected to shrink
145-
* once tools have their own separate extensions.
146-
*/
147-
type KnownEnvironmentTools =
148-
| 'Conda'
149-
| 'Pipenv'
150-
| 'Poetry'
151-
| 'VirtualEnv'
152-
| 'Venv'
153-
| 'VirtualEnvWrapper'
154-
| 'Pyenv'
155-
| 'Unknown';
156-
157-
/**
158-
* Type of the environment. It can be {@link KnownEnvironmentTypes} or custom string which was contributed.
159-
*/
160-
type EnvironmentType = KnownEnvironmentTypes | string;
161-
/**
162-
* Environment types the Python extension is aware of. Note this list is expected to shrink once tools have their
163-
* own separate extensions, in which case they're expected to provide the type themselves.
164-
*/
165-
type KnownEnvironmentTypes = 'VirtualEnvironment' | 'Conda' | 'Unknown';
166-
167-
/**
168-
* Carries bitness for an environment.
169-
*/
170-
type Bitness = '64-bit' | '32-bit' | 'Unknown';
171-
172-
/**
173-
* The possible Python release levels.
174-
*/
175-
type PythonReleaseLevel = 'alpha' | 'beta' | 'candidate' | 'final';
176-
177-
/**
178-
* Release information for a Python version.
179-
*/
180-
type PythonVersionRelease = {
181-
readonly level: PythonReleaseLevel;
182-
readonly serial: number;
183-
};
184-
185-
type VersionInfo = {
186-
readonly major: number | undefined;
187-
readonly minor: number | undefined;
188-
readonly micro: number | undefined;
189-
readonly release: PythonVersionRelease | undefined;
190-
};
191-
192-
type ResolvedVersionInfo = {
193-
readonly major: number;
194-
readonly minor: number;
195-
readonly micro: number;
196-
readonly release: PythonVersionRelease;
197-
};
198-
199-
interface IExtensionApi {
200-
ready: Promise<void>;
201-
debug: {
202-
getRemoteLauncherCommand(host: string, port: number, waitUntilDebuggerAttaches: boolean): Promise<string[]>;
203-
getDebuggerPackagePath(): Promise<string | undefined>;
204-
};
205-
environments: {
206-
getActiveEnvironmentPath(resource?: Resource): EnvironmentPath;
207-
resolveEnvironment(
208-
environment: Environment | EnvironmentPath | string,
209-
): Promise<ResolvedEnvironment | undefined>;
210-
readonly onDidChangeActiveEnvironmentPath: Event<ActiveEnvironmentPathChangeEvent>;
211-
};
212-
}
7+
import { PythonExtension, ResolvedEnvironment } from '@vscode/python-extension';
2138

2149
export interface IInterpreterDetails {
21510
path?: string[];
@@ -219,19 +14,13 @@ export interface IInterpreterDetails {
21914
const onDidChangePythonInterpreterEvent = new EventEmitter<IInterpreterDetails>();
22015
export const onDidChangePythonInterpreter: Event<IInterpreterDetails> = onDidChangePythonInterpreterEvent.event;
22116

222-
async function activateExtension() {
223-
const extension = extensions.getExtension('ms-python.python');
224-
if (extension) {
225-
if (!extension.isActive) {
226-
await extension.activate();
227-
}
17+
let _api: PythonExtension | undefined;
18+
async function getPythonExtensionAPI(): Promise<PythonExtension | undefined> {
19+
if (_api) {
20+
return _api;
22821
}
229-
return extension;
230-
}
231-
232-
async function getPythonExtensionAPI(): Promise<IExtensionApi | undefined> {
233-
const extension = await activateExtension();
234-
return extension?.exports as IExtensionApi;
22+
_api = await PythonExtension.api();
23+
return _api;
23524
}
23625

23726
export async function initializePython(disposables: Disposable[]): Promise<void> {
@@ -275,7 +64,7 @@ export async function getDebuggerPath(): Promise<string | undefined> {
27564
}
27665

27766
export async function runPythonExtensionCommand(command: string, ...rest: any[]) {
278-
await activateExtension();
67+
await getPythonExtensionAPI();
27968
return await commands.executeCommand(command, ...rest);
28069
}
28170

0 commit comments

Comments
 (0)