Skip to content

Commit 408601a

Browse files
authored
fix getInterpreterDetails to include quotes if necessary (#790)
1 parent 7ae029c commit 408601a

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

src/extension/common/python.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
// Licensed under the MIT License.
33

44
/* 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';
612
import { commands, EventEmitter, extensions, Uri, Event, Disposable } from 'vscode';
713
import { createDeferred } from './utils/async';
814
import { traceError, traceLog } from './log/logging';
@@ -22,12 +28,25 @@ export interface IInterpreterDetails {
2228
const onDidChangePythonInterpreterEvent = new EventEmitter<IInterpreterDetails>();
2329
export const onDidChangePythonInterpreter: Event<IInterpreterDetails> = onDidChangePythonInterpreterEvent.event;
2430
async function activateExtension() {
31+
console.log('Activating Python extension...');
32+
activateEnvsExtension();
2533
const extension = extensions.getExtension('ms-python.python');
2634
if (extension) {
2735
if (!extension.isActive) {
2836
await extension.activate();
2937
}
3038
}
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+
}
3150
return extension;
3251
}
3352

@@ -48,8 +67,16 @@ export async function initializePython(disposables: Disposable[]): Promise<void>
4867

4968
if (api) {
5069
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 });
5380
}),
5481
);
5582

@@ -89,8 +116,13 @@ export async function getActiveEnvironmentPath(resource?: Resource) {
89116
export async function getInterpreterDetails(resource?: Uri): Promise<IInterpreterDetails> {
90117
const api = await getPythonExtensionEnviromentAPI();
91118
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 };
94126
}
95127
return { path: undefined, resource };
96128
}

0 commit comments

Comments
 (0)