lsp-mode icon indicating copy to clipboard operation
lsp-mode copied to clipboard

`lsp-rust-analyzer-inlay-hints-mode` can't be automatically started

Open bertulli opened this issue 2 years ago • 2 comments

Thank you for the bug report

  • [X] I am using the latest version of lsp-mode related packages.
  • [X] I checked FAQ and Troubleshooting sections
  • [X] You may also try reproduce the issue using clean environment using the following command: M-x lsp-start-plain

Bug description

Hi all, when using lsp-mode, the type inference inlays don't work out of the box. Even if I set (setq lsp-rust-analyzer-server-display-inlay-hints t) in my init file, when I open a .rs file, the type inlays are not shown. However:

  • If I give M-x lsp-rust-analyzer-inlay-hints-mode, the mode is disabled
  • If I give M-x lsp-rust-analyzer-inlay-hints-mode again, the mode is started, and the inlays are shown!
  • On the other hand, evaluating (with M-:) (lsp-rust-analyzer-inlay-hints-mode), it is started on the first try.
  • Using lsp-start-plain, the double M-x method works, but the M-: one not.

Is this a bug or am I doing something wrong? Thanks for your time.

Steps to reproduce

  • Have lsp-mode, rust-mode, rustic and rust-analyzer installed.
  • Have called (in the init file or manually) (setq lsp-rust-analyzer-server-display-inlay-hints t)
  • Open a .rs file
  • Call twice M-x lsp-rust-analyzer-inlay-hints-mode

Expected behavior

Have the type inference inlays shown up automatically

Which Language Server did you use?

Rust-analyzer with Rustic mode

OS

Linux

Error callstack

No response

Anything else?

NOTE: when starting the server, the message LSP :: Error from the Language Server: waiting for cargo metadata or cargo check (Unknown error) shows up, but this may (or not?) be unrelated.

bertulli avatar Jul 19 '22 20:07 bertulli

EDIT: The reported behaviour happens having in my init file

