Skip to content

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

@mattdurak

Description

@mattdurak

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions