rust-lang / rust-mode

Emacs configuration for Rust
Apache License 2.0
1.1k stars 176 forks source link

syntax highlight error on Emacs 30 #524

Closed condy0919 closed 4 months ago

condy0919 commented 4 months ago

There is a warning when Emacs starts.

⛔ Warning (treesit): Cannot activate tree-sitter, because language grammar for rust is unavailable (not-found): /home/condy/.emacs.d/tree-sitter/libtree-sitter-rust: cannot open shared object file

Then open a hello.rs file:

fn main() {
    println!("hello\n");
}

There is no syntax highlight.

The version of rust-mode: https://github.com/rust-lang/rust-mode/commit/a8f94697acbd9d71ec94b79a30685899e9c31c6a

My rust-mode config:

(use-package rust-mode
  :ensure t
  :mode ("\\.rs\\'" . rust-mode)
  :config
  (with-no-warnings
    (with-eval-after-load 'lsp-mode
      (setq lsp-rust-analyzer-diagnostics-disabled ["unresolved-extern-crate"])))
  :custom
  (rust-indent-where-clause t)
  (rust-format-on-save t)
  (rust-format-show-buffer nil))

When I disable rust-mode and delete ~/.emacs.d/elpa/rust-mode*/, no warning prompted.

psibi commented 4 months ago

Thanks for the detailed and a clear bug report!

I'm aware of the warning and I'm not sure what would be a best way to fix that. I'm open to PR's on the same.

There is no syntax highlight.

This is concerning. I'm not able to reproduce it in Emacs 29.1. Can you check if it is reproducible for you on Emacs 29.x series ?

dannywillems commented 4 months ago

I have the same on 29.2, since my last update.

timhillgit commented 4 months ago

