diff --git a/news/2 Fixes/16893.md b/news/2 Fixes/16893.md new file mode 100644 index 000000000000..39878f4ba792 --- /dev/null +++ b/news/2 Fixes/16893.md @@ -0,0 +1,2 @@ +Fix environment sorting for the `Python: Select Interpreter` command. +(thanks [Marc Mueller](https://github.com/cdce8p)) diff --git a/src/client/interpreter/configuration/environmentTypeComparer.ts b/src/client/interpreter/configuration/environmentTypeComparer.ts index ac0eaae0cd9a..df7a46fd576f 100644 --- a/src/client/interpreter/configuration/environmentTypeComparer.ts +++ b/src/client/interpreter/configuration/environmentTypeComparer.ts @@ -168,7 +168,11 @@ function compareEnvironmentType(a: PythonEnvironment, b: PythonEnvironment, work export function getEnvTypeHeuristic(environment: PythonEnvironment, workspacePath: string): EnvTypeHeuristic { const { envType } = environment; - if (workspacePath.length > 0 && environment.envPath && isParentPath(environment.envPath, workspacePath)) { + if ( + workspacePath.length > 0 && + ((environment.envPath && isParentPath(environment.envPath, workspacePath)) || + (environment.path && isParentPath(environment.path, workspacePath))) + ) { return EnvTypeHeuristic.Local; } diff --git a/src/test/configuration/environmentTypeComparer.unit.test.ts b/src/test/configuration/environmentTypeComparer.unit.test.ts index ef4fc630e5f6..c52a2aea584d 100644 --- a/src/test/configuration/environmentTypeComparer.unit.test.ts +++ b/src/test/configuration/environmentTypeComparer.unit.test.ts @@ -266,6 +266,18 @@ suite('getEnvTypeHeuristic tests', () => { assert.strictEqual(envTypeHeuristic, EnvTypeHeuristic.Global); }); + + test('If envPath is not set, fallback to path', () => { + const environment = { + envType, + path: path.join(workspacePath, 'my-environment'), + version: { major: 3, minor: 10, patch: 2 }, + } as PythonEnvironment; + + const envTypeHeuristic = getEnvTypeHeuristic(environment, workspacePath); + + assert.strictEqual(envTypeHeuristic, EnvTypeHeuristic.Local); + }); }); const globalInterpretersEnvTypes = [