Skip to content

Commit a7262b8

Browse files
author
Kartik Raj
authored
Do not prefer Microsoft store interpreter over other global interpreters when auto-selecting (#22380)
Closes #22364 Do not compare two global interpreters using their kinds.
1 parent b4f06c9 commit a7262b8

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

src/client/interpreter/configuration/environmentTypeComparer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ export function getEnvLocationHeuristic(environment: PythonEnvironment, workspac
234234
* Compare 2 environment types: return 0 if they are the same, -1 if a comes before b, 1 otherwise.
235235
*/
236236
function compareEnvironmentType(a: PythonEnvironment, b: PythonEnvironment): number {
237+
if (!a.type && !b.type) {
238+
// Return 0 if two global interpreters are being compared.
239+
return 0;
240+
}
237241
const envTypeByPriority = getPrioritizedEnvironmentType();
238242
return Math.sign(envTypeByPriority.indexOf(a.envType) - envTypeByPriority.indexOf(b.envType));
239243
}

src/test/configuration/environmentTypeComparer.unit.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
getEnvLocationHeuristic,
1212
} from '../../client/interpreter/configuration/environmentTypeComparer';
1313
import { IInterpreterHelper } from '../../client/interpreter/contracts';
14+
import { PythonEnvType } from '../../client/pythonEnvironments/base/info';
1415
import { EnvironmentType, PythonEnvironment } from '../../client/pythonEnvironments/info';
1516

1617
suite('Environment sorting', () => {
@@ -45,6 +46,7 @@ suite('Environment sorting', () => {
4546
title: 'Local virtual environment should come first',
4647
envA: {
4748
envType: EnvironmentType.Venv,
49+
type: PythonEnvType.Virtual,
4850
envPath: path.join(workspacePath, '.venv'),
4951
version: { major: 3, minor: 10, patch: 2 },
5052
} as PythonEnvironment,
@@ -58,11 +60,13 @@ suite('Environment sorting', () => {
5860
title: "Non-local virtual environment should not come first when there's a local env",
5961
envA: {
6062
envType: EnvironmentType.Venv,
63+
type: PythonEnvType.Virtual,
6164
envPath: path.join('path', 'to', 'other', 'workspace', '.venv'),
6265
version: { major: 3, minor: 10, patch: 2 },
6366
} as PythonEnvironment,
6467
envB: {
6568
envType: EnvironmentType.Venv,
69+
type: PythonEnvType.Virtual,
6670
envPath: path.join(workspacePath, '.venv'),
6771
version: { major: 3, minor: 10, patch: 2 },
6872
} as PythonEnvironment,
@@ -72,10 +76,12 @@ suite('Environment sorting', () => {
7276
title: "Conda environment should not come first when there's a local env",
7377
envA: {
7478
envType: EnvironmentType.Conda,
79+
type: PythonEnvType.Conda,
7580
version: { major: 3, minor: 10, patch: 2 },
7681
} as PythonEnvironment,
7782
envB: {
7883
envType: EnvironmentType.Venv,
84+
type: PythonEnvType.Virtual,
7985
envPath: path.join(workspacePath, '.venv'),
8086
version: { major: 3, minor: 10, patch: 2 },
8187
} as PythonEnvironment,
@@ -85,11 +91,13 @@ suite('Environment sorting', () => {
8591
title: 'Conda base environment should come after any other conda env',
8692
envA: {
8793
envType: EnvironmentType.Conda,
94+
type: PythonEnvType.Conda,
8895
envName: 'base',
8996
version: { major: 3, minor: 10, patch: 2 },
9097
} as PythonEnvironment,
9198
envB: {
9299
envType: EnvironmentType.Conda,
100+
type: PythonEnvType.Conda,
93101
envName: 'random-name',
94102
version: { major: 3, minor: 10, patch: 2 },
95103
} as PythonEnvironment,
@@ -99,6 +107,7 @@ suite('Environment sorting', () => {
99107
title: 'Pipenv environment should come before any other conda env',
100108
envA: {
101109
envType: EnvironmentType.Conda,
110+
type: PythonEnvType.Conda,
102111
envName: 'conda-env',
103112
version: { major: 3, minor: 10, patch: 2 },
104113
} as PythonEnvironment,
@@ -118,19 +127,21 @@ suite('Environment sorting', () => {
118127
} as PythonEnvironment,
119128
envB: {
120129
envType: EnvironmentType.Poetry,
130+
type: PythonEnvType.Virtual,
121131
envName: 'poetry-env',
122132
version: { major: 3, minor: 10, patch: 2 },
123133
} as PythonEnvironment,
124134
expected: 1,
125135
},
126136
{
127-
title: 'Pyenv environment should not come first when there are global envs',
137+
title: 'Pyenv interpreter should not come first when there are global envs',
128138
envA: {
129139
envType: EnvironmentType.Pyenv,
130140
version: { major: 3, minor: 10, patch: 2 },
131141
} as PythonEnvironment,
132142
envB: {
133143
envType: EnvironmentType.Pipenv,
144+
type: PythonEnvType.Virtual,
134145
envName: 'pipenv-env',
135146
version: { major: 3, minor: 10, patch: 2 },
136147
} as PythonEnvironment,
@@ -144,6 +155,7 @@ suite('Environment sorting', () => {
144155
} as PythonEnvironment,
145156
envB: {
146157
envType: EnvironmentType.Poetry,
158+
type: PythonEnvType.Virtual,
147159
envName: 'poetry-env',
148160
version: { major: 3, minor: 10, patch: 2 },
149161
} as PythonEnvironment,
@@ -157,11 +169,25 @@ suite('Environment sorting', () => {
157169
} as PythonEnvironment,
158170
envB: {
159171
envType: EnvironmentType.VirtualEnv,
172+
type: PythonEnvType.Virtual,
160173
envName: 'virtualenv-env',
161174
version: { major: 3, minor: 10, patch: 2 },
162175
} as PythonEnvironment,
163176
expected: 1,
164177
},
178+
{
179+
title:
180+
'Microsoft Store interpreter should not come first when there are global interpreters with higher version',
181+
envA: {
182+
envType: EnvironmentType.MicrosoftStore,
183+
version: { major: 3, minor: 10, patch: 2, raw: '3.10.2' },
184+
} as PythonEnvironment,
185+
envB: {
186+
envType: EnvironmentType.Global,
187+
version: { major: 3, minor: 11, patch: 2, raw: '3.11.2' },
188+
} as PythonEnvironment,
189+
expected: 1,
190+
},
165191
{
166192
title: 'Unknown environment should not come first when there are global envs',
167193
envA: {
@@ -170,6 +196,7 @@ suite('Environment sorting', () => {
170196
} as PythonEnvironment,
171197
envB: {
172198
envType: EnvironmentType.Pipenv,
199+
type: PythonEnvType.Virtual,
173200
envName: 'pipenv-env',
174201
version: { major: 3, minor: 10, patch: 2 },
175202
} as PythonEnvironment,
@@ -179,11 +206,13 @@ suite('Environment sorting', () => {
179206
title: 'If 2 environments are of the same type, the most recent Python version comes first',
180207
envA: {
181208
envType: EnvironmentType.Venv,
209+
type: PythonEnvType.Virtual,
182210
envPath: path.join(workspacePath, '.old-venv'),
183211
version: { major: 3, minor: 7, patch: 5, raw: '3.7.5' },
184212
} as PythonEnvironment,
185213
envB: {
186214
envType: EnvironmentType.Venv,
215+
type: PythonEnvType.Virtual,
187216
envPath: path.join(workspacePath, '.venv'),
188217
version: { major: 3, minor: 10, patch: 2, raw: '3.10.2' },
189218
} as PythonEnvironment,
@@ -194,11 +223,13 @@ suite('Environment sorting', () => {
194223
"If 2 global environments have the same Python version and there's a Conda one, the Conda env should not come first",
195224
envA: {
196225
envType: EnvironmentType.Conda,
226+
type: PythonEnvType.Conda,
197227
envName: 'conda-env',
198228
version: { major: 3, minor: 10, patch: 2 },
199229
} as PythonEnvironment,
200230
envB: {
201231
envType: EnvironmentType.Pipenv,
232+
type: PythonEnvType.Virtual,
202233
envName: 'pipenv-env',
203234
version: { major: 3, minor: 10, patch: 2 },
204235
} as PythonEnvironment,
@@ -209,11 +240,13 @@ suite('Environment sorting', () => {
209240
'If 2 global environments are of the same type and have the same Python version, they should be sorted by name',
210241
envA: {
211242
envType: EnvironmentType.Conda,
243+
type: PythonEnvType.Conda,
212244
envName: 'conda-foo',
213245
version: { major: 3, minor: 10, patch: 2 },
214246
} as PythonEnvironment,
215247
envB: {
216248
envType: EnvironmentType.Conda,
249+
type: PythonEnvType.Conda,
217250
envName: 'conda-bar',
218251
version: { major: 3, minor: 10, patch: 2 },
219252
} as PythonEnvironment,
@@ -237,6 +270,7 @@ suite('Environment sorting', () => {
237270
title: 'Problematic environments should come last',
238271
envA: {
239272
envType: EnvironmentType.Conda,
273+
type: PythonEnvType.Conda,
240274
envPath: path.join(workspacePath, '.venv'),
241275
path: 'python',
242276
} as PythonEnvironment,

src/test/configuration/interpreterSelector/interpreterSelector.unit.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { EnvironmentTypeComparer } from '../../../client/interpreter/configurati
1414
import { InterpreterSelector } from '../../../client/interpreter/configuration/interpreterSelector/interpreterSelector';
1515
import { IInterpreterComparer, IInterpreterQuickPickItem } from '../../../client/interpreter/configuration/types';
1616
import { IInterpreterHelper, IInterpreterService, WorkspacePythonPath } from '../../../client/interpreter/contracts';
17+
import { PythonEnvType } from '../../../client/pythonEnvironments/base/info';
1718
import { EnvironmentType, PythonEnvironment } from '../../../client/pythonEnvironments/info';
1819
import { getOSType, OSType } from '../../common';
1920

@@ -139,12 +140,14 @@ suite('Interpreters - selector', () => {
139140
envPath: path.join('path', 'to', 'another', 'workspace', '.venv'),
140141
path: path.join('path', 'to', 'another', 'workspace', '.venv', 'bin', 'python'),
141142
envType: EnvironmentType.Venv,
143+
type: PythonEnvType.Virtual,
142144
},
143145
{
144146
displayName: 'two',
145147
envPath: path.join(workspacePath, '.venv'),
146148
path: path.join(workspacePath, '.venv', 'bin', 'python'),
147149
envType: EnvironmentType.Venv,
150+
type: PythonEnvType.Virtual,
148151
},
149152
{
150153
displayName: 'three',
@@ -158,6 +161,7 @@ suite('Interpreters - selector', () => {
158161
path: path.join('a', 'conda', 'environment'),
159162
envName: 'conda-env',
160163
envType: EnvironmentType.Conda,
164+
type: PythonEnvType.Conda,
161165
},
162166
].map((item) => ({ ...info, ...item }));
163167

src/test/interpreters/autoSelection/index.unit.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { EnvironmentTypeComparer } from '../../../client/interpreter/configurati
2323
import { IInterpreterHelper, IInterpreterService, WorkspacePythonPath } from '../../../client/interpreter/contracts';
2424
import { InterpreterHelper } from '../../../client/interpreter/helpers';
2525
import { InterpreterService } from '../../../client/interpreter/interpreterService';
26+
import { PythonEnvType } from '../../../client/pythonEnvironments/base/info';
2627
import { EnvironmentType, PythonEnvironment } from '../../../client/pythonEnvironments/info';
2728
import * as Telemetry from '../../../client/telemetry';
2829
import { EventName } from '../../../client/telemetry/constants';
@@ -150,13 +151,15 @@ suite('Interpreters - Auto Selection', () => {
150151
test('If there is a local environment select it', async () => {
151152
const localEnv = {
152153
envType: EnvironmentType.Venv,
154+
type: PythonEnvType.Virtual,
153155
envPath: path.join(workspacePath, '.venv'),
154156
version: { major: 3, minor: 10, patch: 0 },
155157
} as PythonEnvironment;
156158

157159
when(interpreterService.getInterpreters(resource)).thenCall((_) => [
158160
{
159161
envType: EnvironmentType.Conda,
162+
type: PythonEnvType.Conda,
160163
envPath: path.join('some', 'conda', 'env'),
161164
version: { major: 3, minor: 7, patch: 2 },
162165
} as PythonEnvironment,

0 commit comments

Comments
 (0)