Ran into this while working in the rustc repo. There are a number of paths in rustfmt.toml that are ignored, but if you open and then save any of them, then the rust-analyzer VSCode extension still formats the contents with rustfmt.
Minimal steps to reproduce
-
Create empty project with cargo new.
-
Write some poorly formatted code to src/main.rs:
fn main ()
{
println!("Hello, world!");
}
-
Ignore src/main.rs in rustfmt.toml:
ignore = [
"src/main.rs",
]
-
Demonstrate that nightly rustfmt respects this configuration:
$ cargo +nightly fmt -- src/main.rs
$ cat src/main.rs
fn main ()
{
println!("Hello, world!");
}
-
Enable formatOnSave using nightly rustfmt in one of two ways:
Option 1: rust-toolchain
In rust-toolchain:
In .vscode/settings.json:
{
"editor.formatOnSave": true
}
Option 2: overrideCommand
In .vscode/settings.json:
{
"rust-analyzer.rustfmt.overrideCommand": [
"/YOUR/HOME/.rustup/toolchains/nightly-YOUR-HOST-TRIPLE/bin/rustfmt"
],
"editor.formatOnSave": true
}
-
Open src/main.rs in VSCode and save.
Observed Behavior
The rust-analyzer extension formats the code in src/main.rs.
Expected behavior
The rust-analyzer extension respects the configuration in rustfmt.toml and
does not format the code in src/main.rs.
Ran into this while working in the rustc repo. There are a number of paths in
rustfmt.tomlthat are ignored, but if you open and then save any of them, then the rust-analyzer VSCode extension still formats the contents with rustfmt.Minimal steps to reproduce
Create empty project with
cargo new.Write some poorly formatted code to
src/main.rs:Ignore
src/main.rsinrustfmt.toml:Demonstrate that nightly rustfmt respects this configuration:
Enable
formatOnSaveusing nightlyrustfmtin one of two ways:Option 1: rust-toolchain
In
rust-toolchain:In
.vscode/settings.json:{ "editor.formatOnSave": true }Option 2: overrideCommand
In
.vscode/settings.json:{ "rust-analyzer.rustfmt.overrideCommand": [ "/YOUR/HOME/.rustup/toolchains/nightly-YOUR-HOST-TRIPLE/bin/rustfmt" ], "editor.formatOnSave": true }Open
src/main.rsin VSCode and save.Observed Behavior
The rust-analyzer extension formats the code in
src/main.rs.Expected behavior
The rust-analyzer extension respects the configuration in
rustfmt.tomlanddoes not format the code in
src/main.rs.