Skip to content

Commit e624eff

Browse files
author
Kartik Raj
authored
Make sure conda activate is used for micromamba (#20760)
Closes #20756
1 parent 29bee00 commit e624eff

File tree

2 files changed

+26
-84
lines changed

2 files changed

+26
-84
lines changed

src/client/common/terminal/environmentActivationProviders/condaActivationProvider.ts

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ import { IPlatformService } from '../../platform/types';
1414
import { IConfigurationService } from '../../types';
1515
import { ITerminalActivationCommandProvider, TerminalShellType } from '../types';
1616

17-
// Version number of conda that requires we call activate with 'conda activate' instead of just 'activate'
18-
const CondaRequiredMajor = 4;
19-
const CondaRequiredMinor = 4;
20-
const CondaRequiredMinorForPowerShell = 6;
21-
2217
/**
2318
* Support conda env activation (in the terminal).
2419
*/
@@ -65,57 +60,38 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
6560

6661
const condaEnv = envInfo.name.length > 0 ? envInfo.name : envInfo.path;
6762

68-
// Algorithm differs based on version
69-
// Old version, just call activate directly.
70-
// New version, call activate from the same path as our python path, then call it again to activate our environment.
71-
// -- note that the 'default' conda location won't allow activate to work for the environment sometimes.
72-
const versionInfo = await this.condaService.getCondaVersion();
73-
if (versionInfo && versionInfo.major >= CondaRequiredMajor) {
74-
// Conda added support for powershell in 4.6.
63+
// New version.
64+
const interpreterPath = await this.condaService.getInterpreterPathForEnvironment(envInfo);
65+
const activatePath = await this.condaService.getActivationScriptFromInterpreter(interpreterPath, envInfo.name);
66+
// eslint-disable-next-line camelcase
67+
if (activatePath?.path) {
7568
if (
76-
versionInfo.minor >= CondaRequiredMinorForPowerShell &&
77-
(targetShell === TerminalShellType.powershell || targetShell === TerminalShellType.powershellCore)
69+
this.platform.isWindows &&
70+
targetShell !== TerminalShellType.bash &&
71+
targetShell !== TerminalShellType.gitbash
7872
) {
79-
return _getPowershellCommands(condaEnv);
73+
return [activatePath.path, `conda activate ${condaEnv.toCommandArgumentForPythonExt()}`];
8074
}
81-
if (versionInfo.minor >= CondaRequiredMinor) {
82-
// New version.
83-
const interpreterPath = await this.condaService.getInterpreterPathForEnvironment(envInfo);
84-
const activatePath = await this.condaService.getActivationScriptFromInterpreter(
85-
interpreterPath,
86-
envInfo.name,
87-
);
75+
76+
const condaInfo = await this.condaService.getCondaInfo();
77+
78+
if (
79+
activatePath.type !== 'global' ||
8880
// eslint-disable-next-line camelcase
89-
if (activatePath?.path) {
90-
if (
91-
this.platform.isWindows &&
92-
targetShell !== TerminalShellType.bash &&
93-
targetShell !== TerminalShellType.gitbash
94-
) {
95-
return [activatePath.path, `conda activate ${condaEnv.toCommandArgumentForPythonExt()}`];
96-
}
97-
98-
const condaInfo = await this.condaService.getCondaInfo();
99-
100-
if (
101-
activatePath.type !== 'global' ||
102-
// eslint-disable-next-line camelcase
103-
condaInfo?.conda_shlvl === undefined ||
104-
condaInfo.conda_shlvl === -1
105-
) {
106-
// activatePath is not the global activate path, or we don't have a shlvl, or it's -1(conda never sourced).
107-
// and we need to source the activate path.
108-
if (activatePath.path === 'activate') {
109-
return [
110-
`source ${activatePath.path}`,
111-
`conda activate ${condaEnv.toCommandArgumentForPythonExt()}`,
112-
];
113-
}
114-
return [`source ${activatePath.path} ${condaEnv.toCommandArgumentForPythonExt()}`];
115-
}
116-
return [`conda activate ${condaEnv.toCommandArgumentForPythonExt()}`];
81+
condaInfo?.conda_shlvl === undefined ||
82+
condaInfo.conda_shlvl === -1
83+
) {
84+
// activatePath is not the global activate path, or we don't have a shlvl, or it's -1(conda never sourced).
85+
// and we need to source the activate path.
86+
if (activatePath.path === 'activate') {
87+
return [
88+
`source ${activatePath.path}`,
89+
`conda activate ${condaEnv.toCommandArgumentForPythonExt()}`,
90+
];
11791
}
92+
return [`source ${activatePath.path} ${condaEnv.toCommandArgumentForPythonExt()}`];
11893
}
94+
return [`conda activate ${condaEnv.toCommandArgumentForPythonExt()}`];
11995
}
12096

12197
switch (targetShell) {

src/test/common/terminals/activation.conda.unit.test.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import { expect } from 'chai';
55
import * as path from 'path';
6-
import { parse } from 'semver';
76
import { anything, instance, mock, when } from 'ts-mockito';
87
import * as TypeMoq from 'typemoq';
98
import { Disposable } from 'vscode';
@@ -145,37 +144,6 @@ suite('Terminal Environment Activation conda', () => {
145144
expect(activationCommands).to.deep.equal(expected, 'Incorrect Activation command');
146145
});
147146

148-
test('Conda activation on bash uses "source" before 4.4.0', async () => {
149-
const envName = 'EnvA';
150-
const pythonPath = 'python3';
151-
const condaPath = path.join('a', 'b', 'c', 'conda');
152-
platformService.setup((p) => p.isWindows).returns(() => false);
153-
condaService.reset();
154-
componentAdapter
155-
.setup((c) => c.getCondaEnvironment(TypeMoq.It.isAny()))
156-
.returns(() =>
157-
Promise.resolve({
158-
name: envName,
159-
path: path.dirname(pythonPath),
160-
}),
161-
);
162-
condaService.setup((c) => c.getCondaFile()).returns(() => Promise.resolve(condaPath));
163-
condaService.setup((c) => c.getCondaVersion()).returns(() => Promise.resolve(parse('4.3.1', true)!));
164-
const expected = [
165-
`source ${path.join(path.dirname(condaPath), 'activate').fileToCommandArgumentForPythonExt()} EnvA`,
166-
];
167-
168-
const provider = new CondaActivationCommandProvider(
169-
condaService.object,
170-
platformService.object,
171-
configService.object,
172-
componentAdapter.object,
173-
);
174-
const activationCommands = await provider.getActivationCommands(undefined, TerminalShellType.bash);
175-
176-
expect(activationCommands).to.deep.equal(expected, 'Incorrect Activation command');
177-
});
178-
179147
test('Conda activation on bash uses "conda" after 4.4.0', async () => {
180148
const envName = 'EnvA';
181149
const pythonPath = 'python3';
@@ -191,7 +159,6 @@ suite('Terminal Environment Activation conda', () => {
191159
}),
192160
);
193161
condaService.setup((c) => c.getCondaFile()).returns(() => Promise.resolve(condaPath));
194-
condaService.setup((c) => c.getCondaVersion()).returns(() => Promise.resolve(parse('4.4.0', true)!));
195162
const expected = [
196163
`source ${path.join(path.dirname(condaPath), 'activate').fileToCommandArgumentForPythonExt()} EnvA`,
197164
];
@@ -308,7 +275,6 @@ suite('Terminal Environment Activation conda', () => {
308275
path: path.dirname(pythonPath),
309276
}),
310277
);
311-
condaService.setup((c) => c.getCondaVersion()).returns(() => Promise.resolve(parse('4.4.0', true)!));
312278
condaService
313279
.setup((c) => c.getCondaFileFromInterpreter(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
314280
.returns(() => Promise.resolve(interpreterPath));

0 commit comments

Comments
 (0)