emacs-lsp / lsp-mode

Emacs client/library for the Language Server Protocol
https://emacs-lsp.github.io/lsp-mode
GNU General Public License v3.0
4.8k stars 893 forks source link

lsp-rust: rust-analyzer fails on startup - [ERROR rust_analyzer] Unexpected error: Failed to deserialize InitializeParams: invalid type: map, expected u32; #3629

Closed JHicks01 closed 2 years ago

JHicks01 commented 2 years ago

Thank you for the bug report

Bug description

lsp-rust: rust-analyzer fails on startup with the following error:

[ERROR rust_analyzer] Unexpected error: Failed to deserialize InitializeParams: invalid type: map, expected u32;

Steps to reproduce

I'm using latest versions of all packages, and can reproduce by opening main.rs in a new/empty rust project.

Expected behavior

Not to see the error, rust-analyzer to startup correctly

Which Language Server did you use?

lsp-rust with rust-analyzer

OS

Linux

Error callstack

No response

Anything else?

No response

yyoncho commented 2 years ago

Are you using emacs 29 from master?

JHicks01 commented 2 years ago

Are you using emacs 29 from master?

No, emacs 28.1:

GNU Emacs 28.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.33, cairo version 1.16.0) of 2022-06-11

psibi commented 2 years ago

What is the rust-analyzer version you are using ? There was a release even two days ago, so letting us know an explicit version would help us:

❯ rust-analyzer --version
rust-analyzer 2022-06-13
JHicks01 commented 2 years ago

What is the rust-analyzer version you are using ? There was a release even two days ago, so letting us know an explicit version would help us:

❯ rust-analyzer --version
rust-analyzer 2022-06-13

rust-analyzer 0.3.1131-standalone (897a7ec4b 2022-07-17)

JHicks01 commented 2 years ago

If it helps, I just tried with rls instead of rust-analyzer, and got a similar error:

[2022-07-22T19:43:52Z ERROR rls::server] dispatch error: Error { code: InvalidParams, message: "invalid type: map, expected u64", data: None }

So doesn't seem to be related to the language server itself.

yyoncho commented 2 years ago

Can you paste what is the output if this M-: (lsp--json-serialize (list :processId nil)) RET

JHicks01 commented 2 years ago

Can you paste what is the output if this M-: (lsp--json-serialize (list :processId nil)) RET

"{\"processId\":{}}"

yyoncho commented 2 years ago

This should return null instead of {}. I compiled emacs from master, emacs from 28.1 branches and it still works fine. I don't have a clue why it works like that on your side.

yyoncho commented 2 years ago

How did you obtain that version of emacs?

JHicks01 commented 2 years ago

Built emacs-28.1 release.

I just tried uninstalling and rebuilding, same thing happens.

yyoncho commented 2 years ago

Can you give me the exact commit

JHicks01 commented 2 years ago

Can you give me the exact commit

Sorry, I was just using the release from e.g. http://ftp.gnu.org/gnu/emacs/. I'll try again later with e.g. latest commit of the 28 branch.

JHicks01 commented 2 years ago

Can you give me the exact commit

Just tried with #2529e8200232aa8ce252084634d81a2809fb26fa (https://github.com/emacs-mirror/emacs/commit/2529e8200232aa8ce252084634d81a2809fb26fa)

Still get the same error.

yyoncho commented 2 years ago

can you tell me what is your jansson library version?

JHicks01 commented 2 years ago

can you tell me what is your jansson library version?

Is that a dependency of something? I don't appear to have it installed.

JHicks01 commented 2 years ago

FYI - same thing on emacs master. I guess something with my setup must be wrong but I have no idea what it could be.

yyoncho commented 2 years ago

The issue is that json-serialize a function written in C and calling jansson library is misbehaving. It doesn't serialize nil as json null but it serializes it to {}. Given the fact that I cannot reproduce with that same emacs commit then I believe that it might be caused by something external (e. g. Jansson library itself).

JHicks01 commented 2 years ago

The issue is that json-serialize a function written in C and calling jansson library is misbehaving. It doesn't serialize nil as json null but it serializes it to {}. Given the fact that I cannot reproduce with that same emacs commit then I believe that it might be caused by something external (e. g. Jansson library itself).

Thank you for the pointers. You are right, looks like it was the fault of that lib. It looks like I had a different version installed, which must not have been working for whatever reason (I'm not familiar enough with any of this to diagnose).

After installing the libjansson-dev package, it seems to work.

Strangely, I also tried using the --with-json=no option when building emacs, which, unless I misunderstand, should avoid using the lib, but this still resulted in the same error.

Anyway, I'll close this now as it appears to be fine with the package installed. Thank you for the help!

senselessDev commented 2 years ago

I was wasting quite a lot of time fixing this issue with texlab and am not sure about what actually fixed it for me. But for anyone also having this problem, be sure to reinstall lsp-mode (e.g. by rm -rf ~/.emacs.d/elpa/develop/lsp-mode-20220727.817/ or similar) to recompile the lisp bytecode.

FYI, (lsp--json-serialize (list :processId nil)) is still giving "{\"processId\":{}}" for me, but lsp works nevertheless (null gets transmitted).

maddenp commented 2 years ago

I had the same issue with lsp-clojure (see https://github.com/clojure-lsp/clojure-lsp/issues/1197) ad solved it in the same way, by installing the libjansson-dev package from my OS package manager. It seems to me that lsp-mode (at least with some language servers?) fails in an unexpected and hard-to-diagnose way if emacs is not built against libjansson, so would it be possible somehow to have the installation of lsp-mode fail if this library isn't available, if it's true that it will not function correctly without it? I don't know enough to say that that's a fact, though.

yyoncho commented 2 years ago

@maddenp based on this comment(and few other) it seems like there is version of json.el (emacs elisp json handling library) which serializes nil to {}. Can someone attach the json.el version that is causing the trouble(use M-x find-library)?

On my system the following ...

(let ((json-false :json-false))
  (json-encode '(:a nil)))

... returns {"a":null}

maddenp commented 2 years ago

Hi @yyoncho. On a system where I can reproduce the lsp-clojure issue, that bit of elisp also returns {"a":null}, so I suspect it won't help, but I'm attaching the file returned by find-library on json.el. If there's anything else I can try or help with, I'd be happy to. json.el.gz

yyoncho commented 2 years ago

@maddenp hm... that's odd. (lsp--json-serialize (list :processId nil)) is already working correctly, right?

maddenp commented 2 years ago

On my good system, (lsp--json-serialize (list :processId nil)) returns {"processId":null}. On the problem system, it returns {"processId":{}}. Looking the source of lsp--json-serialize, there's a conditional that calls either json-encode or json-serialize. The former encodes nil -> null as discussed above, but the latter -- which appears to be the preferred option, called if defined -- encodes nil -> {}, and it looks like that's what's being called. It doesn't seem to honor the :null-object value passed in by lsp--json-serialize as advertised, but my elisp knowledge is getting thin here.

ahmedriza commented 2 years ago

I got the same error when I initially compiled https://github.com/emacs-ng/emacs-ng and used that version of emacs (I'm running Fedora 36). However, after installing jansson-devel:

sudo dnf install jansson-devel

and recompiling, everything works as expected.