Skip to content
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

Open
BGR360 opened this issue Nov 22, 2021 · 7 comments
Labels
A-ide general IDE features C-bug Category: bug S-unactionable Issue requires feedback, design decisions or is blocked on other work

Comments

@BGR360
Copy link

BGR360 commented Nov 22, 2021

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

  1. Create empty project with cargo new.

  2. Write some poorly formatted code to src/main.rs:

     fn   main ()
     {
    println!("Hello, world!");
     }
  3. Ignore src/main.rs in rustfmt.toml:

    ignore = [
        "src/main.rs",
    ]
  4. Demonstrate that nightly rustfmt respects this configuration:

    $ cargo +nightly fmt -- src/main.rs
    $ cat src/main.rs
     fn   main ()
     {
    println!("Hello, world!");
     }
    
  5. Enable formatOnSave using nightly rustfmt in one of two ways:

    Option 1: rust-toolchain

    In rust-toolchain:

    nightly
    

    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
    }
  6. 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.

@jhgg
Copy link
Contributor

jhgg commented Dec 3, 2021

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

@Veykril
Copy link
Member

Veykril commented Dec 3, 2021

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...

@lnicola
Copy link
Member

lnicola commented Dec 3, 2021

If we can find rustfmt.toml, can't we use --config-path?

Beware rust-lang/rustfmt#4660.

@Veykril
Copy link
Member

Veykril commented Dec 3, 2021

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.

@lnicola
Copy link
Member

lnicola commented Dec 3, 2021

Ah, right 🤦. Even if it can read the config file, it won't know what file it's formatting.

@Veykril
Copy link
Member

Veykril commented Dec 14, 2021

Opened rust-lang/rustfmt#5137

@sosthene-nitrokey
Copy link

This also applies if the module of the file was imported with a #[rustfmt::skip]:

src/lib.rs:

#[rustfmt::skip]
mod hello;

src/hello.rs:

 fn   world ()
 {
println!("Hello, world!");
 }

Saving in hello.rs will reformat the file though it shouldn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ide general IDE features C-bug Category: bug S-unactionable Issue requires feedback, design decisions or is blocked on other work
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants