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

Display problematic environments towards the end of the interpreters list #19588

Merged
merged 1 commit into from
Aug 1, 2022
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
13 changes: 13 additions & 0 deletions src/client/interpreter/configuration/environmentTypeComparer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export class EnvironmentTypeComparer implements IInterpreterComparer {
* Always sort with newest version of Python first within each subgroup.
*/
public compare(a: PythonEnvironment, b: PythonEnvironment): number {
if (isProblematicCondaEnvironment(a)) {
return 1;
}
if (isProblematicCondaEnvironment(b)) {
return -1;
}
// Check environment location.
const envLocationComparison = compareEnvironmentLocation(a, b, this.workspaceFolderPath);
if (envLocationComparison !== 0) {
Expand Down Expand Up @@ -84,6 +90,9 @@ export class EnvironmentTypeComparer implements IInterpreterComparer {
// because we would have to add a way to match environments to a workspace.
const workspaceUri = this.interpreterHelper.getActiveWorkspaceUri(resource);
const filteredInterpreters = interpreters.filter((i) => {
if (isProblematicCondaEnvironment(i)) {
return false;
}
if (getEnvLocationHeuristic(i, workspaceUri?.folderUri.fsPath || '') === EnvLocationHeuristic.Local) {
return true;
}
Expand Down Expand Up @@ -152,6 +161,10 @@ function isBaseCondaEnvironment(environment: PythonEnvironment): boolean {
);
}

export function isProblematicCondaEnvironment(environment: PythonEnvironment): boolean {
return environment.envType === EnvironmentType.Conda && environment.path === 'python';
}

/**
* Compare 2 Python versions in decending order, most recent one comes first.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { EnvironmentType } from '../../../../pythonEnvironments/info';
import { captureTelemetry, sendTelemetryEvent } from '../../../../telemetry';
import { EventName } from '../../../../telemetry/constants';
import { IInterpreterService, PythonEnvironmentsChangedEvent } from '../../../contracts';
import { isProblematicCondaEnvironment } from '../../environmentTypeComparer';
import {
IInterpreterQuickPickItem,
IInterpreterSelector,
Expand Down Expand Up @@ -354,11 +355,7 @@ export class SetInterpreterCommand extends BaseInterpreterSelectorCommand {
this.setRecommendedItem(interpreterSuggestions, items, resource);
// Add warning label to certain environments
items.forEach((item, i) => {
if (
isInterpreterQuickPickItem(item) &&
item.interpreter.path === 'python' &&
item.interpreter.envType === EnvironmentType.Conda
) {
if (isInterpreterQuickPickItem(item) && isProblematicCondaEnvironment(item.interpreter)) {
if (!items[i].label.includes(Octicons.Warning)) {
items[i].label = `${Octicons.Warning} ${items[i].label}`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function resolveBasicEnv(env: BasicEnvInfo, useCache = false): Prom

function getSearchLocation(env: PythonEnvInfo): Uri | undefined {
const folders = getWorkspaceFolders();
const isRootedEnv = folders.some((f) => isParentPath(env.executable.filename, f));
const isRootedEnv = folders.some((f) => isParentPath(env.executable.filename, f) || isParentPath(env.location, f));
if (isRootedEnv) {
// For environments inside roots, we need to set search location so they can be queried accordingly.
// In certain usecases environment directory can itself be a root, for eg. `python -m venv .`.
Expand Down
13 changes: 13 additions & 0 deletions src/test/configuration/environmentTypeComparer.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,19 @@ suite('Environment sorting', () => {
} as PythonEnvironment,
expected: 1,
},
{
title: 'Problematic environments should come last',
envA: {
envType: EnvironmentType.Conda,
envPath: path.join(workspacePath, '.venv'),
path: 'python',
} as PythonEnvironment,
envB: {
envType: EnvironmentType.System,
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
expected: 1,
},
];

testcases.forEach(({ title, envA, envB, expected }) => {
Expand Down