From cca40116c296d1e281a29865925543ec14094999 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Tue, 20 Jul 2021 12:13:03 -0700 Subject: [PATCH 1/3] Fix autoselection when opening a python file directly --- src/client/common/configSettings.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) 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) { From c16afef4a1b7359de2fcca0863114ac9e96702f6 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Tue, 20 Jul 2021 12:25:16 -0700 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) 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 From 5d07b02e0094a5dd73a67326547172ed60f69e65 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Tue, 20 Jul 2021 12:56:17 -0700 Subject: [PATCH 3/3] Add tests --- .../configSettings.pythonPath.unit.test.ts | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) 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);