Skip to content

Commit d135034

Browse files
committed
Better handling for venv activations
1 parent 0ec3e94 commit d135034

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
@@ -19,7 +19,7 @@ import {
1919
NativePythonFinder,
2020
} from '../common/nativePythonFinder';
2121
import { getWorkspacePersistentState } from '../../common/persistentState';
22-
import { shortVersion, sortEnvironments } from '../common/utils';
22+
import { isWindows, shortVersion, sortEnvironments } from '../common/utils';
2323
import { getConfiguration } from '../../common/workspace.apis';
2424
import { pickEnvironmentFrom } from '../../common/pickers/environments';
2525
import {
@@ -110,7 +110,7 @@ function getName(binPath: string): string {
110110
return path.basename(dir1);
111111
}
112112

113-
function getPythonInfo(env: NativeEnvInfo): PythonEnvironmentInfo {
113+
async function getPythonInfo(env: NativeEnvInfo): Promise<PythonEnvironmentInfo> {
114114
if (env.executable && env.version && env.prefix) {
115115
const venvName = env.name ?? getName(env.executable);
116116
const sv = shortVersion(env.version);
@@ -119,18 +119,50 @@ function getPythonInfo(env: NativeEnvInfo): PythonEnvironmentInfo {
119119
const binDir = path.dirname(env.executable);
120120

121121
const shellActivation: Map<TerminalShellType, PythonCommandRunConfiguration[]> = new Map();
122+
const shellDeactivation: Map<TerminalShellType, PythonCommandRunConfiguration[]> = new Map();
123+
124+
// Commands for bash
122125
shellActivation.set(TerminalShellType.bash, [{ executable: 'source', args: [path.join(binDir, 'activate')] }]);
126+
shellDeactivation.set(TerminalShellType.bash, [{ executable: 'deactivate' }]);
127+
128+
// Commands for csh
129+
shellActivation.set(TerminalShellType.cshell, [
130+
{ executable: 'source', args: [path.join(binDir, 'activate')] },
131+
]);
132+
shellDeactivation.set(TerminalShellType.cshell, [{ executable: 'deactivate' }]);
133+
134+
// Commands for zsh
135+
shellActivation.set(TerminalShellType.zsh, [{ executable: 'source', args: [path.join(binDir, 'activate')] }]);
136+
shellDeactivation.set(TerminalShellType.zsh, [{ executable: 'deactivate' }]);
137+
138+
// Commands for powershell
123139
shellActivation.set(TerminalShellType.powershell, [
124140
{ executable: '&', args: [path.join(binDir, 'Activate.ps1')] },
125141
]);
126-
shellActivation.set(TerminalShellType.commandPrompt, [{ executable: path.join(binDir, 'activate.bat') }]);
127-
shellActivation.set(TerminalShellType.unknown, [{ executable: path.join(binDir, 'activate') }]);
128-
129-
const shellDeactivation = new Map<TerminalShellType, PythonCommandRunConfiguration[]>();
130-
shellDeactivation.set(TerminalShellType.bash, [{ executable: 'deactivate' }]);
131142
shellDeactivation.set(TerminalShellType.powershell, [{ executable: 'deactivate' }]);
143+
144+
// Commands for command prompt
145+
shellActivation.set(TerminalShellType.commandPrompt, [{ executable: path.join(binDir, 'activate.bat') }]);
132146
shellDeactivation.set(TerminalShellType.commandPrompt, [{ executable: path.join(binDir, 'deactivate.bat') }]);
133-
shellActivation.set(TerminalShellType.unknown, [{ executable: 'deactivate' }]);
147+
148+
// Commands for fish
149+
if (await fsapi.pathExists(path.join(binDir, 'activate.fish'))) {
150+
shellActivation.set(TerminalShellType.fish, [
151+
{ executable: 'source', args: [path.join(binDir, 'activate.fish')] },
152+
]);
153+
shellDeactivation.set(TerminalShellType.fish, [{ executable: 'deactivate' }]);
154+
}
155+
156+
// Commands for unknown cases
157+
if (isWindows()) {
158+
shellActivation.set(TerminalShellType.unknown, [{ executable: path.join(binDir, 'activate') }]);
159+
shellDeactivation.set(TerminalShellType.unknown, [{ executable: 'deactivate' }]);
160+
} else {
161+
shellActivation.set(TerminalShellType.unknown, [
162+
{ executable: 'source', args: [path.join(binDir, 'activate')] },
163+
]);
164+
shellDeactivation.set(TerminalShellType.unknown, [{ executable: 'deactivate' }]);
165+
}
134166

135167
return {
136168
name: name,
@@ -173,16 +205,16 @@ export async function findVirtualEnvironments(
173205
.map((e) => e as NativeEnvInfo)
174206
.filter((e) => e.kind === NativePythonEnvironmentKind.venv);
175207

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

182-
const env = api.createPythonEnvironmentItem(getPythonInfo(e), manager);
214+
const env = api.createPythonEnvironmentItem(await getPythonInfo(e), manager);
183215
collection.push(env);
184216
log.info(`Found venv environment: ${env.name}`);
185-
});
217+
}
186218
return collection;
187219
}
188220

@@ -339,7 +371,7 @@ export async function createPythonVenv(
339371
}
340372

341373
const resolved = await nativeFinder.resolve(pythonPath);
342-
const env = api.createPythonEnvironmentItem(getPythonInfo(resolved), manager);
374+
const env = api.createPythonEnvironmentItem(await getPythonInfo(resolved), manager);
343375
if (packages && packages?.length > 0) {
344376
await api.installPackages(env, packages, { upgrade: false });
345377
}
@@ -397,7 +429,7 @@ export async function resolveVenvPythonEnvironmentPath(
397429
const resolved = await nativeFinder.resolve(fsPath);
398430

399431
if (resolved.kind === NativePythonEnvironmentKind.venv) {
400-
const envInfo = getPythonInfo(resolved);
432+
const envInfo = await getPythonInfo(resolved);
401433
return api.createPythonEnvironmentItem(envInfo, manager);
402434
}
403435

0 commit comments

Comments
 (0)