rust-lang / rust-lang/rustfmt

rustfmt produces different results when run from build.rs script when in workspace crate

Open
#6,613 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs-triage
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

We have a build.rs script that produces a source file and then runs "rustfmt" on it. We use a "rustfmt.toml" file with style_edition = "2021" to try to get the same behavior as running cargo fmt.

However, when building, we get this output:

impl BlockStorageAllocateBlockAsyncCallbackResult {
    fn decode(src: u32) -> Result<Self, DecodeError> {
        let result = match src {
            1 => BlockStorageAllocateBlockAsyncCallbackResult::Ok,
            2 => BlockStorageAllocateBlockAsyncCallbackResult::AlreadyExists,
            3 => BlockStorageAllocateBlockAsyncCallbackResult::Error,
            _ => return Err(DecodeError::new(format!(
                "Decode BlockStorageAllocateBlockAsyncCallbackResult: Unsupported enum value: {}",
                src
            ))),
        };
        Ok(result)
    }
}

But then running cargo fmt on the same workspace produces this output:

impl BlockStorageAllocateBlockAsyncCallbackResult {
    fn decode(src: u32) -> Result<Self, DecodeError> {
        let result = match src {
            1 => BlockStorageAllocateBlockAsyncCallbackResult::Ok,
            2 => BlockStorageAllocateBlockAsyncCallbackResult::AlreadyExists,
            3 => BlockStorageAllocateBlockAsyncCallbackResult::Error,
            _ => {
                return Err(DecodeError::new(format!(
                "Decode BlockStorageAllocateBlockAsyncCallbackResult: Unsupported enum value: {}",
                src
            )))
            }
        };
        Ok(result)
    }
}

Note the extra braces.

This seems to happen when running from a workspace containing the crate specifically. It is annoying as building will produce code that requires re-running "cargo fmt" (else it fails our PR pipeline)

Minimal reproducible example available here: https://github.com/mattdurak/sample_rustfmt_bug

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 by reproducing the behavior with the linked minimal example, focusing on the workspace crate, build.rs, rustfmt.toml, and the cargo fmt comparison. Trace how rustfmt is invoked in each context and finish when generated code and cargo fmt produce consistent formatting without requiring a second formatting pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.