Repository: astral-sh/ruff-vscode

Issue #154: LSP server error: "Unable to deserialize message" triggered by pygls
Created: 2023-03-13T18:44:27Z
Thanks for building this extension. I'm running into constant crashes in which saving a file causes the below popup to appear:

...and the extension stops working.
It looks like the extension crashes when it gets a pygls message intended for other extensions. (I also have matangover.mypy and ms-python.vscode-pylance installed.)
The below pygls message triggers the crash. Both pylance and mypy are flagging function with two return statements, one of which is unreachable.
[Trace - 6:32:54 PM] Sending request 'textDocument/codeAction - (17)'.
Params:

{
    "textDocument": {
        "uri": "file:///workspaces/alarmdotcom/custom_components/alarmdotcom/alarmhub.py"
    },
    "range": {
        "start": {
            "line": 0,
            "character": 0
        },
        "end": {
            "line": 251,
            "character": 0
        }
    },
    "context": {
        "diagnostics": [
            {
                "range": {
                    "start": {
                        "line": 137,
                        "character": 8
                    },
                    "end": {
                        "line": 137,
                        "character": 8
                    }
                },
                "message": "Statement is unreachable  [unreachable]",
                "code": "error",
                "severity": 1,
                "source": "mypy"
            },
            {
                "range": {
                    "start": {
                        "line": 137,
                        "character": 8
                    },
                    "end": {
                        "line": 147,
                        "character": 9
                    }
                },
                "message": "Code is unreachable",
                "severity": 4,
                "tags": [
                    1
                ],
                "source": "Pylance"
            },
            {
                "range": {
                    "start": {
                        "line": null,
                        "character": 0
                    },
                    "end": {
                        "line": null,
                        "character": 0
                    }
                },
                "message": "In member \"devices\" of class \"AlarmHub\":",
                "severity": 3,
                "source": "mypy"
            },
            {
                "range": {
                    "start": {
                        "line": 137,
                        "character": 8
                    },
                    "end": {
                        "line": 137,
                        "character": 8
                    }
                },
                "message": "Statement is unreachable  [unreachable]",
                "severity": 1,
                "source": "mypy"
            }
        ],
        "only": [
            "source.fixAll.ruff"
        ]
    }
}
[Error - 6:34:44 PM] Request textDocument/codeAction failed.
Error: Connection got disposed.
    at Object.dispose (/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/dist/webpack:/ruff/node_modules/vscode-jsonrpc/lib/common/connection.js:1044:27)
    at Object.dispose (/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/dist/webpack:/ruff/node_modules/vscode-languageclient/lib/common/client.js:73:35)
    at /home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/dist/webpack:/ruff/node_modules/vscode-languageclient/lib/common/client.js:2320:28
    at client (/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/dist/webpack:/ruff/src/common/server.ts:113:19)
    at serverId (/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/dist/webpack:/ruff/src/extension.ts:36:9)
    at /home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/dist/webpack:/ruff/src/extension.ts:116:48
Server Command [RUN]: /usr/local/bin/python /home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/tool/server.py
Server Command [DEBUG]: /usr/local/bin/python /home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/tool/_debug_server.py
Using bundled executable: /home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/bin/ruff
Running Ruff with: ['/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/bin/ruff', '--no-cache', '--no-fix', '--quiet', '--format', 'json', '-', '--stdin-filename', '/workspaces/alarmdotcom/custom_components/alarmdotcom/alarmhub.py']

