Skip to content

Commit c2eb3dc

Browse files
committed
update to use source activate
1 parent fe38ba0 commit c2eb3dc

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/managers/pipenv/pipenvUtils.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
NativePythonEnvironmentKind,
2121
NativePythonFinder,
2222
} from '../common/nativePythonFinder';
23-
import { shortVersion } from '../common/utils';
23+
import { getShellActivationCommands, shortVersion } from '../common/utils';
2424

2525
export const PIPENV_PATH_KEY = `${ENVS_EXTENSION_ID}:pipenv:PIPENV_PATH`;
2626
export const PIPENV_WORKSPACE_KEY = `${ENVS_EXTENSION_ID}:pipenv:WORKSPACE_SELECTED`;
@@ -85,12 +85,11 @@ export async function getPipenv(native?: NativePythonFinder): Promise<string | u
8585
return undefined;
8686
}
8787

88-
function nativeToPythonEnv(
88+
async function nativeToPythonEnv(
8989
info: NativeEnvInfo,
9090
api: PythonEnvironmentApi,
9191
manager: EnvironmentManager,
92-
pipenv: string,
93-
): PythonEnvironment | undefined {
92+
): Promise<PythonEnvironment | undefined> {
9493
if (!(info.prefix && info.executable && info.version)) {
9594
traceError(`Incomplete pipenv environment info: ${JSON.stringify(info)}`);
9695
return undefined;
@@ -100,12 +99,18 @@ function nativeToPythonEnv(
10099
const name = info.name || info.displayName || path.basename(info.prefix);
101100
const displayName = info.displayName || `pipenv (${sv})`;
102101

103-
const shellActivation: Map<string, PythonCommandRunConfiguration[]> = new Map();
104-
const shellDeactivation: Map<string, PythonCommandRunConfiguration[]> = new Map();
102+
// Derive the environment's bin/scripts directory from the python executable
103+
const binDir = path.dirname(info.executable);
104+
let shellActivation: Map<string, PythonCommandRunConfiguration[]> = new Map();
105+
let shellDeactivation: Map<string, PythonCommandRunConfiguration[]> = new Map();
105106

106-
// Use 'pipenv shell' for activation and 'exit' for deactivation
107-
shellActivation.set('unknown', [{ executable: pipenv, args: ['shell'] }]);
108-
shellDeactivation.set('unknown', [{ executable: 'exit', args: [] }]);
107+
try {
108+
const maps = await getShellActivationCommands(binDir);
109+
shellActivation = maps.shellActivation;
110+
shellDeactivation = maps.shellDeactivation;
111+
} catch (ex) {
112+
traceError(`Failed to compute shell activation commands for pipenv at ${binDir}: ${ex}`);
113+
}
109114

110115
const environment: PythonEnvironmentInfo = {
111116
name: name,
@@ -122,7 +127,6 @@ function nativeToPythonEnv(
122127
shellDeactivation,
123128
},
124129
sysPrefix: info.prefix,
125-
group: 'pipenv',
126130
};
127131

128132
return api.createPythonEnvironmentItem(environment, manager);
@@ -158,14 +162,14 @@ export async function refreshPipenv(
158162

159163
const collection: PythonEnvironment[] = [];
160164

161-
envs.forEach((e) => {
165+
for (const e of envs) {
162166
if (pipenv) {
163-
const environment = nativeToPythonEnv(e, api, manager, pipenv);
167+
const environment = await nativeToPythonEnv(e, api, manager);
164168
if (environment) {
165169
collection.push(environment);
166170
}
167171
}
168-
});
172+
}
169173

170174
traceInfo(`Found ${collection.length} pipenv environments`);
171175
return collection;
@@ -182,7 +186,7 @@ export async function resolvePipenvPath(
182186
if (resolved.kind === NativePythonEnvironmentKind.pipenv) {
183187
const pipenv = await getPipenv(nativeFinder);
184188
if (pipenv) {
185-
return nativeToPythonEnv(resolved, api, manager, pipenv);
189+
return await nativeToPythonEnv(resolved, api, manager);
186190
}
187191
}
188192

0 commit comments

Comments
 (0)