# languages: Fix Pyright ignoring toolchain when LSP settings are configured \(\#59990\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/c28e37687d566d7fe20d959622f0ff172e546acf)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: c28e37687d566d7fe20d959622f0ff172e546acf

Requested commit: c28e37687d566d7fe20d959622f0ff172e546acf

Commit: c28e37687d566d7fe20d959622f0ff172e546acf

Tree: aab20f0966ee54c95e18ff7c963a37037ae349fa

Author: Xin Zhao

Committer: GitHub

## Message

````
languages: Fix Pyright ignoring toolchain when LSP settings are configured (#59990)

# Objective

Closes #55936
Helps #57288

There is a strange behavior for Python users when using basedpyright or
pyright as the language server. As reported in #55936, when no settings
are configured in `lsp.basedpyright.settings`, the language server
correctly receives toolchain information discovered by Zed:
```json
{
  "venvPath": ".",
  "venv": ".venv",
  "python": {
    "pythonPath": "/home/xin/works/test/test_python/.venv/bin/python",
    "defaultInterpreterPath": "/home/xin/works/test/test_python/.venv/bin/python"
  },
  "basedpyright.analysis": {
    "typeCheckingMode": "standard"
  },
  "basedpyright.disableOrganizeImports": true
}
```
However, when the user configures any other settings in
`lsp.basedpyright.settings`, the toolchain information is silently lost.
For example, with the following `settings.json`:
```jsonc
{
  "lsp": {
    "basedpyright": {
      "settings": {
        "analysis": {
          "diagnosticMode": "workspace",
          "typeCheckingMode": "standard",
          "inlayHints.callArgumentNames": true,
          "inlayHints.variableTypes": true,
        },
      },
    }
  }
}
```
The final configuration sent to the language server becomes:
```json
{
  "analysis": {
    "diagnosticMode": "workspace",
    "typeCheckingMode": "standard",
    "inlayHints.callArgumentNames": true,
    "inlayHints.variableTypes": true
  }
}
```
No toolchain information is passed, so the language server ignores the
user's selected Python environment and falls back to whatever virtual
environment it discovers on its own. To make toolchain selection work,
users currently have to manually configure virtual environment settings
themselves (as noted in
https://github.com/zed-industries/zed/issues/57288#issuecomment-4750935341).


This issue was introduced in #48262, which aimed to make the virtual
environment used by basedpyright/pyright configurable. The core logic is
the `should_insert_toolchain` closure, originally designed to check
whether Zed should inject the detected toolchain into the language
server configuration. The intent was: if the user has already configured
a virtual environment in the language server settings, skip the
injection.


https://github.com/zed-industries/zed/blob/2c346f60a76fe3f0367ef924927f50a6efdf5718/crates/languages/src/python.rs#L2115-L2128

However, the original logic was inverted — it injected the toolchain
when the user had no settings at all or when they had configured virtual
environment keys. This meant that when the user had LSP settings but no
virtual environment configuration, toolchain injection was skipped, as
reported in #55936.


## Solution

Change the logic defined in `should_insert_toolchain` closure, instead
of insert toolchain when detected virtual environment settings, now we
insert toolchain when no virtual environment settings is detected.

## Testing

Tested locally. The logic change is simple, should be easy for review.

## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [ ] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

---

Release Notes:

- Fixed toolchain selection being ignored when Pyright or Basedpyright
settings are configured without specifying a Python interpreter path
````

## Parents

- [f181a2f47be57d1dc57412d42ac44df2fc7eca8f](https://git.cafe/gitcafe/zed/commit/f181a2f47be57d1dc57412d42ac44df2fc7eca8f?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/c28e37687d566d7fe20d959622f0ff172e546acf?format=markdown)
