microsoft / microsoft/vscode-black-formatter
bundled/tool/*.py incompatibilities with packages in venv with importStrategy=fromEnvironment
- Dominant language
- Python
- Stars
- 196
- Forks
- 49
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 7
Description
The `fromEnvironment` option for the extension setting `black-formatter.importStrategy` indicates that it controls where the black tool is used from, but the setting also affects the venv in which the bundled LSP server runs. If the venv contains old versions of dependencies of the LSP server (eg. `typing_extensions==4.5.0`) then the extension fails to load with errors as below:
```
2023-11-07 14:52:24.748 [info] Traceback (most recent call last):
File "/.vscode/extensions/ms-python.black-formatter-2023.6.0/bundled/tool/lsp_server.py", line 68, in
from pygls import server, uris, workspace
File "/.vscode/extensions/ms-python.black-formatter-2023.6.0/bundled/libs/pygls/server.py", line 37, in
from pygls.lsp import ConfigCallbackType, ShowDocumentCallbackType
File "/.vscode/extensions/ms-python.black-formatter-2023.6.0/bundled/libs/pygls/lsp/__init__.py", line 38, in
from typeguard import check_type
File "/.vscode/extensions/ms-python.black-formatter-2023.6.0/bundled/libs/typeguard/__init__.py", line 21, in
from ._importhook import ImportHookManager as ImportHookManager
File "/.vscode/extensions/ms-python.black-formatter-2023.6.0/bundled/libs/typeguard/_importhook.py", line 22, in
from typing_extensions import Buffer
ImportError: cannot import name 'Buffer' from 'typing_extensions' (/venv/lib/python3.10/site-packages/typing_extensions.py)
```
This issue can currently be worked around by instead configuring `black-formatter.path` to invoke black from the desired venv.
A cursory suggestion would be for the LSP server process to be sandboxed to only use only the bundled packages, regardless of whether `"fromEnvironment"` is configured.
Note that this issue is also present in the other Microsoft LSP Python extensions.
Contributor guide
Assessment
This issue has not been assessed yet.