emacs-format-all-the-code icon indicating copy to clipboard operation
emacs-format-all-the-code copied to clipboard

emacs 29 impossible to pass argument to rustfmt

Open MikHulk opened this issue 4 months ago • 5 comments

I have tried several things and it seems impossible to pass arguments to rustmt.

especially the edition. Is it possible to specify --edition 2021. But if I set Rust formatter like that:

 ("Rust" (rustfmt "--edition 2021"))

or like that:

 ("Rust" (rustfmt "--edition" "2021"))

Or if I try with cargo:

 ("Rust" (cargo "fmt"))

It's still 2015 edition that is used.

As rust-mode provide its own format command I can workaround this qith a hook:

(add-hook
 'rust-mode-hook
 (lambda ()
   (define-key rust-mode-map (kbd "C-c C-f") 'rust-format-buffer)
 )
)

(add-hook
 'rust-ts-mode-hook
 (lambda ()
   (define-key rust-ts-mode-map (kbd "C-c C-f") 'rust-format-buffer)
 )
)

But format-all definitively doesn't work for Rust.

MikHulk avatar Feb 28 '24 18:02 MikHulk

Does this work:

(setq format-all-formatters
      '(("Rust" (rustfmt "--edition" "2021"))))

We should add Rust to the readme.

lassik avatar Mar 01 '24 10:03 lassik

Yes it works when I eval the expression in emacs buffer but that's exacttly what I have in 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.
 '(column-number-mode t)
 '(custom-enabled-themes '(tango-dark))
 '(custom-safe-themes
   '("43f03c7bf52ec64cdf9f2c5956852be18c69b41c38ab5525d0bedfbd73619b6a" default))
 '(dired-sidebar-subtree-line-prefix "_")
 '(dired-sidebar-theme 'ascii)
 '(dired-sidebar-width 25)
 '(display-line-numbers-type nil)
 '(format-all-default-formatters
   '(("Assembly" asmfmt)
     ("ATS" atsfmt)
     ("Bazel" buildifier)
     ("BibTeX" emacs-bibtex)
     ("C" clang-format)
     ("C#" csharpier)
     ("C++" clang-format)
     ("Cabal Config" cabal-fmt)
     ("Clojure" zprint)
     ("CMake" cmake-format)
     ("Crystal" crystal)
     ("CSS" prettier)
     ("Cuda" clang-format)
     ("D" dfmt)
     ("Dart" dart-format)
     ("Dhall" dhall)
     ("Dockerfile" dockfmt)
     ("Elixir" mix-format)
     ("Elm" elm-format)
     ("Emacs Lisp" emacs-lisp)
     ("Erlang" efmt)
     ("F#" fantomas)
     ("Fish" fish-indent)
     ("Fortran Free Form" fprettify)
     ("GLSL" clang-format)
     ("Go" gofmt)
     ("GraphQL" prettier)
     ("Haskell" brittany)
     ("HCL" hclfmt)
     ("HTML" html-tidy)
     ("HTML+EEX" mix-format)
     ("HTML+ERB" erb-format)
     ("Hy" emacs-hy)
     ("Java" clang-format)
     ("JavaScript" prettier)
     ("JSON" prettier)
     ("JSON5" prettier)
     ("Jsonnet" jsonnetfmt)
     ("JSX" prettier)
     ("Kotlin" ktlint)
     ("LaTeX" latexindent)
     ("Less" prettier)
     ("Literate Haskell" brittany)
     ("Lua" lua-fmt)
     ("Markdown" prettier)
     ("Meson" muon-fmt)
     ("Nix" nixpkgs-fmt)
     ("Objective-C" clang-format)
     ("OCaml" ocp-indent)
     ("Perl" perltidy)
     ("PHP" prettier)
     ("Protocol Buffer" clang-format)
     ("PureScript" purty)
     ("Python" black)
     ("R" styler)
     ("Reason" bsrefmt)
     ("ReScript" rescript)
     ("Ruby" rufo)
     ("Rust" (rustfmt "--edition" "2021"))
     ("Scala" scalafmt)
     ("SCSS" prettier)
     ("Shell" shfmt)
     ("Solidity" prettier)
     ("SQL" sqlformat)
     ("Svelte" prettier)
     ("Swift" swiftformat)
     ("Terraform" terraform-fmt)
     ("TOML" prettier)
     ("TSX" prettier)
     ("TypeScript" prettier)
     ("V" v-fmt)
     ("Verilog" istyle-verilog)
     ("Vue" prettier)
     ("XML" html-tidy)
     ("YAML" prettier)
     ("Zig" zig)
     ("_Angular" prettier)
     ("_Beancount" bean-format)
     ("_Caddyfile" caddy-fmt)
     ("_Flow" prettier)
     ("_Gleam" gleam)
     ("_Ledger" ledger-mode)
     ("_Nginx" nginxfmt)
     ("_Snakemake" snakefmt)))
;; ...

MikHulk avatar Mar 01 '24 11:03 MikHulk

That's weird. Can you set format-all-debug to t, then run format-all-buffer in a Rust buffer and look in the *Messages* buffer to find the shell command that format-all tried to run?

lassik avatar Mar 01 '24 11:03 lassik

Hi,

I retried with this configuration with the rust-mode hook deactivated and it seems to work now with this configuration. Sorry for that, but I must have made mistake when I was trying several things with the .emacs...

MikHulk avatar Mar 02 '24 12:03 MikHulk

Ah no... that's because I was trying on souce with no async construction.

That's the message I got:

You can run the command ‘eval-expression’ with M-:
t
Format-All: Formatting lib.rs as Rust using rustfmt
Format-All: Running: /etc/profiles/per-user/mik/bin/rustfmt
Format-All: Directory: /home/mik/Dev/Rust/osc_dispatcher/src/
Type q in help window to delete it.
Formatting error
Mark set

And the error from rustfmt:

error[E0670]: `async fn` is not permitted in Rust 2015
 --> <stdin>:8:5
  |
8 | pub async fn run<F, Fut>(process: F) -> Result<(), Box<dyn std::error::Error>>
  |     ^^^^^ to use `async fn`, switch to Rust 2018 or later
  |
  = help: pass `--edition 2021` to `rustc`
  = note: for more on editions, read https://doc.rust-lang.org/edition-guide

error: `async move` blocks are only allowed in Rust 2018 or later
  --> <stdin>:22:42
   |
22 | ...                   tokio::spawn(async move {
   |                                    ^^^^^^^^^^

And after:

(setq format-all-formatters
      '(("Rust" (rustfmt "--edition" "2021"))))

it works:

(("Rust" (rustfmt "--edition" "2021")))
You can run the command ‘eval-expression’ with M-:
(("Rust" (rustfmt "--edition" "2021")))
Format-All: Formatting lib.rs as Rust using rustfmt --edition 2021
Format-All: Running: /etc/profiles/per-user/mik/bin/rustfmt --edition 2021
Format-All: Directory: /home/mik/Dev/Rust/osc_dispatcher/src/
Already formatted
Mark set [2 times]

MikHulk avatar Mar 02 '24 12:03 MikHulk