Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import '../../extensions';
import { inject, injectable } from 'inversify';
import * as path from 'path';
import { Uri } from 'vscode';
import { traceInfo, traceVerbose, traceWarn } from '../../../logging';

import { IComponentAdapter, ICondaService } from '../../../interpreter/contracts';
import { IPlatformService } from '../../platform/types';
Expand Down Expand Up @@ -53,28 +54,36 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
pythonPath: string,
targetShell: TerminalShellType,
): Promise<string[] | undefined> {
traceVerbose(`Getting conda activation commands for interpreter ${pythonPath} with shell ${targetShell}`);
const envInfo = await this.pyenvs.getCondaEnvironment(pythonPath);
if (!envInfo) {
traceWarn(`No conda environment found for interpreter ${pythonPath}`);
return undefined;
}
traceVerbose(`Found conda environment: ${JSON.stringify(envInfo)}`);

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

// New version.
const interpreterPath = await this.condaService.getInterpreterPathForEnvironment(envInfo);
traceInfo(`Using interpreter path: ${interpreterPath}`);
const activatePath = await this.condaService.getActivationScriptFromInterpreter(interpreterPath, envInfo.name);
traceVerbose(`Got activation script: ${activatePath?.path}} with type: ${activatePath?.type}`);
// eslint-disable-next-line camelcase
if (activatePath?.path) {
if (
this.platform.isWindows &&
targetShell !== TerminalShellType.bash &&
targetShell !== TerminalShellType.gitbash
) {
return [activatePath.path, `conda activate ${condaEnv.toCommandArgumentForPythonExt()}`];
const commands = [activatePath.path, `conda activate ${condaEnv.toCommandArgumentForPythonExt()}`];
traceInfo(`Using Windows-specific commands: ${commands.join(', ')}`);
return commands;
}

const condaInfo = await this.condaService.getCondaInfo();

traceVerbose(`Conda shell level: ${condaInfo?.conda_shlvl}`);
if (
activatePath.type !== 'global' ||
// eslint-disable-next-line camelcase
Expand All @@ -84,27 +93,36 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
// activatePath is not the global activate path, or we don't have a shlvl, or it's -1(conda never sourced).
// and we need to source the activate path.
if (activatePath.path === 'activate') {
return [
const commands = [
`source ${activatePath.path}`,
`conda activate ${condaEnv.toCommandArgumentForPythonExt()}`,
];
traceInfo(`Using source activate commands: ${commands.join(', ')}`);
return commands;
}
return [`source ${activatePath.path} ${condaEnv.toCommandArgumentForPythonExt()}`];
const command = [`source ${activatePath.path} ${condaEnv.toCommandArgumentForPythonExt()}`];
traceInfo(`Using single source command: ${command}`);
return command;
}
return [`conda activate ${condaEnv.toCommandArgumentForPythonExt()}`];
const command = [`conda activate ${condaEnv.toCommandArgumentForPythonExt()}`];
traceInfo(`Using direct conda activate command: ${command}`);
return command;
}

switch (targetShell) {
case TerminalShellType.powershell:
case TerminalShellType.powershellCore:
traceVerbose('Using PowerShell-specific activation');
return _getPowershellCommands(condaEnv);

// TODO: Do we really special-case fish on Windows?
case TerminalShellType.fish:
traceVerbose('Using Fish shell-specific activation');
return getFishCommands(condaEnv, await this.condaService.getCondaFile());

default:
if (this.platform.isWindows) {
traceVerbose('Using Windows shell-specific activation fallback option.');
return this.getWindowsCommands(condaEnv);
}
return getUnixCommands(condaEnv, await this.condaService.getCondaFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { inject, injectable } from 'inversify';
import * as path from 'path';
import { SemVer } from 'semver';
import { IFileSystem, IPlatformService } from '../../../common/platform/types';
import { traceVerbose } from '../../../logging';
import { cache } from '../../../common/utils/decorators';
import { ICondaService } from '../../../interpreter/contracts';
import { traceDecoratorVerbose } from '../../../logging';
Expand All @@ -23,12 +24,15 @@ export class CondaService implements ICondaService {
interpreterPath?: string,
envName?: string,
): Promise<{ path: string | undefined; type: 'local' | 'global' } | undefined> {
traceVerbose(`Getting activation script for interpreter ${interpreterPath}, env ${envName}`);
const condaPath = await this.getCondaFileFromInterpreter(interpreterPath, envName);
traceVerbose(`Found conda path: ${condaPath}`);

const activatePath = (condaPath
? path.join(path.dirname(condaPath), 'activate')
: 'activate'
).fileToCommandArgumentForPythonExt(); // maybe global activate?
traceVerbose(`Using activate path: ${activatePath}`);

// try to find the activate script in the global conda root prefix.
if (this.platform.isLinux || this.platform.isMac) {
Expand All @@ -41,6 +45,7 @@ export class CondaService implements ICondaService {
.fileToCommandArgumentForPythonExt();

if (activatePath === globalActivatePath || !(await this.fileSystem.fileExists(activatePath))) {
traceVerbose(`Using global activate path: ${globalActivatePath}`);
return {
path: globalActivatePath,
type: 'global',
Expand Down
Loading