forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvenvSwitchPython.ts
More file actions
28 lines (26 loc) · 1.23 KB
/
Copy pathvenvSwitchPython.ts
File metadata and controls
28 lines (26 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as path from 'path';
import { Disposable, Uri } from 'vscode';
import { createDeferred } from '../../../common/utils/async';
import { getExtension } from '../../../common/vscodeApis/extensionsApi';
import { PVSC_EXTENSION_ID, PythonExtension } from '../../../api/types';
import { traceInfo } from '../../../logging';
export async function switchSelectedPython(interpreter: string, uri: Uri, purpose: string): Promise<void> {
let dispose: Disposable | undefined;
try {
const deferred = createDeferred<void>();
const api: PythonExtension = getExtension(PVSC_EXTENSION_ID)?.exports as PythonExtension;
dispose = api.environments.onDidChangeActiveEnvironmentPath(async (e) => {
if (path.normalize(e.path) === path.normalize(interpreter)) {
traceInfo(`Switched to interpreter ${purpose}: ${interpreter}`);
deferred.resolve();
}
});
api.environments.updateActiveEnvironmentPath(interpreter, uri);
traceInfo(`Switching interpreter ${purpose}: ${interpreter}`);
await deferred.promise;
} finally {
dispose?.dispose();
}
}