rust-lang / rust-lang/rust-analyzer

`formatOnSave` with VSCode extension does not respect `ignore` config in rustfmt.toml

Open
#10,826 10 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-ide C-bug S-unactionable
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the rust-analyzer VSCode extension's formatOnSave path and compare its rustfmt invocation with cargo +nightly fmt using the rustfmt.toml ignore setting. Reproduce with .vscode/settings.json and the ignored src/main.rs example. Done means saving the ignored file leaves it unchanged, with coverage for the configuration and format-on-save behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, vscode
Domain
developer-experience, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.