Repository: rust-lang/rust-mode (language: rust)

Issue:
syntax highlight error on Emacs 30
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:

``` rust
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:

```elisp
(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.


Discussion / hints:
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 ?
I have the same on 29.2, since my last update.
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`
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.
> 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))
```
> 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.

``` elisp
 (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)
      )
```
@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.
```elisp
(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)

```bash
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.
@condy0919 

> There is no warning about treesit, syntax highlight works fine.

Sorry, I'm confused. Is it working for you or it is not ? 
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.
@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.
Switching from `-Q` to `-q` has no difference, it seems like not caused by site-lisp. weird.
@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.
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.
I figured it out.

```elisp
(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.


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)`

``` text
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")
```

```elisp
;;; 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` .

Fix patch:
diff --git a/rust-mode-treesitter.el b/rust-mode-treesitter.el
index dbad06a..89c7cd4 100644
--- a/rust-mode-treesitter.el
+++ b/rust-mode-treesitter.el
@@ -5,7 +5,6 @@
 
 ;;; Code:
 
-;;;###autoload
 (when (version<= "29.1" emacs-version)
   ;; We have the when macro because of
   ;; https://github.com/rust-lang/rust-mode/issues/520


Test patch: