Cargo fmt not working

Why does my cargo fmt does not work ?

Toolchain is 1.91, rustup is up to date

cargo fmt -v
[lib (2024)] "src/lib.rs"
[bin (2024)] "src/main.rs"
rustfmt --edition 2024 src/lib.rs src/main.rs
cargo fmt -- -v
Formatting src/app.rs
Formatting src/central_panel.rs
Formatting src/lib.rs
Spent 0.001 secs in the parsing phase, and 0.005 secs in the formatting phase
Formatting src/main.rs
Spent 0.000 secs in the parsing phase, and 0.000 secs in the formatting phase

But I have other file like "src/central_panel.rs" which contains non formatted code

When I run manually rustfmt

rustfmt src/central_panel.rs  -v
Formatting src/central_panel.rs
Spent 0.001 secs in the parsing phase, and 0.004 secs in the formatting phase

When I add a big trailing space in central_panel.rs I have

error[internal]: left behind trailing whitespace
   --> src/central_panel.rs:181:181:1
    |
181 |                                 
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |

warning: rustfmt has failed to format. See previous 1 errors.

and cargo clippy is not throwing any error

rustfmt will silently skip formatting a section of code if it fails to find a way to format it that is under the maximum line length. The most common culprit is long string literals, but deeply nested code can also be a problem.

Please show us the contents of central_panel.rs so we can evaluate if that is the case.

Hi, thanks

i think it's related to a "deeply nested"

My code is like that

fn function(){
    ScrollArea::vertical().show_viewport(ui, |ui, rect| {
             // some loop and if - not formatted
    });
}

I found a potential fix

fn function(){
    let closure = |ui, rect| {
             // some loop and if - formatted
    };
    ScrollArea::vertical().show_viewport(ui, closure);
}

Why rustfmt display an error?

It seems like terrible UX/DX to have a formatter not doing it's job without showing any error :confused:

1 Like

Or how do we change the max lenght?

Right now in my code the max lenght is only Col 90 - not that deeply nested

Thanks for the debugging but do you know more about rustfmt?

I can't help you diagnose the problem without seeing the exact text of the file. But it sounds like you found it by shortening it.

Do you mean, why did it not show an error? I am not familiar with rustfmt’s original design, but I think the idea is that, in cases that it cannot satisfy its formatting rules, it tries to leave formatting of the problematic code to you and format the remainder, so you get partial formatting help rather than no formatting help at all.

I do agree that it would be nice if it showed at least a warning about the line length — but someone would have to put in the work to do that. rustfmt doesn’t have very many people working on it, and it’s a hard problem to improve it without breaking any of its prior formatting stability guarantees. Help is wanted.

Create a file named rustfmt.toml with:

max_width = 120

Configuration documentation can be found at https://rust-lang.github.io/rustfmt/. There are a number of width options for individual cases too.

1 Like

rustfmt will never modify the code you wrote in such a way that the AST is different. Every change will only be of the non-semantic formatting (effectively, whitespace). This means that if you have to extract an fn argument to be a binding, rustfmt is not the tool that would do that for you.

1 Like

I don't need to extract the fn argument ^^

I'm just saying that, by making my fn as a closure variable, the rustfmt is suddenly working
-> Why ?

Okay thanks, that's also a working option

Seems still super weird to me to do that (since this will format part of my code with this new width)


I tried to have a MRE and I think it's because of a string

resp.on_hover_text(format!(
    "You cannot use this button right now because the char is {ascii_char} and this char is not allowed"
));

And my string is ending at col: 109

After checking, if I have size_of_line > max_width rustfmt just quit (with no error reported🙁 )

New questions:

  • Is there a setting in rustfmt to tell be verbose and the user "I don't work with line width more than X" ? (the default verbose -v don't do that)
  • Is there a setting in rustfmt more like allow_max_width to allow rustfmt to format/accept more than X max_width but still format with the default max_width

You can break such a string into multiple lines like this:

resp.on_hover_text(format!(
    "You cannot use this button right now because the char is \
        {ascii_char} and this char is not allowed"
));

\ at the end of a line eats the newline and all following whitespace, so this literal has the same contents. (The built-in macro concat!() is also an option, if you prefer.)

Yes, but it's unstable: error_on_unformatted.

3 Likes

Thanks!

This has been a known issue for years: Gives up on chains if any line is too long. · Issue #3863 · rust-lang/rustfmt · GitHub

2 Likes