I pulled the above out of a verbose trace. Below is output with ruff.trace.server="messages".
Name: Ruff
Module: ruff
Python extension loading
Waiting for interpreter from python extension.
Python extension loaded
Server Command [RUN]: /usr/local/bin/python /home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/tool/server.py
Server Command [DEBUG]: /usr/local/bin/python /home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/tool/_debug_server.py
[Trace - 6:27:14 PM] Sending request 'initialize - (0)'.
[Trace - 6:27:17 PM] Received response 'initialize - (0)' in 2749ms.
[Trace - 6:27:17 PM] Sending notification 'initialized'.
[Trace - 6:27:17 PM] Sending notification 'textDocument/didOpen'.
[Trace - 6:27:17 PM] Received notification 'window/logMessage'.
Using bundled executable: /home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/bin/ruff
[Trace - 6:27:17 PM] Received notification 'window/logMessage'.
Running Ruff with: ['/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/bin/ruff', '--no-cache', '--no-fix', '--quiet', '--format', 'json', '-', '--stdin-filename', '/workspaces/alarmdotcom/custom_components/alarmdotcom/alarmhub.py']
[Trace - 6:27:17 PM] Received notification 'textDocument/publishDiagnostics'.
[Trace - 6:27:18 PM] Sending request 'textDocument/codeAction - (1)'.
[Trace - 6:27:18 PM] Received response 'textDocument/codeAction - (1)' in 14ms.
[Trace - 6:27:21 PM] Sending request 'textDocument/codeAction - (2)'.
[Trace - 6:27:21 PM] Received response 'textDocument/codeAction - (2)' in 20ms.
[Trace - 6:27:24 PM] Sending request 'textDocument/hover - (3)'.
[Trace - 6:27:24 PM] Received response 'textDocument/hover - (3)' in 26ms.
[Trace - 6:27:24 PM] Sending request 'textDocument/codeAction - (4)'.
[Trace - 6:27:24 PM] Received response 'textDocument/codeAction - (4)' in 39ms.
[Trace - 6:27:24 PM] Sending request 'textDocument/codeAction - (5)'.
[Trace - 6:27:24 PM] Received response 'textDocument/codeAction - (5)' in 20ms.
[Trace - 6:27:26 PM] Sending request 'textDocument/hover - (6)'.
[Trace - 6:27:26 PM] Received response 'textDocument/hover - (6)' in 12ms.
[Trace - 6:27:27 PM] Sending request 'textDocument/codeAction - (7)'.
[Trace - 6:27:27 PM] Received response 'textDocument/codeAction - (7)' in 4ms.
[Trace - 6:27:27 PM] Sending request 'textDocument/codeAction - (8)'.
[Trace - 6:27:27 PM] Received response 'textDocument/codeAction - (8)' in 6ms.
[Trace - 6:27:29 PM] Sending request 'textDocument/codeAction - (9)'.
[Trace - 6:27:29 PM] Received response 'textDocument/codeAction - (9)' in 32ms.
[Trace - 6:27:31 PM] Sending request 'textDocument/codeAction - (10)'.
[Trace - 6:27:31 PM] Received notification 'window/logMessage'.
Using bundled executable: /home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/bin/ruff
[Trace - 6:27:31 PM] Received notification 'window/logMessage'.
Running Ruff with: ['/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/bin/ruff', '--no-cache', '--no-fix', '--quiet', '--format', 'json', '-', '--fix', '--stdin-filename', '/workspaces/alarmdotcom/custom_components/alarmdotcom/alarmhub.py']
[Trace - 6:27:31 PM] Received response 'textDocument/codeAction - (10)' in 56ms.
[Trace - 6:27:33 PM] Sending request 'textDocument/codeAction - (11)'.
[Trace - 6:27:34 PM] Received response 'textDocument/codeAction - (11)' in 15ms.
[Trace - 6:27:36 PM] Sending request 'textDocument/codeAction - (12)'.
Unable to deserialize message
  + Exception Group Traceback (most recent call last):
  |   File "/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/pygls/protocol.py", line 404, in _deserialize_message
  |     return self._converter.structure(data, request_type)
  |   File "/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/cattrs/converters.py", line 309, in structure
  |     return self._structure_func.dispatch(cl)(obj, cl)
  |   File "<cattrs generated structure lsprotocol.types.TextDocumentCodeActionRequest>", line 26, in structure_TextDocumentCodeActionRequest
  |     if errors: raise __c_cve('While structuring ' + 'TextDocumentCodeActionRequest', errors, __cl)
  | cattrs.errors.ClassValidationError: While structuring TextDocumentCodeActionRequest (1 sub-exception)
  +-+---------------- 1 ----------------
    | Exception Group Traceback (most recent call last):
    |   File "<cattrs generated structure lsprotocol.types.TextDocumentCodeActionRequest>", line 10, in structure_TextDocumentCodeActionRequest
    |     res['params'] = __c_structure_params(o['params'], __c_type_params)
    |   File "<cattrs generated structure lsprotocol.types.CodeActionParams>", line 31, in structure_CodeActionParams
    |     if errors: raise __c_cve('While structuring ' + 'CodeActionParams', errors, __cl)
    | cattrs.errors.ClassValidationError: While structuring CodeActionParams (1 sub-exception)
    | Structuring class TextDocumentCodeActionRequest @ attribute params
    +-+---------------- 1 ----------------
      | Exception Group Traceback (most recent call last):
      |   File "<cattrs generated structure lsprotocol.types.CodeActionParams>", line 15, in structure_CodeActionParams
      |     res['context'] = __c_structure_context(o['context'], __c_type_context)
      |   File "<cattrs generated structure lsprotocol.types.CodeActionContext>", line 21, in structure_CodeActionContext
      |     if errors: raise __c_cve('While structuring ' + 'CodeActionContext', errors, __cl)
      | cattrs.errors.ClassValidationError: While structuring CodeActionContext (1 sub-exception)
      | Structuring class CodeActionParams @ attribute context
      +-+---------------- 1 ----------------
        | Exception Group Traceback (most recent call last):
        |   File "<cattrs generated structure lsprotocol.types.CodeActionContext>", line 5, in structure_CodeActionContext
        |     res['diagnostics'] = __c_structure_diagnostics(o['diagnostics'], __c_type_diagnostics)
        |   File "/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/cattrs/converters.py", line 510, in _structure_list
        |     raise IterableValidationError(
        | cattrs.errors.IterableValidationError: While structuring typing.List[lsprotocol.types.Diagnostic] (1 sub-exception)
        | Structuring class CodeActionContext @ attribute diagnostics
        +-+---------------- 1 ----------------
          | Exception Group Traceback (most recent call last):
          |   File "/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/cattrs/converters.py", line 502, in _structure_list
          |     res.append(handler(e, elem_type))
          |   File "<cattrs generated structure lsprotocol.types.Diagnostic>", line 56, in structure_Diagnostic
          |     if errors: raise __c_cve('While structuring ' + 'Diagnostic', errors, __cl)
          | cattrs.errors.ClassValidationError: While structuring Diagnostic (1 sub-exception)
          | Structuring typing.List[lsprotocol.types.Diagnostic] @ index 2
          +-+---------------- 1 ----------------
            | Exception Group Traceback (most recent call last):
            |   File "<cattrs generated structure lsprotocol.types.Diagnostic>", line 5, in structure_Diagnostic
            |     res['range'] = __c_structure_range(o['range'], __c_type_range)
            |   File "<cattrs generated structure lsprotocol.types.Range>", line 14, in structure_Range
            |     if errors: raise __c_cve('While structuring ' + 'Range', errors, __cl)
            | cattrs.errors.ClassValidationError: While structuring Range (2 sub-exceptions)
            | Structuring class Diagnostic @ attribute range
            +-+---------------- 1 ----------------
              | Exception Group Traceback (most recent call last):
              |   File "<cattrs generated structure lsprotocol.types.Range>", line 5, in structure_Range
              |     res['start'] = __c_structure_start(o['start'], __c_type_start)
              |   File "<cattrs generated structure lsprotocol.types.Position>", line 14, in structure_Position
              |     if errors: raise __c_cve('While structuring ' + 'Position', errors, __cl)
              | cattrs.errors.ClassValidationError: While structuring Position (1 sub-exception)
              | Structuring class Range @ attribute start
              +-+---------------- 1 ----------------
                | Traceback (most recent call last):
                |   File "<cattrs generated structure lsprotocol.types.Position>", line 5, in structure_Position
                |     res['line'] = __c_structure_line(o['line'])
                | TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
                | Structuring class Position @ attribute line
                +------------------------------------
              +---------------- 2 ----------------
              | Exception Group Traceback (most recent call last):
              |   File "<cattrs generated structure lsprotocol.types.Range>", line 10, in structure_Range
              |     res['end'] = __c_structure_end(o['end'], __c_type_end)
              |   File "<cattrs generated structure lsprotocol.types.Position>", line 14, in structure_Position
              |     if errors: raise __c_cve('While structuring ' + 'Position', errors, __cl)
              | cattrs.errors.ClassValidationError: While structuring Position (1 sub-exception)
              | Structuring class Range @ attribute end
              +-+---------------- 1 ----------------
                | Traceback (most recent call last):
                |   File "<cattrs generated structure lsprotocol.types.Position>", line 5, in structure_Position
                |     res['line'] = __c_structure_line(o['line'])
                | TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
                | Structuring class Position @ attribute line
                +------------------------------------

Error receiving data
  + Exception Group Traceback (most recent call last):
  |   File "/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/pygls/protocol.py", line 404, in _deserialize_message
  |     return self._converter.structure(data, request_type)
  |   File "/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/cattrs/converters.py", line 309, in structure
  |     return self._structure_func.dispatch(cl)(obj, cl)
  |   File "<cattrs generated structure lsprotocol.types.TextDocumentCodeActionRequest>", line 26, in structure_TextDocumentCodeActionRequest
  |     if errors: raise __c_cve('While structuring ' + 'TextDocumentCodeActionRequest', errors, __cl)
  | cattrs.errors.ClassValidationError: While structuring TextDocumentCodeActionRequest (1 sub-exception)
  +-+---------------- 1 ----------------
    | Exception Group Traceback (most recent call last):
    |   File "<cattrs generated structure lsprotocol.types.TextDocumentCodeActionRequest>", line 10, in structure_TextDocumentCodeActionRequest
    |     res['params'] = __c_structure_params(o['params'], __c_type_params)
    |   File "<cattrs generated structure lsprotocol.types.CodeActionParams>", line 31, in structure_CodeActionParams
    |     if errors: raise __c_cve('While structuring ' + 'CodeActionParams', errors, __cl)
    | cattrs.errors.ClassValidationError: While structuring CodeActionParams (1 sub-exception)
    | Structuring class TextDocumentCodeActionRequest @ attribute params
    +-+---------------- 1 ----------------
      | Exception Group Traceback (most recent call last):
      |   File "<cattrs generated structure lsprotocol.types.CodeActionParams>", line 15, in structure_CodeActionParams
      |     res['context'] = __c_structure_context(o['context'], __c_type_context)
      |   File "<cattrs generated structure lsprotocol.types.CodeActionContext>", line 21, in structure_CodeActionContext
      |     if errors: raise __c_cve('While structuring ' + 'CodeActionContext', errors, __cl)
      | cattrs.errors.ClassValidationError: While structuring CodeActionContext (1 sub-exception)
      | Structuring class CodeActionParams @ attribute context
      +-+---------------- 1 ----------------
        | Exception Group Traceback (most recent call last):
        |   File "<cattrs generated structure lsprotocol.types.CodeActionContext>", line 5, in structure_CodeActionContext
        |     res['diagnostics'] = __c_structure_diagnostics(o['diagnostics'], __c_type_diagnostics)
        |   File "/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/cattrs/converters.py", line 510, in _structure_list
        |     raise IterableValidationError(
        | cattrs.errors.IterableValidationError: While structuring typing.List[lsprotocol.types.Diagnostic] (1 sub-exception)
        | Structuring class CodeActionContext @ attribute diagnostics
        +-+---------------- 1 ----------------
          | Exception Group Traceback (most recent call last):
          |   File "/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/cattrs/converters.py", line 502, in _structure_list
          |     res.append(handler(e, elem_type))
          |   File "<cattrs generated structure lsprotocol.types.Diagnostic>", line 56, in structure_Diagnostic
          |     if errors: raise __c_cve('While structuring ' + 'Diagnostic', errors, __cl)
          | cattrs.errors.ClassValidationError: While structuring Diagnostic (1 sub-exception)
          | Structuring typing.List[lsprotocol.types.Diagnostic] @ index 2
          +-+---------------- 1 ----------------
            | Exception Group Traceback (most recent call last):
            |   File "<cattrs generated structure lsprotocol.types.Diagnostic>", line 5, in structure_Diagnostic
            |     res['range'] = __c_structure_range(o['range'], __c_type_range)
            |   File "<cattrs generated structure lsprotocol.types.Range>", line 14, in structure_Range
            |     if errors: raise __c_cve('While structuring ' + 'Range', errors, __cl)
            | cattrs.errors.ClassValidationError: While structuring Range (2 sub-exceptions)
            | Structuring class Diagnostic @ attribute range
            +-+---------------- 1 ----------------
              | Exception Group Traceback (most recent call last):
              |   File "<cattrs generated structure lsprotocol.types.Range>", line 5, in structure_Range
              |     res['start'] = __c_structure_start(o['start'], __c_type_start)
              |   File "<cattrs generated structure lsprotocol.types.Position>", line 14, in structure_Position
              |     if errors: raise __c_cve('While structuring ' + 'Position', errors, __cl)
              | cattrs.errors.ClassValidationError: While structuring Position (1 sub-exception)
              | Structuring class Range @ attribute start
              +-+---------------- 1 ----------------
                | Traceback (most recent call last):
                |   File "<cattrs generated structure lsprotocol.types.Position>", line 5, in structure_Position
                |     res['line'] = __c_structure_line(o['line'])
                | TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
                | Structuring class Position @ attribute line
                +------------------------------------
              +---------------- 2 ----------------
              | Exception Group Traceback (most recent call last):
              |   File "<cattrs generated structure lsprotocol.types.Range>", line 10, in structure_Range
              |     res['end'] = __c_structure_end(o['end'], __c_type_end)
              |   File "<cattrs generated structure lsprotocol.types.Position>", line 14, in structure_Position
              |     if errors: raise __c_cve('While structuring ' + 'Position', errors, __cl)
              | cattrs.errors.ClassValidationError: While structuring Position (1 sub-exception)
              | Structuring class Range @ attribute end
              +-+---------------- 1 ----------------
                | Traceback (most recent call last):
                |   File "<cattrs generated structure lsprotocol.types.Position>", line 5, in structure_Position
                |     res['line'] = __c_structure_line(o['line'])
                | TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
                | Structuring class Position @ attribute line
                +------------------------------------

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

Traceback (most recent call last):
  File "/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/pygls/protocol.py", line 510, in data_received
    self._data_received(data)
  File "/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/pygls/protocol.py", line 542, in _data_received
    json.loads(body.decode(self.CHARSET),
  File "/usr/local/lib/python3.10/json/__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "/usr/local/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.10/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "/home/vscode/.vscode-remote/extensions/charliermarsh.ruff-2023.8.0-linux-x64/bundled/libs/pygls/protocol.py", line 418, in _deserialize_message
    raise JsonRpcInvalidParams() from exc
pygls.exceptions.JsonRpcInvalidParams: Invalid Params
[Trace - 6:27:36 PM] Received notification 'window/showMessage'.
[Trace - 6:27:36 PM] Sending request 'textDocument/codeAction - (13)'.
[Trace - 6:27:36 PM] Received response 'textDocument/codeAction - (13)' in 8ms.

Any ideas as to how to fix this?
Thank you!

Duplicate issue #198: Unexpected error in LSP server, see server's logs for details, repeatedly, over and over again, repeatedly
Created: 2023-06-01T12:33:49Z
v2023.17.11351528 Pre-Release
I'm editing some code that's definitely broken -- like it's still pseudo code most places, and this message pops up over and over, every few seconds and the only way to get rid of it is to close it, only for it to show up again a few seconds later.
Reinstalled "Release Version" v2023.16.0
No change although it might be popping up more frequently as it seems to get so excited that it pops up over itself and pushes the other one down behind itself (like it's climbing over the old one).
Unfortunately, the error message doesn't  provide a clickable link to the log file to open it, nor does it provide any clues as to what "server's logs" it might be referring to or where they might be.  There are no .log files in my source tree including the virtualenv.  There are half-a-dozen places where people store log files these days and I've poked and stroked 'till my wrist got numb and I still ain't got no clue (with apologies to Frank Zappa).
The python ruff-lsp is not installed in my virtual env, so I'm not even sure from whence it's getting the idea that there is one...whereis ruff-lsp also comes up empty.
Where might I find a log file from which I might be able to glean a clue as to what's going on here?

Closing comment (2023-06-16T21:17:45Z): Blerg, I'm going to close as a duplicate of #154, since this is the same trace as the bottom error there.