openlawlibrary / pygls

A pythonic generic language server
https://pygls.readthedocs.io/en/latest/
Apache License 2.0
588 stars 105 forks source link

pydantic throws ValidationError #243

Closed slackline closed 1 year ago

slackline commented 2 years ago

I've encountered an error that pydantic has with pygls when used by jedi-language-server...

  File "/home/neil/.local/lib/python3.9/site-packages/pygls/protocol.py", line 152, in deserialize_message
    deserialize_params(data, get_params_type)
  File "/home/neil/.local/lib/python3.9/site-packages/pygls/protocol.py", line 138, in deserialize_params
    data['params'] = params_type(**params)
  File "pydantic/main.py", line 406, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for InitializeParams
processId
  value is not a valid integer (type=type_error.integer)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/neil/.local/bin/jedi-language-server", line 8, in <module>
    sys.exit(cli())
  File "/home/neil/.local/lib/python3.9/site-packages/jedi_language_server/cli.py", line 125, in cli
    SERVER.start_io()
  File "/home/neil/.local/lib/python3.9/site-packages/pygls/server.py", line 204, in start_io
    self.loop.run_until_complete(
  File "/usr/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/home/neil/.local/lib/python3.9/site-packages/pygls/server.py", line 75, in aio_readline
    proxy(b''.join(message))
  File "/home/neil/.local/lib/python3.9/site-packages/pygls/protocol.py", line 456, in data_received
    json.loads(body.decode(self.CHARSET),
  File "/usr/lib/python3.9/json/__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "/usr/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.9/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
  File "/home/neil/.local/lib/python3.9/site-packages/pygls/protocol.py", line 154, in deserialize_message
    raise JsonRpcInvalidParams()
pygls.exceptions.JsonRpcInvalidParams: Invalid Params

The authors of jedi-language-server suggested it might be down to lsp-jedi who suggested it might be down to jedi-language-server, but the error appeares to stem from pygls and its use of pydantic (from my crude reading).

This is with the following versions installed under user account rather than virtualenv

❱ pip show pygls pydantic jedi-language-server         
Name: pygls
Version: 0.11.3
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: /home/neil/.local/lib/python3.9/site-packages
Requires: pydantic, typeguard
Required-by: jedi-language-server
---
Name: pydantic
Version: 1.8.2
Summary: Data validation and settings management using python 3.6 type hinting
Home-page: https://github.com/samuelcolvin/pydantic
Author: Samuel Colvin
Author-email: s@muelcolvin.com
License: MIT
Location: /home/neil/.local/lib/python3.9/site-packages
Requires: typing-extensions
Required-by: jedi-language-server, pygls
---
Name: jedi-language-server
Version: 0.36.0
Summary: A language server for Jedi!
Home-page: https://github.com/pappasam/jedi-language-server
Author: Sam Roeca
Author-email: samuel.roeca@gmail.com
License: MIT
Location: /home/neil/.local/lib/python3.9/site-packages
Requires: docstring-to-markdown, jedi, pydantic, pygls
Required-by: 

If there is any more useful information or steps I can take to help with investigating and resolving this please let me know.

dimbleby commented 2 years ago
pydantic.error_wrappers.ValidationError: 1 validation error for InitializeParams
processId
  value is not a valid integer (type=type_error.integer)

This assertion is the server claiming that the client is sending a bad message (specifically: a non-integer process ID).

You will need to get trace showing the message, and compare it with what the LSP specification says is allowed. I'm not an emacs user but I expect that its LSP client has some sort of setting to log the protocol flows.

Then you can either agree with the server that the client is sending a bad message - in which case raise the issue at the client; or you can disagree, in which case raise the issue at the server.

slackline commented 2 years ago

Thanks for the pointers, I'll try and get a trace out and work out where the problem is occurring.

tombh commented 1 year ago

Pygls has now migrated to lsprotocol, so no longer depends on Pydantic. lsprotocol itseslf uses https://github.com/python-attrs/cattrs

slackline commented 1 year ago

Thank you @tombh very much appreciated.

tombh commented 1 year ago

Our pleasure 🙇 The greater thanks goes to @alcarney in particular for the migration itself. And Brett and Karthik from Microsoft too ❤️