Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File mode specification error: (void-function rust-mode) #528

Closed
jroimartin opened this issue Mar 5, 2024 · 12 comments · Fixed by #530
Closed

File mode specification error: (void-function rust-mode) #528

jroimartin opened this issue Mar 5, 2024 · 12 comments · Fixed by #530

Comments

@jroimartin
Copy link
Contributor

The following error is returned when opening a .rs file:

File mode specification error: (void-function rust-mode)

It seems to be caused by:

rust-mode/rust-mode.el

Lines 78 to 79 in d8a09f2

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))

Because after the last changes to support treesitter, no rust-mode is autoloaded.

rust-mode/rust-mode.el

Lines 74 to 76 in d8a09f2

(if (and (version<= "29.1" emacs-version) rust-mode-treesitter-derive)
(require 'rust-mode-treesitter)
(require 'rust-prog-mode))

(define-derived-mode rust-mode prog-mode "Rust"

(define-derived-mode rust-mode rust-ts-mode "Rust"

My current workaround it is to require 'rust-mode in my init.el. However, this was not necessary before and it woud be great to continue supporting lazy-loading.

@psibi
Copy link
Member

psibi commented Mar 8, 2024

How are you installing rust-mode ?

@psibi
Copy link
Member

psibi commented Mar 8, 2024

With this configuration I'm not able to reproduce the issue:

(use-package rust-mode
  :defer t
  :init
  (setq rust-mode-treesitter-derive t))

@jroimartin
Copy link
Contributor Author

@psibi plain package.el. These are the relevant pieces of my configuration:

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(package-selected-packages
   '(denote forge dockerfile-mode rust-mode yaml-mode magit go-mode markdown-mode)))
;; Install selected packages if any is missing.
(unless (seq-every-p #'package-installed-p package-selected-packages)
  (package-install-selected-packages))
;; Rust.
;; Requires: rustup [+toolchain] component add rust-analyzer
;; Indentation: 4 spaces
(customize-set-variable 'rust-indent-offset 4)
(add-hook 'rust-mode-hook
          #'(lambda ()
              (setq indent-tabs-mode nil)))
(add-hook 'rust-mode-hook
          #'(lambda ()
              (eglot-ensure)
              (add-hook 'before-save-hook #'eglot-format-buffer nil t)))

@psibi
Copy link
Member

psibi commented Mar 8, 2024

Thanks!

Does reverting this PR fix it: https://github.com/rust-lang/rust-mode/pull/526/files ?

@jroimartin
Copy link
Contributor Author

@psibi probably, but that would break the treesitter derived mode, right?

If I'm not wrong, you choose the right rust-mode based on this:

rust-mode/rust-mode.el

Lines 74 to 76 in d8a09f2

(if (and (version<= "29.1" emacs-version) rust-mode-treesitter-derive)
(require 'rust-mode-treesitter)
(require 'rust-prog-mode))

What about doing something like this? (not tested)

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode-choose))

;;;###autoload
(defun rust-mode-choose ()
  "Choose the appropriate rust-mode."
  (if (and (version<= "29.1" emacs-version) rust-mode-treesitter-derive)
      (require 'rust-mode-treesitter)
    (require 'rust-prog-mode))
  (rust-mode))

@psibi
Copy link
Member

psibi commented Mar 8, 2024

That sounds fair, can you send out a PR for it ? CC: @condy0919 What do you think about the above proposed patch ?

jroimartin added a commit to jroimartin/rust-mode that referenced this issue Mar 8, 2024
This PR fixes the following error that happends when opening a .rs
file:

	File mode specification error: (void-function rust-mode)

It adds a stub function called `rust-mode-choose` that selects the
appropriate `rust-mode` version (prog or treesitter) based on the
environment.

Fixes rust-lang#528
@psibi
Copy link
Member

psibi commented Mar 8, 2024

(add-to-list 'auto-mode-alist '("\.rs\'" . rust-mode-choose))

Thinking further, I'm not so sure about this: Since rust-mode-chose is not a mode.

@jroimartin
Copy link
Contributor Author

jroimartin commented Mar 8, 2024

I have just created #530. You can see in the default auto-mode-alist value, that this pattern is commonly used. For instance,

     ("[/.]c\\(?:on\\)?f\\(?:i?g\\)?\\(?:\\.[a-zA-Z0-9._-]+\\)?\\'" . conf-mode-maybe)
(defun conf-mode-maybe ()
  "Select Conf mode or XML mode according to start of file."
  (if (save-excursion
	(save-restriction
	  (widen)
	  (goto-char (point-min))
	  (looking-at "<\\?xml \\|<!-- \\|<!DOCTYPE ")))
      (xml-mode)
    (conf-mode)))

@psibi
Copy link
Member

psibi commented Mar 8, 2024

Cool, thanks!

@jroimartin
Copy link
Contributor Author

That being said, I'm far from being an experienced elisp programmer. So, take everything I said with a grain of salt.

jroimartin added a commit to jroimartin/rust-mode that referenced this issue Mar 8, 2024
This PR fixes the following error that happends when opening a .rs
file:

	File mode specification error: (void-function rust-mode)

It adds a stub function called `rust-mode-choose` that selects the
appropriate `rust-mode` version (prog or treesitter) based on the
environment.

Fixes rust-lang#528
jroimartin added a commit to jroimartin/rust-mode that referenced this issue Mar 8, 2024
This PR fixes the following error that happends when opening a .rs
file:

	File mode specification error: (void-function rust-mode)

It adds a stub function called `rust-mode` that selects the actual
`rust-mode` (prog or treesitter) based on the environment. Once the
actual `rust-mode` is loaded, the stub function gets redefined.

Fixes rust-lang#528
@condy0919
Copy link
Contributor

Good job!

jroimartin added a commit to jroimartin/rust-mode that referenced this issue Mar 11, 2024
This PR fixes the following error that happends when opening a .rs
file:

	File mode specification error: (void-function rust-mode)

It conditionally autoloads the proper rust-mode version depending on
the user environment.

Fixes rust-lang#528
@ia0
Copy link

ia0 commented Mar 12, 2024

Thanks! I confirm the fix on my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants