question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fix onDidChangeExecutionDetails API to fire taking auto selected interpreter in account

See original GitHub issue

Environment 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:

  1. Make sure the python.pythonPath setting is not being used (the DeprecatedPythonPath experiment should be active).

  2. 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).

  3. Create an extension using yo code. Change activationEvents 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);
}

  1. 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
karrtikrcommented, Jun 16, 2021

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.

1reaction
matangovercommented, Feb 23, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expose interpreter path using API · Issue #11294 - GitHub
This code snippet shows how to use the new API, could be helpful. Update the API as follows by adding a settings section,....
Read more >
How to preselect (set default) python interpreter in python ...
Once you selected an interpreter, it should be stored in the settings.json file in .vscode folder. It should be something like this:
Read more >
Interpreter in Apache Zeppelin
A Zeppelin interpreter is a plug-in which enables Zeppelin users to use a specific language/data-processing-backend. For example, to use Scala code in ...
Read more >
Configure a Python interpreter | PyCharm Documentation
Select the target interpreter. View interpreters. When PyCharm stops supporting any of the outdated Python versions, the corresponding Python ...
Read more >
Initialization, Finalization, and Threads — Python 3.11.1 ...
Initialize the Python interpreter. In an application embedding Python, this should be called before using any other Python/C API functions; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found