rust-lang / rust.vim

Vim configuration for Rust.
Apache License 2.0
3.9k stars 297 forks source link

Consistent way to ensure rustfmt uses 2018 edition #439

Open zyxw59 opened 3 years ago

zyxw59 commented 3 years ago

rust.vim version

vim-plug, current rust.vim commit 87c745d8d506fc1eecc1d81df15d5bde1658a2fc

Steps to reproduce:

Option (A): Specify --edition 2018 in g:rustfmt_options in .vimrc (or equivalent) Option (B): Do not specify --edition 2018 in g:rustfmt_options in .vimrc (or equivalent)

  1. Create a new rust project, 2018 edition.
  2. Use an async fn (or other 2018-specific syntax).
  3. Run :RustFmt
  4. Add a rustfmt.toml file which does not specify edition
  5. Run :RustFmt

Expected behavior:

At least one of option (A) or (B) works (correctly formats the file at steps 3 and 5)

Actual behavior

Option (A) fails at step 3, with "Option 'edition' given more than once" Option (B) fails at step 5, with "async fn is not permitted in the 2015 edition"

chris-morgan commented 3 years ago

This isn’t a rust.vim bug but a rustfmt one. I suggest filing a bug against rustfmt to the effect that --edition 2018 should override any value from rustfmt.toml, rather than conflicting with it.

I was then intending to close this WONTFIX since there’s nothing that rust.vim can reasonably do about this, but then I got to wondering why it’s g:rustfmt_options with no b:rustfmt_options. Seems reasonable to add that, I think?

zyxw59 commented 3 years ago

The conflict isn't between --edition 2018 and rustfmt.toml, it's between the --edition 2018 specified in g:rustfmt_options and the --edition 2018 added here when rustfmt.toml isn't present.

scbrickley commented 3 years ago

I have the same issue. Everything works fine when I run rustfmt from the command line, but when I run it in vim, I get the error mentioned above.

I have the following line in my vimrc, and it fails when a run :RustFmt in vim in a rust project that doesn't have a rustfmt.toml file.

let g:rustfmt_options = '--edition=2018'

My fix was to add a global .rustfmt.toml file in my home directory, as described here: https://rust-lang.github.io/rustfmt/