rustfmt produces different results when run from build.rs script when in workspace crate
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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