What about using the function (treesit-language-available-p 'rust) instead of the variable rust-mode-treesitter-derive? That leaves less to configure and can be used inside rust-mode-treesitter.el

mplsgrant commented 4 months ago

I also have the same warning as condy0919, and I also have the syntax highlighting/formatting issue. I tested against 29.1 and 29.2.

Reverting to d7c3825 solves the issue.

psibi commented 4 months ago

What about using the function (treesit-language-available-p 'rust) instead of the variable rust-mode-treesitter-derive?

Yeah, I think that change might be helpful atleast inside the rust-mode-treesitter.el. Want to send a PR ?

@mplsgrant

I also have the same warning as condy0919, and I also have the syntax highlighting/formatting issue.

Can you share your configuration ? I'm unfortunately not able to reproduce it. This is how my configuration looks like:

(use-package rust-mode
  :quelpa (rust-mode :fetcher file
                     :path "~/github/rust-mode")
  :defer t
  :custom
  (rust-mode-treesitter-derive nil))
mplsgrant commented 4 months ago

Can you share your configuration ?

Just remembered I use rustic instead of rust-mode directly. If you are wondering, I was able to revert to an earlier commit by checking that commit out in my rust-mode straight repo.

 (use-package rustic
      :straight t
      :bind (:map rustic-mode-map
                  ("M-j" . lsp-ui-imenu)
                  ("C-c C-c l" . flycheck-list-errors)
                  ("C-c C-c a" . lsp-execute-code-action)
                  ("C-c C-c r" . lsp-rename)
                  ("C-c C-c q" . lsp-workspace-restart)
                  ("C-c C-c Q" . lsp-workspace-shutdown)
                  ("C-c C-c s" . lsp-rust-analyzer-status)
                  ("C-c C-c e" . lsp-rust-analyzer-expand-macro)
                  ("C-c C-c d" . dap-hydra)
                  ("C-}"       . lsp-goto-type-definition)
                  ("C-{"       . lsp-goto-implementation)
                  ("C-<right>" . right-word)
                  ("C-<left>"  . left-word)
                  ("C-c C-r"   . rustic-popup)
                  ("C-c C-p"   . projectile-command-map) ; Projectile usually uses C-c C-p, but it's too close to C-c p for my liking
                  ("M-?"       . lsp-find-references)
                  ("C-M-?"      . lsp-find-implementation)
                  ("M-/"       . lsp-ui-doc-glance)
                  ("M-C-/"     . flycheck-explain-error-at-point)
                  )
      :config
      ;; This is the rustic implementation from Robert Krahn
      ;; https://robert.kra.hn/posts/rust-emacs-setup/
      ;; uncomment for less flashiness
      (setq lsp-eldoc-hook nil)
      (setq lsp-enable-symbol-highlighting t)
      (setq lsp-signature-auto-activate nil)

      ;; comment to disable rustfmt on save
      (add-hook 'rustic-mode-hook 'rk/rustic-mode-hook))

    (defun rk/rustic-mode-hook ()
      ;; so that run C-c C-c C-r works without having to confirm, but don't try to
      ;; save rust buffers that are not file visiting. Once
      ;; https://github.com/brotzeit/rustic/issues/253 has been resolved this should
      ;; no longer be necessary.
      (when buffer-file-name
        (setq-local buffer-save-without-query t))
      (add-hook 'before-save-hook 'lsp-format-buffer nil t)
      )
psibi commented 4 months ago

@mplsgrant For rustic, you would have to use my fork: https://github.com/psibi/rustic

It contains the changes accounting for things in rust-mode and tree sitter.

condy0919 commented 4 months ago
(require 'package)
(setq package-archives
      '(("melpa"  . "https://melpa.org/packages/")
        ("gnu"    . "https://elpa.gnu.org/packages/")
        ("nongnu" . "https://elpa.nongnu.org/nongnu/")))

;; Bootstrap `use-package'
(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))
(eval-and-compile
  (setq use-package-always-ensure nil)
  (setq use-package-always-defer nil)
  (setq use-package-always-demand nil)
  (setq use-package-expand-minimally nil)
  (setq use-package-enable-imenu-support t))
(eval-when-compile
  (require 'use-package))

(use-package rust-mode
  :ensure t
  :mode ("\\.rs\\'" . rust-mode)
  :custom
  (rust-indent-where-clause t)
  (rust-format-on-save t)
  (rust-format-show-buffer nil))

I made a minimal init.el and deleted ~/.emacs/elpa/, then start Emacs and open a rs file, the syntax highlight is still broken :sob:

But when I start emacs in the following way (tested against emacs 29.2 and 30)

emacs -Q -L ~/.emacs.d/elpa/rust-mode-* -l ~/.emacs.d/init.el

There is no warning about treesit, and syntax highlight works fine for rs files.

psibi commented 4 months ago

@condy0919

There is no warning about treesit, syntax highlight works fine.

Sorry, I'm confused. Is it working for you or it is not ?

condy0919 commented 4 months ago

Under the normal startup of Emacs, rust-mode is still broken. It works well if and only if I start it with the -Q option.

psibi commented 4 months ago

@condy0919

Ah, I see what you mean. Looking at the emacs manual, it looks like -Q implies --no-site-file. Given your testing, it looks like there is some other elisp code lying in your machine which is causing the issue.

Unfortunately I can't help you much in this case since this is not reproducible at my end. My suggestion would be keep removing things manually to see which emacs extension is causing issue for you.

condy0919 commented 4 months ago

Switching from -Q to -q has no difference, it seems like not caused by site-lisp. weird.

psibi commented 4 months ago

@condy0919 Yeah, very strange. Can you see if removing the ~/.emacs.d/eln-cache* files work ? Infact, I would try removing everything under ~/.emacs.d and running things freshly to see if there is an issue.

condy0919 commented 4 months ago

I'm not using native-comp yet.

removing everything under ~/.emacs.d and running things freshly to see if there is an issue.

OK, let me figure it out.

condy0919 commented 4 months ago

I figured it out.

(use-package rust-mode
  :ensure t
  :mode ("\\.rs\\'" . rust-mode)
  :config
  (with-no-warnings
    (with-eval-after-load 'lsp-mode
      (setq lsp-rust-analyzer-diagnostics-disabled ["unresolved-extern-crate"])))
  :custom
  (rust-indent-where-clause t)
  (rust-format-on-save t)
  (rust-format-show-buffer nil))

Just remove the :mode ... line. It makes use-package load rust-mode eagerly.

condy0919 commented 4 months ago

The warning prompt is from rust-ts-mode. https://github.com/emacs-mirror/emacs/blob/39239982403f01a37d42d1cd8db0b2ed0b48b50c/lisp/progmodes/rust-ts-mode.el#L477

rust-ts-mode is required by rust-mode-autoloads.el, it's reported when I try (unload-feature 'rust-ts-mode)

Debugger entered--Lisp error: (error "Loaded libraries (\"/home/condy/.emacs.d/elpa/rust-mode-20240228.338/rust-mode-autoloads.el\") depend on /usr/share/emacs/30.0.50/lisp/progmodes/rust-ts-mode.elc")
;;; Generated autoloads from rust-mode-treesitter.el

(when (version<= "29.1" emacs-version)
  (require 'treesit)
  (require 'rust-ts-mode)
  (require 'rust-common)
  (define-derived-mode rust-mode rust-ts-mode "Rust" "Major mode for Rust code.

\\{rust-mode-map}" :group 'rust-mode (add-hook 'before-save-hook rust-before-save-hook nil t) (add-hook 'after-save-hook rust-after-save-hook nil t)))

The root cause is rust-mode-treesitter.el .

condy0919 commented 4 months ago

The solution is double-check in rust-mode-treesitter.el

;;;###autoload
(when (and (version<= "29.1" emacs-version) (treesit-language-available-p 'rust))

But it requires the CI machine must have treesit-rust.so installed, otherwise rust-mode-treesitter.el will be removed in release.

ia0 commented 4 months ago

I'm using rust-mode 20240302.1316 and I still have the issue. Something to note is that when I install rust-mode for the first time it works. It stops working after I restart emacs, i.e. rust-mode needs to be already installed when starting emacs to fail. I also try deleting the rust-mode-treesitter files in emacs.d/elpa but it didn't help. Not sure what's causing the issue.

ia0 commented 4 months ago

Actually the solution in #528 fixes the issue, i.e. adding (require 'rust-mode) explicitly.