vscode-rust icon indicating copy to clipboard operation
vscode-rust copied to clipboard

Can I use stable rustc, but nightly rustfmt?

Open wigy-opensource-developer opened this issue 7 years ago • 10 comments

Version of VSCode: 1.28.1 Version of the extension: 0.4.10 OS: GNU/Linux x86_64

I tried to set the following in my workspace settings, but it seems to be ignored. I get different results running "Format Document" in VSCode and cargo +nightly fmt in the command line.

"rust.rustfmt_path": "${env:HOME}/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustfmt"

Is there a way to execute cargo +nightly fmt on format document?

That's tricky - you need to enable unstable_features but that's only allowed for nightly toolchains (here). Until rustfmt_path is stabilised I think you can try and hack it around by setting CFG_RELEASE_CHANNEL env var to nightly when starting VSCode. (Hopefully stuff won't break?)

Xanewok avatar Oct 19 '18 20:10 Xanewok

@wigy-opensource-developer did @Xanewok's suggestion work for you? I've tried without success.


EDIT: @wigy-opensource-developer I noticed that ${env:HOME} must be ${env.HOME} to resolve properly. After that it still didn't work though. As a workaround I've installed https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave and configured it as followed:

    "emeraldwalk.runonsave": {
        "commands": [
            {
                "match": "\\.rs",
                "isAsync": true,
                "cmd": "${env.HOME}/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustfmt ${file}",
            }
        ]
    },

I'm expecting the rls and runonsave to race against each other, though on a quick test runonsave seems to win the race ;-)

steveej avatar Oct 27 '18 22:10 steveej

That is a nice workaround, thanks. In the middle of a stressful release I did not want to dig up my VSCode, so I have not even tried the suggestion from @Xanewok, because I was unsure if I will be able to build using stable rustc afterwards.

Just as a note: my workaround causes a side effect of flushing the undo-history in case the file is changed by the configured command.

steveej avatar Nov 01 '18 19:11 steveej

I believe we can stabilise the rustfmt_path - it's not exactly rocket science and there are not a lot of moving parts where things can go wrong.

@jsgf Have you used the rustfmt_path extensively and if so did you encounter any problems with it so far?

Xanewok avatar Apr 07 '19 19:04 Xanewok

Hi! We haven't been using it. It looks like there's a bug because it passes --file-lines [] if its trying to do a full-file reformat, which ends up being a no-file reformat.

jsgf avatar May 20 '19 23:05 jsgf

When I try setting rust.rustfmt_path to C:\Users\radix\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\bin\rustfmt.exe (appropriately escaped in the JSON settings file), it doesn't seem to actually change any behavior -- it is still using the stable rustfmt. I know this because the following output shows up when I try to format:

Warning: can't set `fn_single_line = true`, unstable features are only available in nightly channel.

radix avatar Jan 04 '20 16:01 radix

This is solved via a rust-analyzer setting in vscode:

https://github.com/rust-analyzer/rust-analyzer/issues/3627#issuecomment-612021748

(edit) originally from https://github.com/rust-analyzer/rust-analyzer/issues/3916#issuecomment-611617573

rrichardson avatar May 28 '21 18:05 rrichardson

For anyone using rustanalyzer, adding the +nightly arg to the rustanalyzer's extra args worked (i.e., stable rustc with "nightly" rustfmt):

"rust-analyzer.rustfmt.extraArgs": [
        "+nightly"
    ]

janu-cambrelen avatar Jan 02 '22 06:01 janu-cambrelen

Here a full "How to" for others. Using vscode and rust-analyzer.

First, make sure you have the nightly rustfmt installed. It can be done easily with rustup: > rustup toolchain install nightly

Than, for vscode, in settings.json

"rust-analyzer.rustfmt.extraArgs": [
        "+nightly"
    ],

Finally, in the project dir, we can use a the "unstable_features" by activating them in a rustfmt.toml file. Here is mine as example:

# I can't rely on contributors using .editorconfig
newline_style = "Unix"
# require the shorthand instead of it being optional
use_field_init_shorthand = true
# outdated default — `?` was unstable at the time
# additionally the `try!` macro is deprecated now
use_try_shorthand = true
# Max to use the 100 char width for everything or Default. See https://rust-lang.github.io/rustfmt/?version=v1.4.38&search=#use_small_heuristics
use_small_heuristics = "Max"

# Unstable features below
unstable_features = true
version = "Two"
# code can be 100 characters, why not comments?
comment_width = 100
# force contributors to follow the formatting requirement
error_on_line_overflow = true
error_on_unformatted = true
# next 4: why not?
format_code_in_doc_comments = true
format_macro_bodies = true
format_macro_matchers = true
format_strings = true
# better grepping
imports_granularity = "Module"
# quicker manual lookup
group_imports = "StdExternalCrate"
# why use an attribute if a normal doc comment would suffice?
normalize_doc_attributes = true
# why not?
wrap_comments = true

GilShoshan94 avatar Feb 06 '22 15:02 GilShoshan94