(require 'rust-mode)
(require 'rustic)
(require 'lsp)
(require 'flycheck)

(with-eval-after-load 'lsp
  (with-eval-after-load 'rustic
    (setq lsp-rust-analyzer-server-display-inlay-hints t)
    (add-hook 'rustic-mode-hook #'lsp-rust-analyzer-inlay-hints-mode)
    ))

With this configuration, opening a .rs file it gives me File mode specification error: (void-function lsp-rust-analyzer-inlay-hints-mode), and indeed I can't see the function neither in M-x nor in C-h f.

However, only setting (setq lsp-rust-analyzer-server-display-inlay-hints t), doesn't give me the error, but I still need to automatically enable the inlays.

bertulli avatar Jul 19 '22 20:07 bertulli

Another thing: completely removing the snippet in the previous comment from my init file, I can't find nor the lsp-rust-analyzer variables, nor the function. It seems as the mode is not autoloaded, but gets loaded when I open a rust-analyzer project. However, even after that, adding the function to the hook doesn't show up the inlays, and I must activate it manually.

bertulli avatar Jul 19 '22 20:07 bertulli

I recommend use-package since it makes it easier to avoid those problems.

(use-package lsp-mode
  :config
  (setq lsp-rust-analyzer-server-display-inlay-hints t))

or

(use-package rustic)
(use-package lsp-mode)

(setq lsp-rust-analyzer-server-display-inlay-hints t)

In case you still have this issue with an up-to-date lsp-mode and rust-analyzer, feel free to reopen the issue.

brotzeit avatar Aug 23 '22 10:08 brotzeit

Hi, thanks for your help. Turned out that part of the problem is that lsp-rust.el needed to be manually required (via a require). Note that now I'm on GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.34, cairo version 1.17.6) of 2022-07-31, and I'm using the Arch Linux shipped version of rust-analyzer (I don't know if that's useful).

Now, however, I have another problem. When opening a Rust file, I get the message

LSP :: Connected to [rust-analyzer:16126/starting].
LSP :: rust-analyzer:16126 initialized successfully in folders: (/home/alessandro/Rust/Aoc-01-1)
LSP :: Error from the Language Server: waiting for cargo metadata or cargo check (Unknown error) [5 times]

Moreover, the inlay-hints-mode doesn't start automatically. Upon killing the buffer, and reopening (note: without restarting Emacs) I get this backtrace:

Debugger entered--Lisp error: (error "The connected server(s) does not support method textDocument/inlayHint.\nTo find out what capabilities support your server use ‘M-x lsp-describe-session’\nand expand the capabilities section")
  error("The connected server(s) does not support method %s.\nTo find out what capabilities support your server use `M-x lsp-describe-session'\nand expand the capabilities section" "textDocument/inlayHint")
  lsp--send-request-async((:jsonrpc "2.0" :method "textDocument/inlayHint" :params #<hash-table equal 2/65 0x15896d7a0a11>) #<subr F616e6f6e796d6f75732d6c616d626461_anonymous_lambda_107> tick nil nil nil nil)
  lsp-request-async("textDocument/inlayHint" #<hash-table equal 2/65 0x15896d7a0a11> #<subr F616e6f6e796d6f75732d6c616d626461_anonymous_lambda_107> :mode tick)
  lsp-rust-analyzer-update-inlay-hints(#<buffer main.rs>)
  lsp-rust-analyzer-inlay-hints-mode()
  run-hooks(change-major-mode-after-body-hook prog-mode-hook rust-mode-hook rustic-mode-hook)
  apply(run-hooks (change-major-mode-after-body-hook prog-mode-hook rust-mode-hook rustic-mode-hook))
  run-mode-hooks(rustic-mode-hook)
  rustic-mode()
  set-auto-mode-0(rustic-mode nil)
  set-auto-mode--apply-alist((("\\.odc\\'" . archive-mode) ("\\.odf\\'" . archive-mode) ("\\.odi\\'" . archive-mode) ("\\.otp\\'" . archive-mode) ("\\.odp\\'" . archive-mode) ("\\.otg\\'" . archive-mode) ("\\.odg\\'" . archive-mode) ("\\.ots\\'" . archive-mode) ("\\.ods\\'" . archive-mode) ("\\.odm\\'" . archive-mode) ("\\.ott\\'" . archive-mode) ("\\.odt\\'" . archive-mode) ("\\.uml\\'" . plantuml-mode) ("\\.td\\'" . tablegen-mode) ("\\.ll\\'" . llvm-mode) ("\\.lex\\'" . flex-mode) ("\\.yy\\'" . bison-mode) ("\\.[pP][dD][fF]\\'" . #f(compiled-function () #<bytecode 0x1dc13bd938d7>)) ("\\.hva\\'" . latex-mode) ("\\.jison\\'" . jison-mode) ("\\.l\\'" . flex-mode) ("\\.y\\'" . bison-mode) ("\\(?:build\\|profile\\)\\.boot\\'" . clojure-mode) ("\\.cljs\\'" . clojurescript-mode) ("\\.cljc\\'" . clojurec-mode) ("\\.\\(clj\\|cljd\\|dtm\\|edn\\)\\'" . clojure-mode) ("\\.cmake\\'" . cmake-mode) ("CMakeLists\\.txt\\'" . cmake-mode) ("/Jenkinsfile\\'" . groovy-mode) ("\\.g\\(?:ant\\|roovy\\|radle\\)\\'" . groovy-mode) ("/git-rebase-todo\\'" . git-rebase-mode) ("\\.\\(plantuml\\|pum\\|plu\\)\\'" . plantuml-mode) ("\\.rs\\'" . rustic-mode) ("\\.\\(?:md\\|markdown\\|mkd\\|mdown\\|mkdn\\|mdwn\\)\\'" . markdown-mode) ("/systemd/[^z-a]+?\\.d/[^/]+?\\.conf\\'" . systemd-mode) ("\\.#\\(?:[.0-9@-Z\\_a-z-]+?\\.\\(?:automount\\|busname\\|..." . systemd-mode) ("[.0-9@-Z\\_a-z-]+?\\.\\(?:automount\\|busname\\|link\\|m..." . systemd-mode) ("\\.nspawn\\'" . systemd-mode) ("\\.gpg\\(~\\|\\.~[0-9]+~\\)?\\'" nil epa-file) ("\\.elc\\'" . elisp-byte-code-mode) ("\\.zst\\'" nil jka-compr) ("\\.dz\\'" nil jka-compr) ("\\.xz\\'" nil jka-compr) ("\\.lzma\\'" nil jka-compr) ("\\.lz\\'" nil jka-compr) ("\\.g?z\\'" nil jka-compr) ("\\.bz2\\'" nil jka-compr) ("\\.Z\\'" nil jka-compr) ("\\.vr[hi]?\\'" . vera-mode) ("\\(?:\\.\\(?:rbw?\\|ru\\|rake\\|thor\\|jbuilder\\|rabl\\|ge..." . ruby-mode) ...) nil nil)
  set-auto-mode()
  normal-mode(t)
  after-find-file(nil t)
  find-file-noselect-1(#<buffer main.rs> "~/Rust/Aoc-01-1/src/main.rs" nil nil "~/Rust/Aoc-01-1/src/main.rs" (20064237 2067))
  find-file-noselect("/home/alessandro/Rust/Aoc-01-1/src/main.rs" nil nil nil)
  find-file("/home/alessandro/Rust/Aoc-01-1/src/main.rs")
  dired--find-file(find-file "/home/alessandro/Rust/Aoc-01-1/src/main.rs")
  dired--find-possibly-alternative-file("/home/alessandro/Rust/Aoc-01-1/src/main.rs")
  dired-find-file()
  funcall-interactively(dired-find-file)
  command-execute(dired-find-file)

bertulli avatar Aug 28 '22 21:08 bertulli

I forgot a possibly important thing: if, in my init file, I comment out the section on dap-mode, I still get the errors, but the inlays are successfully started!

bertulli avatar Aug 28 '22 21:08 bertulli

Does it work with a minimal config ? rustic requires lsp-rust so if you have a minimal config with (use-package lsp-mode) (use-package rustic) it should work.

brotzeit avatar Aug 29 '22 12:08 brotzeit

Ok, I think I (almost) got it. Following a suggestion from @nitincodery , I see this intervetion from yyoncho, where they suggested to use

(add-hook 'lsp-after-open-hook (lambda ()
                                 (when (lsp-find-workspace 'rust-analyzer nil)
                                   (lsp-rust-analyzer-inlay-hints-mode))))

instead of

(add-hook 'rustic-mode-hook #'lsp-rust-analyzer-inlay-hints-mode)

Putting that in my init.el, I still get a few errors, but the inlays works🎉. Specifically, sometimes I have to edit (even without saving) the buffer to refresh the display, but then they're on.

May I suggest to add this to the tutorial config page? It seems something other users can benefit from.

bertulli avatar Aug 30 '22 14:08 bertulli

Ok, I think I (almost) got it. Following a suggestion from @nitincodery , I see this intervetion from yyoncho, where they suggested to use

(add-hook 'lsp-after-open-hook (lambda ()
                                 (when (lsp-find-workspace 'rust-analyzer nil)
                                   (lsp-rust-analyzer-inlay-hints-mode))))

instead of

(add-hook 'rustic-mode-hook #'lsp-rust-analyzer-inlay-hints-mode)

Putting that in my init.el, I still get a few errors, but the inlays works🎉. Specifically, sometimes I have to edit (even without saving) the buffer to refresh the display, but then they're on.

May I suggest to add this to the tutorial config page? It seems something other users can benefit from.

I still get the same issue even with the lsp-after-open-hook lambda.

tiejunhu avatar Feb 22 '23 08:02 tiejunhu

Hi, I'm still having a similar issue, I can't see the inlays after just opening a rust file, but when I start editing the first character (whatever I do, e.g just adding a space somewhere) they show up. Is it by design?

giuliohome avatar Mar 26 '23 19:03 giuliohome

It still requires an edit before showing also for me.

bertulli avatar Mar 28 '23 09:03 bertulli

There is PR for moving this functionality in the core. After that, this issue should go away.

yyoncho avatar Mar 28 '23 09:03 yyoncho