microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
150 stars 35 forks source link

Request textDocument/formatting failed: AttributeError: 'Workspace' object has no attribute 'get_text_document' #371

Closed XDeviation closed 11 months ago

XDeviation commented 11 months ago

As title, when I call format, it will raise this Error Full log:

2023-11-07 17:42:45.636 [info] [Trace - 5:42:45 PM] Sending request 'textDocument/formatting - (1)'.
2023-11-07 17:42:45.643 [info] [Trace - 5:42:45 PM] Received response 'textDocument/formatting - (1)' in 7ms. Request failed: AttributeError: 'Workspace' object has no attribute 'get_text_document' (-32602).
2023-11-07 17:42:45.643 [info] [Error - 5:42:45 PM] Request textDocument/formatting failed.
2023-11-07 17:42:45.643 [info]   Message: AttributeError: 'Workspace' object has no attribute 'get_text_document'
  Code: -32602 
{'traceback': ['  File "/mnt/data/gibraltar/.venv/lib/python3.8/site-packages/pygls/protocol.py", line 340, in _handle_request\n    self._execute_request(msg_id, handler, params)\n', '  File "/mnt/data/gibraltar/.venv/lib/python3.8/site-packages/pygls/protocol.py", line 264, in _execute_request\n    self._send_response(msg_id, handler(params))\n', '  File "/mnt/data/vscode/extensions/ms-python.black-formatter-2023.6.0/bundled/tool/lsp_server.py", line 101, in formatting\n    document = LSP_SERVER.workspace.get_text_document(params.text_document.uri)\n']}

vscode-black-formatter version: v2023.6.0 black version: 22.12.0 python version: 3.8.10 thanks.

MartinHenselHLL commented 11 months ago

Seconded, might be connected to latest VSCode update or the latest python extension update (both released 2023-11-06, it still worked last week).

VSCode: Version: 1.84.1 (user setup) Commit: 2b35e1e6d88f1ce073683991d1eff5284a32690f Date: 2023-11-06T12:35:59.022Z Electron: 25.9.2 ElectronBuildId: 24603566 Chromium: 114.0.5735.289 Node.js: 18.15.0 V8: 11.4.183.29-electron.0 OS: Windows_NT x64 10.0.19044

Python extension version: v2023.20.0 ms-python.black-formatter extension version: v2023.6.0 python version: 3.11.6 black version: 23.10.1 (from environment)

Full log:

2023-11-07 17:02:10.631 [info] No interpreter found from setting black-formatter.interpreter
2023-11-07 17:02:10.632 [info] Getting interpreter from ms-python.python extension for workspace d:\projects
2023-11-07 17:02:10.633 [info] Interpreter from ms-python.python extension for d:\projects: d:\projects\venv\python.exe
2023-11-07 17:02:10.643 [info] Server run command: d:\projects\venv\python.exe c:\Users\********\.vscode\extensions\ms-python.black-formatter-2023.6.0\bundled\tool\lsp_server.py
2023-11-07 17:02:10.645 [info] Server: Start requested.
2023-11-07 17:02:11.669 [info] CWD Server: d:\projects
2023-11-07 17:02:11.669 [info] Settings used to run Server:
[
    {
        "cwd": "d:\\projects",
        "workspace": "file:///d%3A/projects",
        "args": [],
        "path": [],
        "interpreter": [
            "d:\\projects\\venv\\python.exe"
        ],
        "importStrategy": "fromEnvironment",
        "showNotifications": "off"
    }
]

2023-11-07 17:02:11.669 [info] Global settings:
{
    "cwd": "C:\\Users\\********\\AppData\\Local\\Programs\\Microsoft VS Code",
    "workspace": "C:\\Users\\********\\AppData\\Local\\Programs\\Microsoft VS Code",
    "args": [],
    "path": [],
    "interpreter": [],
    "importStrategy": "fromEnvironment",
    "showNotifications": "off"
}

