Skip to content

Commit

Permalink
Do not prefer Microsoft store interpreter over other global interpret…
Browse files Browse the repository at this point in the history
…ers when auto-selecting (#22380)

Closes #22364

Do not compare two global interpreters using their kinds.
  • Loading branch information
Kartik Raj authored Oct 30, 2023
1 parent b4f06c9 commit a7262b8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ export function getEnvLocationHeuristic(environment: PythonEnvironment, workspac
* Compare 2 environment types: return 0 if they are the same, -1 if a comes before b, 1 otherwise.
*/
function compareEnvironmentType(a: PythonEnvironment, b: PythonEnvironment): number {
if (!a.type && !b.type) {
// Return 0 if two global interpreters are being compared.
return 0;
}
const envTypeByPriority = getPrioritizedEnvironmentType();
return Math.sign(envTypeByPriority.indexOf(a.envType) - envTypeByPriority.indexOf(b.envType));
}
Expand Down
36 changes: 35 additions & 1 deletion src/test/configuration/environmentTypeComparer.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getEnvLocationHeuristic,
} from '../../client/interpreter/configuration/environmentTypeComparer';
import { IInterpreterHelper } from '../../client/interpreter/contracts';
import { PythonEnvType } from '../../client/pythonEnvironments/base/info';
import { EnvironmentType, PythonEnvironment } from '../../client/pythonEnvironments/info';

suite('Environment sorting', () => {
Expand Down Expand Up @@ -45,6 +46,7 @@ suite('Environment sorting', () => {
title: 'Local virtual environment should come first',
envA: {
envType: EnvironmentType.Venv,
type: PythonEnvType.Virtual,
envPath: path.join(workspacePath, '.venv'),
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
Expand All @@ -58,11 +60,13 @@ suite('Environment sorting', () => {
title: "Non-local virtual environment should not come first when there's a local env",
envA: {
envType: EnvironmentType.Venv,
type: PythonEnvType.Virtual,
envPath: path.join('path', 'to', 'other', 'workspace', '.venv'),
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
envB: {
envType: EnvironmentType.Venv,
type: PythonEnvType.Virtual,
envPath: path.join(workspacePath, '.venv'),
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
Expand All @@ -72,10 +76,12 @@ suite('Environment sorting', () => {
title: "Conda environment should not come first when there's a local env",
envA: {
envType: EnvironmentType.Conda,
type: PythonEnvType.Conda,
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
envB: {
envType: EnvironmentType.Venv,
type: PythonEnvType.Virtual,
envPath: path.join(workspacePath, '.venv'),
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
Expand All @@ -85,11 +91,13 @@ suite('Environment sorting', () => {
title: 'Conda base environment should come after any other conda env',
envA: {
envType: EnvironmentType.Conda,
type: PythonEnvType.Conda,
envName: 'base',
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
envB: {
envType: EnvironmentType.Conda,
type: PythonEnvType.Conda,
envName: 'random-name',
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
Expand All @@ -99,6 +107,7 @@ suite('Environment sorting', () => {
title: 'Pipenv environment should come before any other conda env',
envA: {
envType: EnvironmentType.Conda,
type: PythonEnvType.Conda,
envName: 'conda-env',
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
Expand All @@ -118,19 +127,21 @@ suite('Environment sorting', () => {
} as PythonEnvironment,
envB: {
envType: EnvironmentType.Poetry,
type: PythonEnvType.Virtual,
envName: 'poetry-env',
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
expected: 1,
},
{
title: 'Pyenv environment should not come first when there are global envs',
title: 'Pyenv interpreter should not come first when there are global envs',
envA: {
envType: EnvironmentType.Pyenv,
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
envB: {
envType: EnvironmentType.Pipenv,
type: PythonEnvType.Virtual,
envName: 'pipenv-env',
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
Expand All @@ -144,6 +155,7 @@ suite('Environment sorting', () => {
} as PythonEnvironment,
envB: {
envType: EnvironmentType.Poetry,
type: PythonEnvType.Virtual,
envName: 'poetry-env',
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
Expand All @@ -157,11 +169,25 @@ suite('Environment sorting', () => {
} as PythonEnvironment,
envB: {
envType: EnvironmentType.VirtualEnv,
type: PythonEnvType.Virtual,
envName: 'virtualenv-env',
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
expected: 1,
},
{
title:
'Microsoft Store interpreter should not come first when there are global interpreters with higher version',
envA: {
envType: EnvironmentType.MicrosoftStore,
version: { major: 3, minor: 10, patch: 2, raw: '3.10.2' },
} as PythonEnvironment,
envB: {
envType: EnvironmentType.Global,
version: { major: 3, minor: 11, patch: 2, raw: '3.11.2' },
} as PythonEnvironment,
expected: 1,
},
{
title: 'Unknown environment should not come first when there are global envs',
envA: {
Expand All @@ -170,6 +196,7 @@ suite('Environment sorting', () => {
} as PythonEnvironment,
envB: {
envType: EnvironmentType.Pipenv,
type: PythonEnvType.Virtual,
envName: 'pipenv-env',
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
Expand All @@ -179,11 +206,13 @@ suite('Environment sorting', () => {
title: 'If 2 environments are of the same type, the most recent Python version comes first',
envA: {
envType: EnvironmentType.Venv,
type: PythonEnvType.Virtual,
envPath: path.join(workspacePath, '.old-venv'),
version: { major: 3, minor: 7, patch: 5, raw: '3.7.5' },
} as PythonEnvironment,
envB: {
envType: EnvironmentType.Venv,
type: PythonEnvType.Virtual,
envPath: path.join(workspacePath, '.venv'),
version: { major: 3, minor: 10, patch: 2, raw: '3.10.2' },
} as PythonEnvironment,
Expand All @@ -194,11 +223,13 @@ suite('Environment sorting', () => {
"If 2 global environments have the same Python version and there's a Conda one, the Conda env should not come first",
envA: {
envType: EnvironmentType.Conda,
type: PythonEnvType.Conda,
envName: 'conda-env',
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
envB: {
envType: EnvironmentType.Pipenv,
type: PythonEnvType.Virtual,
envName: 'pipenv-env',
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
Expand All @@ -209,11 +240,13 @@ suite('Environment sorting', () => {
'If 2 global environments are of the same type and have the same Python version, they should be sorted by name',
envA: {
envType: EnvironmentType.Conda,
type: PythonEnvType.Conda,
envName: 'conda-foo',
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
envB: {
envType: EnvironmentType.Conda,
type: PythonEnvType.Conda,
envName: 'conda-bar',
version: { major: 3, minor: 10, patch: 2 },
} as PythonEnvironment,
Expand All @@ -237,6 +270,7 @@ suite('Environment sorting', () => {
title: 'Problematic environments should come last',
envA: {
envType: EnvironmentType.Conda,
type: PythonEnvType.Conda,
envPath: path.join(workspacePath, '.venv'),
path: 'python',
} as PythonEnvironment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { EnvironmentTypeComparer } from '../../../client/interpreter/configurati
import { InterpreterSelector } from '../../../client/interpreter/configuration/interpreterSelector/interpreterSelector';
import { IInterpreterComparer, IInterpreterQuickPickItem } from '../../../client/interpreter/configuration/types';
import { IInterpreterHelper, IInterpreterService, WorkspacePythonPath } from '../../../client/interpreter/contracts';
import { PythonEnvType } from '../../../client/pythonEnvironments/base/info';
import { EnvironmentType, PythonEnvironment } from '../../../client/pythonEnvironments/info';
import { getOSType, OSType } from '../../common';

Expand Down Expand Up @@ -139,12 +140,14 @@ suite('Interpreters - selector', () => {
envPath: path.join('path', 'to', 'another', 'workspace', '.venv'),
path: path.join('path', 'to', 'another', 'workspace', '.venv', 'bin', 'python'),
envType: EnvironmentType.Venv,
type: PythonEnvType.Virtual,
},
{
displayName: 'two',
envPath: path.join(workspacePath, '.venv'),
path: path.join(workspacePath, '.venv', 'bin', 'python'),
envType: EnvironmentType.Venv,
type: PythonEnvType.Virtual,
},
{
displayName: 'three',
Expand All @@ -158,6 +161,7 @@ suite('Interpreters - selector', () => {
path: path.join('a', 'conda', 'environment'),
envName: 'conda-env',
envType: EnvironmentType.Conda,
type: PythonEnvType.Conda,
},
].map((item) => ({ ...info, ...item }));

Expand Down
3 changes: 3 additions & 0 deletions src/test/interpreters/autoSelection/index.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { EnvironmentTypeComparer } from '../../../client/interpreter/configurati
import { IInterpreterHelper, IInterpreterService, WorkspacePythonPath } from '../../../client/interpreter/contracts';
import { InterpreterHelper } from '../../../client/interpreter/helpers';
import { InterpreterService } from '../../../client/interpreter/interpreterService';
import { PythonEnvType } from '../../../client/pythonEnvironments/base/info';
import { EnvironmentType, PythonEnvironment } from '../../../client/pythonEnvironments/info';
import * as Telemetry from '../../../client/telemetry';
import { EventName } from '../../../client/telemetry/constants';
Expand Down Expand Up @@ -150,13 +151,15 @@ suite('Interpreters - Auto Selection', () => {
test('If there is a local environment select it', async () => {
const localEnv = {
envType: EnvironmentType.Venv,
type: PythonEnvType.Virtual,
envPath: path.join(workspacePath, '.venv'),
version: { major: 3, minor: 10, patch: 0 },
} as PythonEnvironment;

when(interpreterService.getInterpreters(resource)).thenCall((_) => [
{
envType: EnvironmentType.Conda,
type: PythonEnvType.Conda,
envPath: path.join('some', 'conda', 'env'),
version: { major: 3, minor: 7, patch: 2 },
} as PythonEnvironment,
Expand Down

0 comments on commit a7262b8

Please sign in to comment.