Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix activation of conda environments in Powershell #1536

Merged
merged 1 commit into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2 Fixes/1520.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes the issue where Conda environments created using the latest version of Anaconda are not activated in Powershell.
1 change: 1 addition & 0 deletions news/2 Fixes/1534.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix activation of environments with spaces in the python path when using Powershell.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { BaseActivationCommandProvider } from './baseActivationProvider';

@injectable()
export class CommandPromptAndPowerShell extends BaseActivationCommandProvider {
constructor( @inject(IServiceContainer) serviceContainer: IServiceContainer) {
constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer) {
super(serviceContainer);
}
public isShellSupported(targetShell: TerminalShellType): boolean {
Expand Down Expand Up @@ -41,7 +41,7 @@ export class CommandPromptAndPowerShell extends BaseActivationCommandProvider {
const powershellExe = targetShell === TerminalShellType.powershell ? 'powershell' : 'pwsh';
const activationCmd = scriptFile.fileToCommandArgument();
return [
`& cmd /k "${activationCmd} & ${powershellExe}"`
`& cmd /k "${activationCmd.replace(/"/g, '""')} & ${powershellExe}"`
];
} else {
// Powershell on non-windows os, we cannot execute the batch file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { IServiceContainer } from '../../../ioc/types';
import '../../extensions';
import { IPlatformService } from '../../platform/types';
import { IConfigurationService } from '../../types';
import { TerminalShellType } from '../types';
import { ITerminalActivationCommandProvider } from '../types';
import { ITerminalActivationCommandProvider, TerminalShellType } from '../types';

@injectable()
export class CondaActivationCommandProvider implements ITerminalActivationCommandProvider {
Expand All @@ -29,8 +28,15 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman

const isWindows = this.serviceContainer.get<IPlatformService>(IPlatformService).isWindows;
if (targetShell === TerminalShellType.powershell || targetShell === TerminalShellType.powershellCore) {
if (!isWindows) {
return;
}
// https://github.com/conda/conda/issues/626
return;
// On windows, the solution is to go into cmd, then run the batch (.bat) file and go back into powershell.
const powershellExe = targetShell === TerminalShellType.powershell ? 'powershell' : 'pwsh';
return [
`& cmd /k "activate ${envInfo.name.toCommandArgument().replace(/"/g, '""')} & ${powershellExe}"`
];
} else if (targetShell === TerminalShellType.fish) {
// https://github.com/conda/conda/blob/be8c08c083f4d5e05b06bd2689d2cd0d410c2ffe/shell/etc/fish/conf.d/conda.fish#L18-L28
return [`conda activate ${envInfo.name.toCommandArgument()}`];
Expand Down
8 changes: 4 additions & 4 deletions src/test/common/terminals/activation.commandPrompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ suite('Terminal Environment Activation (cmd/powershell)', () => {
});

test('Ensure batch files are supported by powershell (on windows)', async () => {
const bash = new CommandPromptAndPowerShell(serviceContainer.object);
const batch = new CommandPromptAndPowerShell(serviceContainer.object);

platform.setup(p => p.isWindows).returns(() => true);
const pathToScriptFile = path.join(path.dirname(pythonPath), 'activate.bat');
fileSystem.setup(fs => fs.fileExistsAsync(TypeMoq.It.isValue(pathToScriptFile))).returns(() => Promise.resolve(true));
const command = await bash.getActivationCommands(resource, TerminalShellType.powershell);
const command = await batch.getActivationCommands(resource, TerminalShellType.powershell);

// Executing batch files from powershell requires going back to cmd, then into powershell

const activationCommand = pathToScriptFile.fileToCommandArgument();
const commands = [`& cmd /k "${activationCommand} & powershell"`];
const commands = [`& cmd /k "${activationCommand.replace(/"/g, '""')} & powershell"`];
expect(command).to.be.deep.equal(commands, 'Invalid command');
});

Expand All @@ -122,7 +122,7 @@ suite('Terminal Environment Activation (cmd/powershell)', () => {
// Executing batch files from powershell requires going back to cmd, then into powershell

const activationCommand = pathToScriptFile.fileToCommandArgument();
const commands = [`& cmd /k "${activationCommand} & pwsh"`];
const commands = [`& cmd /k "${activationCommand.replace(/"/g, '""')} & pwsh"`];
expect(command).to.be.deep.equal(commands, 'Invalid command');
});

Expand Down
34 changes: 27 additions & 7 deletions src/test/common/terminals/activation.conda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as path from 'path';
import * as TypeMoq from 'typemoq';
import { Disposable } from 'vscode';
import { EnumEx } from '../../../client/common/enumUtils';
import '../../../client/common/extensions';
import { IFileSystem, IPlatformService } from '../../../client/common/platform/types';
import { IProcessService } from '../../../client/common/process/types';
import { CondaActivationCommandProvider } from '../../../client/common/terminal/environmentActivationProviders/condaActivationProvider';
Expand Down Expand Up @@ -67,29 +68,32 @@ suite('Terminal Environment Activation conda', () => {
expect(activationCommands).to.equal(undefined, 'Activation commands should be undefined');
});

async function expectNoCondaActivationCommandForPowershell(isWindows: boolean, isOsx: boolean, isLinux: boolean, pythonPath: string, shellType: TerminalShellType) {
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);
platformService.setup(p => p.isWindows).returns(() => isWindows);
platformService.setup(p => p.isMac).returns(() => isOsx);
condaService.setup(c => c.isCondaEnvironment(TypeMoq.It.isAny())).returns(() => Promise.resolve(true));
pythonSettings.setup(s => s.pythonPath).returns(() => pythonPath);
condaService.setup(c => c.getCondaEnvironment(TypeMoq.It.isAny())).returns(() => Promise.resolve({ name: 'EnvA', path: path.dirname(pythonPath) }));
const envName = hasSpaceInEnvironmentName ? 'EnvA' : 'Env A';
condaService.setup(c => c.getCondaEnvironment(TypeMoq.It.isAny())).returns(() => Promise.resolve({ name: envName, path: path.dirname(pythonPath) }));

const activationCommands = await new CondaActivationCommandProvider(serviceContainer.object).getActivationCommands(undefined, shellType);
let expectedActivationCommamnd: string[] | undefined;
switch (shellType) {
case TerminalShellType.powershell:
case TerminalShellType.powershellCore: {
expectedActivationCommamnd = undefined;
const powershellExe = shellType === TerminalShellType.powershell ? 'powershell' : 'pwsh';
const envNameForCmd = envName.toCommandArgument().replace(/"/g, '""');
expectedActivationCommamnd = isWindows ? [`& cmd /k \"activate ${envNameForCmd} & ${powershellExe}\"`] : undefined;
break;
}
case TerminalShellType.fish: {
expectedActivationCommamnd = ['conda activate EnvA'];
expectedActivationCommamnd = [`conda activate ${envName.toCommandArgument()}`];
break;
}
default: {
expectedActivationCommamnd = isWindows ? ['activate EnvA'] : ['source activate EnvA'];
expectedActivationCommamnd = isWindows ? [`activate ${envName.toCommandArgument()}`] : [`source activate ${envName.toCommandArgument()}`];
break;
}
}
Expand All @@ -101,16 +105,32 @@ suite('Terminal Environment Activation conda', () => {
await expectNoCondaActivationCommandForPowershell(true, false, false, pythonPath, shellType.value);
});

test(`Conda activation command for shell ${shellType.name} on (windows)`, async () => {
test(`Conda activation command for shell ${shellType.name} on (linux)`, async () => {
const pythonPath = path.join('users', 'xyz', '.conda', 'envs', 'enva', 'bin', 'python');
await expectNoCondaActivationCommandForPowershell(false, false, true, pythonPath, shellType.value);
});

test(`Conda activation command for shell ${shellType.name} on (linux)`, async () => {
test(`Conda activation command for shell ${shellType.name} on (mac)`, async () => {
const pythonPath = path.join('users', 'xyz', '.conda', 'envs', 'enva', 'bin', 'python');
await expectNoCondaActivationCommandForPowershell(false, true, false, pythonPath, shellType.value);
});
});
EnumEx.getNamesAndValues(TerminalShellType).forEach(shellType => {
test(`Conda activation command for shell ${shellType.name} on (windows), containing spaces in environment name`, async () => {
const pythonPath = path.join('c', 'users', 'xyz', '.conda', 'envs', 'enva', 'python.exe');
await expectNoCondaActivationCommandForPowershell(true, false, false, pythonPath, shellType.value, true);
});

test(`Conda activation command for shell ${shellType.name} on (linux), containing spaces in environment name`, async () => {
const pythonPath = path.join('users', 'xyz', '.conda', 'envs', 'enva', 'bin', 'python');
await expectNoCondaActivationCommandForPowershell(false, false, true, pythonPath, shellType.value, true);
});

test(`Conda activation command for shell ${shellType.name} on (mac), containing spaces in environment name`, async () => {
const pythonPath = path.join('users', 'xyz', '.conda', 'envs', 'enva', 'bin', 'python');
await expectNoCondaActivationCommandForPowershell(false, true, false, pythonPath, shellType.value, true);
});
});
async function expectCondaActivationCommand(isWindows: boolean, isOsx: boolean, isLinux: boolean, pythonPath: string) {
terminalSettings.setup(t => t.activateEnvironment).returns(() => true);
platformService.setup(p => p.isLinux).returns(() => isLinux);
Expand Down