diff --git a/news/2 Fixes/4243.md b/news/2 Fixes/4243.md new file mode 100644 index 000000000000..32b54fd50125 --- /dev/null +++ b/news/2 Fixes/4243.md @@ -0,0 +1 @@ +Conda activation fails when there is a space in the env name diff --git a/src/client/common/terminal/environmentActivationProviders/condaActivationProvider.ts b/src/client/common/terminal/environmentActivationProviders/condaActivationProvider.ts index 8f9c4b2459d9..e7b5732b3f97 100644 --- a/src/client/common/terminal/environmentActivationProviders/condaActivationProvider.ts +++ b/src/client/common/terminal/environmentActivationProviders/condaActivationProvider.ts @@ -67,7 +67,7 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman `source ${activatePath}`; return [ firstActivate, - `conda activate ${envInfo.name}` + `conda activate ${envInfo.name.toCommandArgument()}` ]; } } diff --git a/src/test/common/terminals/activation.conda.unit.test.ts b/src/test/common/terminals/activation.conda.unit.test.ts index b6f70192abf2..d766a41bc650 100644 --- a/src/test/common/terminals/activation.conda.unit.test.ts +++ b/src/test/common/terminals/activation.conda.unit.test.ts @@ -170,6 +170,59 @@ suite('Terminal Environment Activation conda', () => { expect(activationCommands).to.deep.equal(expected, 'Incorrect Activation command'); }); + const interpreterPath = path.join('path', 'to', 'interpreter'); + const environmentName = 'Env'; + const environmentNameHasSpaces = 'Env with spaces'; + const testsForActivationUsingInterpreterPath = + [ + { + testName: 'Activation provides correct activation commands (windows) after 4.4.0 given interpreter path is provided, with no spaces in env name', + envName: environmentName, + expectedResult: ['path/to/activate', 'conda activate Env'], + isWindows: true + }, + { + testName: 'Activation provides correct activation commands (non-windows) after 4.4.0 given interpreter path is provided, with no spaces in env name', + envName: environmentName, + expectedResult: ['source path/to/activate', 'conda activate Env'], + isWindows: false + }, + { + testName: 'Activation provides correct activation commands (windows) after 4.4.0 given interpreter path is provided, with spaces in env name', + envName: environmentNameHasSpaces, + expectedResult: ['path/to/activate', 'conda activate \"Env with spaces\"'], + isWindows: true + }, + { + testName: 'Activation provides correct activation commands (non-windows) after 4.4.0 given interpreter path is provided, with spaces in env name', + envName: environmentNameHasSpaces, + expectedResult: ['source path/to/activate', 'conda activate \"Env with spaces\"'], + isWindows: false + } + ]; + + testsForActivationUsingInterpreterPath.forEach((testParams) => { + test(testParams.testName, async () => { + const pythonPath = 'python3'; + platformService.setup(p => p.isWindows).returns(() => testParams.isWindows); + condaService.reset(); + condaService.setup(c => c.getCondaEnvironment(TypeMoq.It.isAny())) + .returns(() => Promise.resolve({ + name: testParams.envName, + path: path.dirname(pythonPath) + })); + condaService.setup(c => c.getCondaVersion()) + .returns(() => Promise.resolve(parse('4.4.0', true)!)); + condaService.setup(c => c.getCondaFileFromInterpreter(TypeMoq.It.isAny(), TypeMoq.It.isAny())) + .returns(() => Promise.resolve(interpreterPath)); + + const provider = new CondaActivationCommandProvider(condaService.object, platformService.object, configService.object); + const activationCommands = await provider.getActivationCommands(undefined, TerminalShellType.bash); + + expect(activationCommands).to.deep.equal(testParams.expectedResult, 'Incorrect Activation command'); + }); + }); + async function expectNoCondaActivationCommandForPowershell(isWindows: boolean, isOsx: boolean, isLinux: boolean, pythonPath: string, shellType: TerminalShellType, hasSpaceInEnvironmentName = false) { terminalSettings.setup(t => t.activateEnvironment).returns(() => true); platformService.setup(p => p.isLinux).returns(() => isLinux);