Skip to content

getActiveEnvironmentPath returns incorrect active environment after switching when the Python Environments extension is enabled. #24953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
StellaHuang95 opened this issue Apr 1, 2025 · 7 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster triage-needed Needs assignment to the proper sub-team

Comments

@StellaHuang95
Copy link

Note: This issue only repros when the Python Environments extension is enabled in VS Code.

Repro Steps:

  1. open a workspace in VS Code and create a new Python file, make sure the Python Environments extension is enabled.
  2. set a global Python interpreter as the active environment.
  3. click the Select Python Interpreter button on the bottom right corner and switch to a different conda env or venv.
  4. notice the active environment does update in the UI. However, Pylance still considers the previous environment as the active one.

Investigation I did:

  1. On the Pylance side, we listen for the pythonEnvironmentApi.onDidChangeEnvironment event and send a custom LSP notification when the event fires.
  2. When the Pylance server receives this LSP notification, it sends a workspace/configuration request.
  3. The response is intercepted in the middleware, where we call pythonApi!.environments!.getActiveEnvironmentPath(uri) and pythonApi!.environments!.resolveEnvironment(activePath) to get the active Python path.
  4. The middleware then modifies the response like pythonPath and returns it to the server.

I noticed that the env path returned in Step 3 from the python api does not always return the newly selected environment, especially when switching from a global interpreter to a conda one or a workspace venv.

@karthiknadig, do you have any insights on what might be causing this behavior in the Python API?

@StellaHuang95 StellaHuang95 added the bug Issue identified by VS Code Team member as probable bug label Apr 1, 2025
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Apr 1, 2025
@karthiknadig
Copy link
Member

@StellaHuang95 I suspect there is some caching somewhere that might be causing this. Is this with Stable or pre-release python extension.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Apr 1, 2025
@StellaHuang95
Copy link
Author

This is with latest stable python extension.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Apr 1, 2025
@karthiknadig
Copy link
Member

Is it possible to switch Pylance extension to use the Python Env extension API for getting Python also? Essentially, if this extension is available then you should use it for all information related to python. Using, both APIs can be problematic.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Apr 1, 2025
@StellaHuang95
Copy link
Author

The feature I’m implementing is an experimental feature specifically for Python Environment extension users. Currently, Pylance lists the Python Environment extension as an optional dependency, and I don’t have any plans to change the existing behavior of how Pylance gets the active environment, but this is something we can discuss more in tomorrow’s meeting.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Apr 1, 2025
@karthiknadig
Copy link
Member

Python extension doesn’t have envs extension as dependency either. It only uses it when it is installed. The API from environment extension is direct. What you could do is check if it is installed, and use that as a signal to consume API from environment extension.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Apr 1, 2025
@StellaHuang95
Copy link
Author

Sure I can give that a try to see if the problems go away. When you said python extension used envs extension APIs when it's installed, what APIs are you referring to? The getActiveEnvironmentPath() and resolveEnvironment() are currently called in Pylance, but I don't see it calls the environment extension api anywhere.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Apr 2, 2025
@karthiknadig
Copy link
Member

All python related APIs in the python extension uses Python Environments extension when it is installed. I have a check in the implementations where I see if Python Envs extension is installed, if yes then use the Python envs API, else use the legacy API.

You should be able to do something similar in the Pylance extension. Here is the code that I use:

let _useExt: boolean | undefined;
export function useEnvExtension(): boolean {
if (_useExt !== undefined) {
return _useExt;
}
_useExt = !!getExtension(ENVS_EXTENSION_ID);
return _useExt;
}

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

2 participants