-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
formatOnSave
with VSCode extension does not respect ignore
config in rustfmt.toml
#10826
Comments
So the root of the issue here is that we provide the file contents to rustfmt via stdin. And unfortunately, stdin does not respect ignore paths. We could parse the rustfmt.toml ourselves, and decide whether or not the file should be ignored (basically emulate the simple logic here: https://github.com/elmarco/rustfmt/blob/master/src/ignore_path.rs#L22-L30 |
It feels wrong to me that we need to parse(and find) the rustfmt.toml ourselves. Just like we don't want to parse(and find) the cargo config either. I wonder if it would make sense for rustfmt to accept a path via a flag and stdin input still, which then doesn't get formatted if the path matches the ignore set. Probably sounds too odd for the tool tho... |
If we can find Beware rust-lang/rustfmt#4660. |
The problem is we tell rustfmt to format this specific file by just handing it over the file contents. Rustfmt has no idea about the path of the file we are formatting because we only give it the contents. As such it doesn't check(can't in fact) whether the file belongs to the ignored files. |
Ah, right 🤦. Even if it can read the config file, it won't know what file it's formatting. |
Opened rust-lang/rustfmt#5137 |
This also applies if the module of the file was imported with a
#[rustfmt::skip]
mod hello;
fn world ()
{
println!("Hello, world!");
} Saving in |
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
:Ignore
src/main.rs
inrustfmt.toml
:Demonstrate that nightly rustfmt respects this configuration:
Enable
formatOnSave
using nightlyrustfmt
in one of two ways:Option 1: rust-toolchain
In
rust-toolchain
:In
.vscode/settings.json
:Option 2: overrideCommand
In
.vscode/settings.json
: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
anddoes not format the code in
src/main.rs
.The text was updated successfully, but these errors were encountered: