microsoft / vscode-black-formatter

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

When using black from environment: AttributeError: 'Attribute' object has no attribute 'alias' #452

Closed moi90 closed 5 months ago

moi90 commented 5 months ago

ms-python.black-formatter stopped working when using black from the environment. When I do > Format Document, Code says "There is no formatter for 'python' files installed. However, it is installed and active.

When I try to > Black Formatter: Restart Server, I get the following in the Black Formatter Output console:

2024-02-06 10:15:53.181 [info] Interpreter from setting black-formatter.interpreter: /data1/mschroeder/22-LOKI-Pipeline/venv/bin/python
2024-02-06 10:15:53.188 [info] Interpreter from setting black-formatter.interpreter: /data1/mschroeder/22-LOKI-Pipeline/venv/bin/python
2024-02-06 10:15:53.188 [info] Interpreter from setting black-formatter.interpreter: /data1/mschroeder/22-LOKI-Pipeline/venv/bin/python
2024-02-06 10:15:53.188 [info] Interpreter from setting black-formatter.interpreter: /data1/mschroeder/22-LOKI-Pipeline/venv/bin/python
2024-02-06 10:15:53.209 [info] Server run command: /data1/mschroeder/22-LOKI-Pipeline/venv/bin/python /home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/tool/lsp_server.py
2024-02-06 10:15:53.212 [info] Server: Start requested.
2024-02-06 10:15:53.761 [info] Unable to deserialize message
Traceback (most recent call last):
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/pygls/protocol/json_rpc.py", line 330, in _deserialize_message
    return self._converter.structure(data, request_type)
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/cattrs/converters.py", line 332, in structure
    return self._structure_func.dispatch(cl)(obj, cl)
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/cattrs/dispatch.py", line 112, in _dispatch
    res = self._function_dispatch.dispatch(typ)
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/cattrs/dispatch.py", line 62, in dispatch
    return handler(typ)
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/lsprotocol/_hooks.py", line 1233, in _with_custom_structure
    return cattrs.gen.make_dict_structure_fn(cls, converter, **attributes)
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/cattrs/gen/__init__.py", line 355, in make_dict_structure_fn
    ian = a.alias
AttributeError: 'Attribute' object has no attribute 'alias'

2024-02-06 10:15:53.762 [info] Error receiving data
Traceback (most recent call last):
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/pygls/protocol/json_rpc.py", line 330, in _deserialize_message
    return self._converter.structure(data, request_type)
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/cattrs/converters.py", line 332, in structure
    return self._structure_func.dispatch(cl)(obj, cl)
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/cattrs/dispatch.py", line 112, in _dispatch
    res = self._function_dispatch.dispatch(typ)
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/cattrs/dispatch.py", line 62, in dispatch
    return handler(typ)
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/lsprotocol/_hooks.py", line 1233, in _with_custom_structure
    return cattrs.gen.make_dict_structure_fn(cls, converter, **attributes)
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/cattrs/gen/__init__.py", line 355, in make_dict_structure_fn
    ian = a.alias
AttributeError: 'Attribute' object has no attribute 'alias'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/pygls/protocol/json_rpc.py", line 443, in data_received
    self._data_received(data)
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/pygls/protocol/json_rpc.py", line 475, in _data_received
    json.loads(
  File "/data1/mschroeder/22-LOKI-Pipeline/venv/lib/python3.10/json/__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "/data1/mschroeder/22-LOKI-Pipeline/venv/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/data1/mschroeder/22-LOKI-Pipeline/venv/lib/python3.10/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "/home/mschroeder/.vscode-server/extensions/ms-python.black-formatter-2024.1.10361006/bundled/libs/pygls/protocol/json_rpc.py", line 348, in _deserialize_message
    raise JsonRpcInternalError() from exc
pygls.exceptions.JsonRpcInternalError: Internal Error

Black installed in the environment:

black --version
black, 24.1.1 (compiled: no)
Python (CPython) 3.10.13

VS Code Version:

Version: 1.86.0
Commit: 05047486b6df5eb8d44b2ecd70ea3bdf775fd937
Date: 2024-01-31T10:27:46.147Z
Electron: 27.2.3
ElectronBuildId: 26495564
Chromium: 118.0.5993.159
Node.js: 18.17.1
V8: 11.8.172.18-electron.0
OS: Linux x64 5.15.0-92-generic

Extension Version: Both v2024.1.10361006 (pre-release) and v2024.0.0.

(However, it works with the bundled version of black.)

side2k commented 5 months ago

It feels like bundled set of libraries is mismatched - changes in the cattrs introduced by this PR are not supplemented with changes in attrs introduced here. Is there a way to update any python libs bundled with a vscode extension?

update: further investigation shown that attrs module is imported from my virtualenv. Maybe I can fix this by upgrading attrs in the env.

side2k commented 5 months ago

Yep, updating attrs to >=22.2.0 worked as a workaround for me. @moi90 it might help you too.

However, the issue in general still persists - bundled version of cattrs is using attrs from virtualenv, and not from the bundle.

Gosh, I didn't even think before that, how this extension failure would disrupt my everyday work.

karthiknadig commented 5 months ago

If your environment has one of these packages there make sure it is same or greater than the versions listed here: https://github.com/microsoft/vscode-black-formatter/blob/main/requirements.txt

We recommend using the bundled libs. When using black from your environment it can run into problems like this.

side2k commented 5 months ago

@karthiknadig isn't the point of bundling libs with the extension in making it independent from the environment?

karthiknadig commented 5 months ago

There are two things here, the server itself needs to communicate with VS Code and that requires attrs, cattrs, pygls etc. Then there is black and its dependencies. You can set up the extension in a way to use the bundled libs for communication but use the black from your environment for formatting. Like this:

"black-formatter.importStrategy": "useBundled",
"black-formatter.path": ["${interpreter}", "-m", "black"],

The "${interpreter}" part there is automatically resolved to the selected interpreter.

You can even point it to black binary:

"black-formatter.importStrategy": "useBundled",
"black-formatter.path": ["black"],