diff --git a/CHANGELOG.md b/CHANGELOG.md index 06094bd75be0..4d65bff4a7a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ ([#16615](https://github.com/Microsoft/vscode-python/issues/16615)) 1. Ensure we block on autoselection when no interpreter is explictly set by user. ([#16723](https://github.com/Microsoft/vscode-python/issues/16723)) +1. Fix autoselection when opening a python file directly. + ([#16733](https://github.com/Microsoft/vscode-python/issues/16733)) ### Thanks diff --git a/src/client/common/configSettings.ts b/src/client/common/configSettings.ts index 2bc5cfb24dfd..fc06923aba8e 100644 --- a/src/client/common/configSettings.ts +++ b/src/client/common/configSettings.ts @@ -679,18 +679,13 @@ export class PythonSettings implements IPythonSettings { const autoSelectedPythonInterpreter = this.interpreterAutoSelectionService.getAutoSelectedInterpreter( this.workspaceRoot, ); - if (inExperiment) { - if (autoSelectedPythonInterpreter && this.workspaceRoot) { - this.pythonPath = autoSelectedPythonInterpreter.path; + if (autoSelectedPythonInterpreter) { + this.pythonPath = autoSelectedPythonInterpreter.path; + if (this.workspaceRoot) { this.interpreterAutoSelectionService .setWorkspaceInterpreter(this.workspaceRoot, autoSelectedPythonInterpreter) .ignoreErrors(); } - } else if (autoSelectedPythonInterpreter && this.workspaceRoot) { - this.pythonPath = autoSelectedPythonInterpreter.path; - this.interpreterAutoSelectionService - .setWorkspaceInterpreter(this.workspaceRoot, autoSelectedPythonInterpreter) - .ignoreErrors(); } } if (inExperiment && this.pythonPath === DEFAULT_INTERPRETER_SETTING) { diff --git a/src/test/common/configSettings/configSettings.pythonPath.unit.test.ts b/src/test/common/configSettings/configSettings.pythonPath.unit.test.ts index bced9e2f8c6d..06a58b65a053 100644 --- a/src/test/common/configSettings/configSettings.pythonPath.unit.test.ts +++ b/src/test/common/configSettings/configSettings.pythonPath.unit.test.ts @@ -6,7 +6,7 @@ import { expect } from 'chai'; import * as path from 'path'; import * as sinon from 'sinon'; -import { anything, instance, mock, when } from 'ts-mockito'; +import { anything, instance, mock, verify, when } from 'ts-mockito'; import * as typemoq from 'typemoq'; import { Uri, WorkspaceConfiguration } from 'vscode'; import { IWorkspaceService } from '../../../client/common/application/types'; @@ -118,7 +118,24 @@ suite('Python Settings - pythonPath', () => { expect(configSettings.pythonPath).to.be.equal('python'); }); - test("If we don't have a custom python path and we do have an auto selected interpreter, then use it", () => { + test("If a workspace is opened and if we don't have a custom python path but we do have an auto selected interpreter, then use it", () => { + const pythonPath = path.join(__dirname, 'this is a python path that was auto selected'); + const interpreter = { path: pythonPath } as PythonEnvironment; + const workspaceFolderUri = Uri.file(__dirname); + const selectionService = mock(MockAutoSelectionService); + when(selectionService.getAutoSelectedInterpreter(workspaceFolderUri)).thenReturn(interpreter); + when(selectionService.setWorkspaceInterpreter(workspaceFolderUri, anything())).thenResolve(); + configSettings = new CustomPythonSettings(workspaceFolderUri, instance(selectionService)); + pythonSettings + .setup((p) => p.get(typemoq.It.isValue('pythonPath'))) + .returns(() => 'python') + .verifiable(typemoq.Times.atLeast(1)); + configSettings.update(pythonSettings.object); + + expect(configSettings.pythonPath).to.be.equal(pythonPath); + verify(selectionService.setWorkspaceInterpreter(workspaceFolderUri, interpreter)).once(); // Verify we set the autoselected interpreter + }); + test("If no workspace is opened and we don't have a custom python path but we do have an auto selected interpreter, then use it", () => { const pythonPath = path.join(__dirname, 'this is a python path that was auto selected'); const interpreter = { path: pythonPath } as PythonEnvironment; const workspaceFolderUri = Uri.file(__dirname);