Skip to content

Commit b946a45

Browse files
committed
Better handling for venv activations
1 parent 5992c4d commit b946a45

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

src/managers/builtin/venvUtils.ts

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
NativePythonFinder,
2323
} from '../common/nativePythonFinder';
2424
import { getWorkspacePersistentState } from '../../common/persistentState';
25-
import { shortVersion, sortEnvironments } from '../common/utils';
25+
import { isWindows, shortVersion, sortEnvironments } from '../common/utils';
2626
import { findFiles, getConfiguration } from '../../common/workspace.apis';
2727
import { pickEnvironmentFrom } from '../../common/pickers/environments';
2828
import {
@@ -113,7 +113,7 @@ function getName(binPath: string): string {
113113
return path.basename(dir1);
114114
}
115115

116-
function getPythonInfo(env: NativeEnvInfo): PythonEnvironmentInfo {
116+
async function getPythonInfo(env: NativeEnvInfo): Promise<PythonEnvironmentInfo> {
117117
if (env.executable && env.version && env.prefix) {
118118
const venvName = env.name ?? getName(env.executable);
119119
const sv = shortVersion(env.version);
@@ -122,18 +122,50 @@ function getPythonInfo(env: NativeEnvInfo): PythonEnvironmentInfo {
122122
const binDir = path.dirname(env.executable);
123123

124124
const shellActivation: Map<TerminalShellType, PythonCommandRunConfiguration[]> = new Map();
125+
const shellDeactivation: Map<TerminalShellType, PythonCommandRunConfiguration[]> = new Map();
126+
127+
// Commands for bash
125128
shellActivation.set(TerminalShellType.bash, [{ executable: 'source', args: [path.join(binDir, 'activate')] }]);
129+
shellDeactivation.set(TerminalShellType.bash, [{ executable: 'deactivate' }]);
130+
131+
// Commands for csh
132+
shellActivation.set(TerminalShellType.cshell, [
133+
{ executable: 'source', args: [path.join(binDir, 'activate')] },
134+
]);
135+
shellDeactivation.set(TerminalShellType.cshell, [{ executable: 'deactivate' }]);
136+
137+
// Commands for zsh
138+
shellActivation.set(TerminalShellType.zsh, [{ executable: 'source', args: [path.join(binDir, 'activate')] }]);
139+
shellDeactivation.set(TerminalShellType.zsh, [{ executable: 'deactivate' }]);
140+
141+
// Commands for powershell
126142
shellActivation.set(TerminalShellType.powershell, [
127143
{ executable: '&', args: [path.join(binDir, 'Activate.ps1')] },
128144
]);
129-
shellActivation.set(TerminalShellType.commandPrompt, [{ executable: path.join(binDir, 'activate.bat') }]);
130-
shellActivation.set(TerminalShellType.unknown, [{ executable: path.join(binDir, 'activate') }]);
131-
132-
const shellDeactivation = new Map<TerminalShellType, PythonCommandRunConfiguration[]>();
133-
shellDeactivation.set(TerminalShellType.bash, [{ executable: 'deactivate' }]);
134145
shellDeactivation.set(TerminalShellType.powershell, [{ executable: 'deactivate' }]);
146+
147+
// Commands for command prompt
148+
shellActivation.set(TerminalShellType.commandPrompt, [{ executable: path.join(binDir, 'activate.bat') }]);
135149
shellDeactivation.set(TerminalShellType.commandPrompt, [{ executable: path.join(binDir, 'deactivate.bat') }]);
136-
shellActivation.set(TerminalShellType.unknown, [{ executable: 'deactivate' }]);
150+
151+
// Commands for fish
152+
if (await fsapi.pathExists(path.join(binDir, 'activate.fish'))) {
153+
shellActivation.set(TerminalShellType.fish, [
154+
{ executable: 'source', args: [path.join(binDir, 'activate.fish')] },
155+
]);
156+
shellDeactivation.set(TerminalShellType.fish, [{ executable: 'deactivate' }]);
157+
}
158+
159+
// Commands for unknown cases
160+
if (isWindows()) {
161+
shellActivation.set(TerminalShellType.unknown, [{ executable: path.join(binDir, 'activate') }]);
162+
shellDeactivation.set(TerminalShellType.unknown, [{ executable: 'deactivate' }]);
163+
} else {
164+
shellActivation.set(TerminalShellType.unknown, [
165+
{ executable: 'source', args: [path.join(binDir, 'activate')] },
166+
]);
167+
shellDeactivation.set(TerminalShellType.unknown, [{ executable: 'deactivate' }]);
168+
}
137169

138170
return {
139171
name: name,
@@ -176,16 +208,16 @@ export async function findVirtualEnvironments(
176208
.map((e) => e as NativeEnvInfo)
177209
.filter((e) => e.kind === NativePythonEnvironmentKind.venv);
178210

179-
envs.forEach((e) => {
211+
for (const e of envs) {
180212
if (!(e.prefix && e.executable && e.version)) {
181213
log.warn(`Invalid conda environment: ${JSON.stringify(e)}`);
182-
return;
214+
continue;
183215
}
184216

185-
const env = api.createPythonEnvironmentItem(getPythonInfo(e), manager);
217+
const env = api.createPythonEnvironmentItem(await getPythonInfo(e), manager);
186218
collection.push(env);
187219
log.info(`Found venv environment: ${env.name}`);
188-
});
220+
}
189221
return collection;
190222
}
191223

@@ -351,7 +383,7 @@ export async function createPythonVenv(
351383
}
352384

353385
const resolved = await nativeFinder.resolve(pythonPath);
354-
const env = api.createPythonEnvironmentItem(getPythonInfo(resolved), manager);
386+
const env = api.createPythonEnvironmentItem(await getPythonInfo(resolved), manager);
355387
if (packages?.length > 0) {
356388
await api.installPackages(env, packages, { upgrade: false });
357389
}
@@ -500,7 +532,7 @@ export async function resolveVenvPythonEnvironmentPath(
500532
const resolved = await nativeFinder.resolve(fsPath);
501533

502534
if (resolved.kind === NativePythonEnvironmentKind.venv) {
503-
const envInfo = getPythonInfo(resolved);
535+
const envInfo = await getPythonInfo(resolved);
504536
return api.createPythonEnvironmentItem(envInfo, manager);
505537
}
506538

0 commit comments

Comments
 (0)