Skip to content

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

@StellaHuang95

Description

@StellaHuang95

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?

Metadata

Metadata

Assignees

Labels

bugIssue identified by VS Code Team member as probable buginfo-neededIssue requires more information from postertriage-neededNeeds assignment to the proper sub-team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions