2
2
// Licensed under the MIT License.
3
3
4
4
/* eslint-disable @typescript-eslint/naming-convention */
5
- import { Environment , EnvironmentPath , PythonExtension , Resource } from '@vscode/python-extension' ;
5
+ import {
6
+ ActiveEnvironmentPathChangeEvent ,
7
+ Environment ,
8
+ EnvironmentPath ,
9
+ PythonExtension ,
10
+ Resource ,
11
+ } from '@vscode/python-extension' ;
6
12
import { commands , EventEmitter , extensions , Uri , Event , Disposable } from 'vscode' ;
7
13
import { createDeferred } from './utils/async' ;
8
14
import { traceError , traceLog } from './log/logging' ;
@@ -22,12 +28,25 @@ export interface IInterpreterDetails {
22
28
const onDidChangePythonInterpreterEvent = new EventEmitter < IInterpreterDetails > ( ) ;
23
29
export const onDidChangePythonInterpreter : Event < IInterpreterDetails > = onDidChangePythonInterpreterEvent . event ;
24
30
async function activateExtension ( ) {
31
+ console . log ( 'Activating Python extension...' ) ;
32
+ activateEnvsExtension ( ) ;
25
33
const extension = extensions . getExtension ( 'ms-python.python' ) ;
26
34
if ( extension ) {
27
35
if ( ! extension . isActive ) {
28
36
await extension . activate ( ) ;
29
37
}
30
38
}
39
+ console . log ( 'Python extension activated.' ) ;
40
+ return extension ;
41
+ }
42
+
43
+ async function activateEnvsExtension ( ) {
44
+ const extension = extensions . getExtension ( 'ms-python.vscode-python-envs' ) ;
45
+ if ( extension ) {
46
+ if ( ! extension . isActive ) {
47
+ await extension . activate ( ) ;
48
+ }
49
+ }
31
50
return extension ;
32
51
}
33
52
@@ -48,8 +67,16 @@ export async function initializePython(disposables: Disposable[]): Promise<void>
48
67
49
68
if ( api ) {
50
69
disposables . push (
51
- api . environments . onDidChangeActiveEnvironmentPath ( ( e ) => {
52
- onDidChangePythonInterpreterEvent . fire ( { path : [ e . path ] , resource : e . resource ?. uri } ) ;
70
+ api . environments . onDidChangeActiveEnvironmentPath ( ( e : ActiveEnvironmentPathChangeEvent ) => {
71
+ let resourceUri : Uri | undefined ;
72
+ if ( e . resource instanceof Uri ) {
73
+ resourceUri = e . resource ;
74
+ }
75
+ if ( e . resource && 'uri' in e . resource ) {
76
+ // WorkspaceFolder type
77
+ resourceUri = e . resource . uri ;
78
+ }
79
+ onDidChangePythonInterpreterEvent . fire ( { path : [ e . path ] , resource : resourceUri } ) ;
53
80
} ) ,
54
81
) ;
55
82
@@ -89,8 +116,13 @@ export async function getActiveEnvironmentPath(resource?: Resource) {
89
116
export async function getInterpreterDetails ( resource ?: Uri ) : Promise < IInterpreterDetails > {
90
117
const api = await getPythonExtensionEnviromentAPI ( ) ;
91
118
const environment = await api . environments . resolveEnvironment ( api . environments . getActiveEnvironmentPath ( resource ) ) ;
92
- if ( environment ?. executable . uri ) {
93
- return { path : [ environment ?. executable . uri . fsPath ] , resource } ;
119
+ const rawExecPath = environment ?. executable . uri ?. fsPath ;
120
+ if ( rawExecPath ) {
121
+ let execPath = rawExecPath ;
122
+ if ( rawExecPath . includes ( ' ' ) && ! ( rawExecPath . startsWith ( '"' ) && rawExecPath . endsWith ( '"' ) ) ) {
123
+ execPath = `"${ rawExecPath } "` ;
124
+ }
125
+ return { path : [ execPath ] , resource } ;
94
126
}
95
127
return { path : undefined , resource } ;
96
128
}
0 commit comments