Fix onDidChangeExecutionDetails API to fire taking auto selected interpreter in account
See original GitHub issueEnvironment data
- VS Code version: 1.53.2
- Extension version: v2021.2.582707922
- OS and version: macOS 10.15.7
- Python version: pyenv, Python 3.9
- Type of virtual environment used: venv
- Value of the
python.languageServer
setting: pylance
Expected behaviour
getExecutionDetails
should return the active interpreter, or the extension API should have a way to signal when the active interpreter information is available after initialization is complete.
Actual behaviour
When the Python extension has just been activated, getExecutionDetails
returns ‘python’, even though an interpreter is selected for the current workspace. Subsequently, onDidChangeExecutionDetails
is NOT called. However, a few seconds later (when the Python extension has presumably finished initializing), getExecutionDetails
finally returns the correct executable.
Steps to reproduce:
-
Make sure the
python.pythonPath
setting is not being used (the DeprecatedPythonPath experiment should be active). -
Create a test workspace with some Python file and set the active interpreter to a non-default value (e.g. a venv for that workspace).
-
Create an extension using
yo code
. ChangeactivationEvents
to"onLanguage:python"
. Replace the code in extension.ts with the following:
import * as vscode from 'vscode';
export async function activate(context: vscode.ExtensionContext): Promise<void> {
const outputChannel = vscode.window.createOutputChannel('test');
context.subscriptions.push(outputChannel);
const pythonExtension = vscode.extensions.getExtension('ms-python.python');
if (pythonExtension === undefined) {
outputChannel.appendLine('Python extension not found');
return;
}
outputChannel.appendLine('Activate Python extension');
await pythonExtension.activate();
outputChannel.appendLine('Python extension activated');
const handler = pythonExtension.exports.settings.onDidChangeExecutionDetails(
(resource: vscode.Uri | undefined) => {
outputChannel.appendLine(`Active interpreter changed for resource: ${resource}`);
}
);
context.subscriptions.push(handler);
outputChannel.appendLine('Listener registered');
const folder = vscode.workspace.workspaceFolders?.[0].uri;
const logInterpreter = async () => {
const details = await pythonExtension.exports.settings.getExecutionDetails(folder);
outputChannel.appendLine(`Execution details: ${JSON.stringify(details)}`);
};
logInterpreter();
const interval = setInterval(logInterpreter, 100);
setTimeout(() => clearInterval(interval), 5000);
}
- Run the extension in the extension host and open the test Python workspace. In the ‘test’ Output channel, notice the output:
Activate Python extension
Python extension activated
Listener registered
Execution details: {"execCommand":["python"]}
[.....]
Execution details: {"execCommand":["/Users/matan/test/.venv/bin/python"]}
[.....]
As you can see, the interpreter path ‘changes’ (that is, it is being initialized) and the client extension has no way of knowing this (apart from polling maybe).
My suggestion is making getExecutionDetails
block until the interpreter is initialized from the interpreter storage, or exposing an executionDetailsInitialized
event.
Logs
User belongs to experiment group 'pythonaacf'
User belongs to experiment group 'pythonSendEntireLineToREPL'
User belongs to experiment group 'pythonNotDisplayLinterPrompt'
User belongs to experiment group 'DeprecatePythonPath - experiment'
User belongs to experiment group 'ShowPlayIcon - start'
User belongs to experiment group 'ShowExtensionSurveyPrompt - control'
User belongs to experiment group 'DebugAdapterFactory - experiment'
User belongs to experiment group 'PtvsdWheels37 - experiment'
User belongs to experiment group 'UseTerminalToGetActivatedEnvVars - control'
User belongs to experiment group 'LocalZMQKernel - experiment'
User belongs to experiment group 'CollectLSRequestTiming - control'
User belongs to experiment group 'CollectNodeLSRequestTiming - experiment'
User belongs to experiment group 'EnableIPyWidgets - experiment'
User belongs to experiment group 'RunByLine - experiment'
User belongs to experiment group 'CustomEditorSupport - control'
> conda --version
> conda info --json
> pyenv root
> python3.7 ~/.vscode/extensions/ms-python.python-2021.2.582707922/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python3.6 ~/.vscode/extensions/ms-python.python-2021.2.582707922/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python2 ~/.vscode/extensions/ms-python.python-2021.2.582707922/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python3 ~/.vscode/extensions/ms-python.python-2021.2.582707922/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python ~/.vscode/extensions/ms-python.python-2021.2.582707922/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python ~/.vscode/extensions/ms-python.python-2021.2.582707922/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python ~/.vscode/extensions/ms-python.python-2021.2.582707922/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python ~/.vscode/extensions/ms-python.python-2021.2.582707922/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python ~/.vscode/extensions/ms-python.python-2021.2.582707922/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python ~/.vscode/extensions/ms-python.python-2021.2.582707922/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> pyenv root
> pyenv root
> pyenv root
> pyenv root
> pyenv root
> pyenv root
Starting Pylance language server.
Python interpreter path: ./scripts/.venv/bin/python
> conda --version
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:14 (1 by maintainers)
Top GitHub Comments
It got closed as it wasn’t a trivial fix as mentioned in https://github.com/microsoft/vscode-python/issues/15467#issuecomment-784641625. It’s a matter for us prioritizing to fix it, or a external contributor is also welcome to take it from https://github.com/microsoft/vscode-python/pull/15504.
Indeed it doesn’t happen when DeprecatePythonPath is active. I think I see the bug in the code you linked to - when no workspace is affected, then
workspaceUriIndex
is -1 and_onConfigChanged
gets called anyway.