2023-11-07 17:02:11.669 [info] sys.path used to run Server:
   c:\Users\********\.vscode\extensions\ms-python.black-formatter-2023.6.0\bundled\tool
   d:\projects\venv\python311.zip
   d:\projects\venv\DLLs
   d:\projects\venv\Lib
   d:\projects\venv
   d:\projects\venv\Lib\site-packages
   d:\projects\venv\Lib\site-packages\win32
   d:\projects\venv\Lib\site-packages\win32\lib
   d:\projects\venv\Lib\site-packages\Pythonwin
   c:\Users\********\.vscode\extensions\ms-python.black-formatter-2023.6.0\bundled\libs
2023-11-07 17:02:11.675 [info] d:\projects\venv\python.exe -m black --version
2023-11-07 17:02:11.676 [info] CWD formatter: d:\projects
2023-11-07 17:02:11.812 [info] Version info for formatter running for D:\projects:
black, 23.10.1 (compiled: no)
Python (CPython) 3.11.6

2023-11-07 17:02:11.812 [info] SUPPORTED black>=22.3.0
FOUND black==23.10.1

2023-11-07 17:02:15.905 [info] [Trace - 5:02:15 PM] Sending request 'textDocument/formatting - (1)'.
2023-11-07 17:02:15.917 [info] [Trace - 5:02:15 PM] Received response 'textDocument/formatting - (1)' in 12ms. Request failed: AttributeError: 'Workspace' object has no attribute 'get_text_document' (-32602).
2023-11-07 17:02:15.917 [info] [Error - 5:02:15 PM] Request textDocument/formatting failed.
2023-11-07 17:02:15.917 [info]   Message: AttributeError: 'Workspace' object has no attribute 'get_text_document'
  Code: -32602 
{'traceback': ['  File "d:\\projects\\venv\\Lib\\site-packages\\pygls\\protocol.py", line 340, in _handle_request\n    self._execute_request(msg_id, handler, params)\n', '  File "d:\\projects\\venv\\Lib\\site-packages\\pygls\\protocol.py", line 264, in _execute_request\n    self._send_response(msg_id, handler(params))\n                                ^^^^^^^^^^^^^^^\n', '  File "c:\\Users\\********\\.vscode\\extensions\\ms-python.black-formatter-2023.6.0\\bundled\\tool\\lsp_server.py", line 101, in formatting\n    document = LSP_SERVER.workspace.get_text_document(params.text_document.uri)\n               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n']}

Extracted traceback:

  File "d:\\projects\\venv\\Lib\\site-packages\\pygls\\protocol.py", line 340, in _handle_request
    self._execute_request(msg_id, handler, params)
  File "d:\\projects\\venv\\Lib\\site-packages\\pygls\\protocol.py", line 264, in _execute_request
    self._send_response(msg_id, handler(params))
                                ^^^^^^^^^^^^^^^
  File "c:\\Users\\********\\.vscode\\extensions\\ms-python.black-formatter-2023.6.0\\bundled\\tool\\lsp_server.py", line 101, in formatting
    document = LSP_SERVER.workspace.get_text_document(params.text_document.uri)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Thanks.

karthiknadig commented 11 months ago

Can you verify if your environment has pygls installed? If yes would it be possible to upgrade it to the latest?

XDeviation commented 11 months ago

Sure.

❯ pip show pygls
Name: pygls
Version: 1.0.2
Summary: a pythonic generic language server (pronounced like "pie glass").
Home-page: https://github.com/openlawlibrary/pygls/tree/master/
Author: Open Law Library
Author-email: info@openlawlib.org
License: Apache 2.0
Location: /mnt/data/gibraltar/.venv/lib/python3.8/site-packages
Requires: lsprotocol, typeguard
Required-by: 

When I upgrade pygls to 1.1.2, it works successfully, thanks

MartinHenselHLL commented 11 months ago

Also had the same version of pygls (1.0.2). An upgrade to 1.1.2 solved the problem. Many thanks @karthiknadig !