Skip to content

Commit ae979b1

Browse files
author
Kartik Raj
authored
Fix autoselection when opening a python file directly (#16733)
* Fix autoselection when opening a python file directly * Update changelog * Add tests
1 parent 8253dc8 commit ae979b1

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
([#16615](https://github.com/Microsoft/vscode-python/issues/16615))
2929
1. Ensure we block on autoselection when no interpreter is explictly set by user.
3030
([#16723](https://github.com/Microsoft/vscode-python/issues/16723))
31+
1. Fix autoselection when opening a python file directly.
32+
([#16733](https://github.com/Microsoft/vscode-python/issues/16733))
3133

3234
### Thanks
3335

src/client/common/configSettings.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -679,18 +679,13 @@ export class PythonSettings implements IPythonSettings {
679679
const autoSelectedPythonInterpreter = this.interpreterAutoSelectionService.getAutoSelectedInterpreter(
680680
this.workspaceRoot,
681681
);
682-
if (inExperiment) {
683-
if (autoSelectedPythonInterpreter && this.workspaceRoot) {
684-
this.pythonPath = autoSelectedPythonInterpreter.path;
682+
if (autoSelectedPythonInterpreter) {
683+
this.pythonPath = autoSelectedPythonInterpreter.path;
684+
if (this.workspaceRoot) {
685685
this.interpreterAutoSelectionService
686686
.setWorkspaceInterpreter(this.workspaceRoot, autoSelectedPythonInterpreter)
687687
.ignoreErrors();
688688
}
689-
} else if (autoSelectedPythonInterpreter && this.workspaceRoot) {
690-
this.pythonPath = autoSelectedPythonInterpreter.path;
691-
this.interpreterAutoSelectionService
692-
.setWorkspaceInterpreter(this.workspaceRoot, autoSelectedPythonInterpreter)
693-
.ignoreErrors();
694689
}
695690
}
696691
if (inExperiment && this.pythonPath === DEFAULT_INTERPRETER_SETTING) {

src/test/common/configSettings/configSettings.pythonPath.unit.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { expect } from 'chai';
77
import * as path from 'path';
88
import * as sinon from 'sinon';
9-
import { anything, instance, mock, when } from 'ts-mockito';
9+
import { anything, instance, mock, verify, when } from 'ts-mockito';
1010
import * as typemoq from 'typemoq';
1111
import { Uri, WorkspaceConfiguration } from 'vscode';
1212
import { IWorkspaceService } from '../../../client/common/application/types';
@@ -118,7 +118,24 @@ suite('Python Settings - pythonPath', () => {
118118

119119
expect(configSettings.pythonPath).to.be.equal('python');
120120
});
121-
test("If we don't have a custom python path and we do have an auto selected interpreter, then use it", () => {
121+
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", () => {
122+
const pythonPath = path.join(__dirname, 'this is a python path that was auto selected');
123+
const interpreter = { path: pythonPath } as PythonEnvironment;
124+
const workspaceFolderUri = Uri.file(__dirname);
125+
const selectionService = mock(MockAutoSelectionService);
126+
when(selectionService.getAutoSelectedInterpreter(workspaceFolderUri)).thenReturn(interpreter);
127+
when(selectionService.setWorkspaceInterpreter(workspaceFolderUri, anything())).thenResolve();
128+
configSettings = new CustomPythonSettings(workspaceFolderUri, instance(selectionService));
129+
pythonSettings
130+
.setup((p) => p.get(typemoq.It.isValue('pythonPath')))
131+
.returns(() => 'python')
132+
.verifiable(typemoq.Times.atLeast(1));
133+
configSettings.update(pythonSettings.object);
134+
135+
expect(configSettings.pythonPath).to.be.equal(pythonPath);
136+
verify(selectionService.setWorkspaceInterpreter(workspaceFolderUri, interpreter)).once(); // Verify we set the autoselected interpreter
137+
});
138+
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", () => {
122139
const pythonPath = path.join(__dirname, 'this is a python path that was auto selected');
123140
const interpreter = { path: pythonPath } as PythonEnvironment;
124141
const workspaceFolderUri = Uri.file(__dirname);

0 commit comments

Comments
 (0)