rust-lang / vscode-rust

Rust extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=rust-lang.rust
Other
1.39k stars 167 forks source link

Can I use stable rustc, but nightly rustfmt? #438

Open wigy-opensource-developer opened 5 years ago

wigy-opensource-developer commented 5 years ago

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?

Xanewok commented 5 years ago

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?)

steveej commented 5 years ago

@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 ;-)

wigy-opensource-developer commented 5 years ago

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.

steveej commented 5 years ago

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.

Xanewok commented 5 years ago

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?

jsgf commented 5 years ago

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.

radix commented 4 years ago

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.
rrichardson commented 3 years ago

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

janu-cambrelen commented 2 years ago

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"
    ]
GilShoshan94 commented 2 years ago

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