swyddfa / esbonio

A language server for working with Sphinx projects.
https://docs.esbon.io/
124 stars 21 forks source link

Crash: ValueError: Position.character should be in range [0:2147483647], but was... #714

Closed CendioOssman closed 6 months ago

CendioOssman commented 6 months ago

I'm getting this crash when parsing one of our files:

Error receiving data
  + Exception Group Traceback (most recent call last):
  |   File "/home/ossman/.local/lib/python3.11/site-packages/pygls/protocol/json_rpc.py", line 330, in _deserialize_message
  |     return self._converter.structure(data, request_type)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/home/ossman/.local/lib/python3.11/site-packages/cattrs/converters.py", line 332, 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/ossman/.local/lib/python3.11/site-packages/cattrs/converters.py", line 531, 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/ossman/.local/lib/python3.11/site-packages/cattrs/converters.py", line 521, 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 0
          +-+---------------- 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 (1 sub-exception)
            | Structuring class Diagnostic @ attribute range
            +-+---------------- 1 ----------------
              | Traceback (most recent call last):
              |   File "<cattrs generated structure lsprotocol.types.Position>", line 16, in structure_Position
              |     return __cl(
              |             ^^^^
              |   File "<attrs generated init lsprotocol.types.Position>", line 7, in __init__
              |     __attr_validator_character(self, __attr_character, self.character)
              |   File "/home/ossman/.local/lib/python3.11/site-packages/lsprotocol/validators.py", line 44, in uinteger_validator
              |     raise ValueError(
              | ValueError: Position.character should be in range [0:2147483647], but was 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.
              | 
              | During handling of the above exception, another exception occurred:
              | 
              | 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 19, in structure_Position
              |     except Exception as exc: raise __c_cve('While structuring ' + 'Position', [exc], __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 16, in structure_Position
                |     return __cl(
                |             ^^^^
                |   File "<attrs generated init lsprotocol.types.Position>", line 7, in __init__
                |     __attr_validator_character(self, __attr_character, self.character)
                |   File "/home/ossman/.local/lib/python3.11/site-packages/lsprotocol/validators.py", line 44, in uinteger_validator
                |     raise ValueError(
                | ValueError: Position.character should be in range [0:2147483647], but was 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.
                +------------------------------------

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

Traceback (most recent call last):
  File "/home/ossman/.local/lib/python3.11/site-packages/pygls/protocol/json_rpc.py", line 443, in data_received
    self._data_received(data)
  File "/home/ossman/.local/lib/python3.11/site-packages/pygls/protocol/json_rpc.py", line 475, in _data_received
    json.loads(
  File "/usr/lib64/python3.11/json/__init__.py", line 359, in loads
    return cls(**kw).decode(s)
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ossman/.local/lib/python3.11/site-packages/pygls/protocol/json_rpc.py", line 344, in _deserialize_message
    raise JsonRpcInvalidParams() from exc
pygls.exceptions.JsonRpcInvalidParams: Invalid Params

This is via vscode-ltex. All it says at its end is:

[Error - 12:58:49] Request textDocument/codeAction failed.
  Message: Message could not be parsed.
  Code: -32700 
[object Object]

Unsure what you need to debug this, so please let me know how to proceed.

alcarney commented 6 months ago

This looks like an issue I've seen reported elsewhere (vscode-restructuredtext/vscode-restructuredtext#433) and (vscode-restructuredtext/vscode-restructuredtext#429)

The TL;DR is that a common pattern in VSCode extensions when reporting a diagnostic that includes the end of the line, is to set the column number to a very large value, well outside the range allowed by the language server protocol ([0:2147483647]). When asking esbonio for any available code actions, VSCode includes all diagnostics in your project as additional context, leading to the situation where any VSCode extension has the opportunity to accidentally break the server.

I keep meaning to push an update to make the server less pedantic about these issues, hopefully now that there's an issue in this repo to remind me, I can get something out sooner rather than later :sweat_smile:

alcarney commented 6 months ago

0.16.4 of esbonio is now available with a patch that should hopefully fix the issue you are seeing

CendioOssman commented 6 months ago

Thanks for your quick attention!

I upgraded to 0.16.4, and I'm no longer getting any crashes on the files it previously complained about. :)

alcarney commented 6 months ago

Glad to hear it! :)