rustfmt: Gives up on chains if any line is too long.
If there is a method call chain, and just one call exceeds max_width, it seems like rustfmt gives up and doesn’t format anything.
Example:
fn f() {
foo("This text is under the max_width limit, and shouldn't cause any problems on its own.").long("But this line is extra long, and doesn't fit within 100 max_width. 1234567890123456789").baz().collect().unwrap();
}
No format changes will be applied to this. I’m also a bit surprised that error_on_line_overflow does not complain.
I would expect it to wrap each call chain element, even if one of them is too long, such as:
fn f() {
foo("This text is under the max_width limit, and shouldn't cause any problems on its own.")
.long("But this line is extra long, and doesn't fit within 100 max_width. 1234567890123456789")
.baz()
.collect()
.unwrap();
}
All default settings. rustfmt 1.4.8-nightly (afb1ee1c 2019-09-08)
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 140
- Comments: 57 (23 by maintainers)
Commits related to this issue
- Wrap some really long lines. These do not wrap due to https://github.com/rust-lang/rustfmt/issues/3863. — committed to ehuss/cargo by ehuss 7 years ago
- Format strings with rustfmt https://github.com/rust-lang/rustfmt/issues/3863#issuecomment-678632593 — committed to NilsIrl/MozWire by NilsIrl 6 years ago
- Define svg paths in const multiline literals, allowing fmt to work Fmt completely breaks when a long line is present. See https://github.com/rust-lang/rustfmt/issues/3863 — committed to rparrett/bevy_prototype_lyon by rparrett 4 years ago
- Cleanup SVG example - Define svg paths in multiline const literals so that cargo fmt will work. See https://github.com/rust-lang/rustfmt/issues/3863 - Use Bevy's SpatialBundle — committed to rparrett/bevy_prototype_lyon by rparrett 4 years ago
- Cleanup SVG example - Define svg paths in multiline const literals so that cargo fmt will work. See https://github.com/rust-lang/rustfmt/issues/3863 - Fix shack not showing up at all due to missing... — committed to rparrett/bevy_prototype_lyon by rparrett 4 years ago
- Cleanup SVG example - Define svg paths in multiline const literals so that cargo fmt will work. See https://github.com/rust-lang/rustfmt/issues/3863 - Fix shack not showing up at all due to missing... — committed to rparrett/bevy_prototype_lyon by rparrett 4 years ago
- Cleanup SVG example (#167) - Define svg paths in multiline const literals so that cargo fmt will work. See https://github.com/rust-lang/rustfmt/issues/3863 - Fix shack not showing up at all due t... — committed to Nilirad/bevy_prototype_lyon by rparrett 4 years ago
- [wgsl-in] Break up long string, reformat rest of file. Whitespace and formatting changes only. It turns out that if `cargo fmt` comes across a single line it can't make fit within the margins, then ... — committed to jimblandy/naga by jimblandy 4 years ago
- [wgsl-in] Break up long string, reformat rest of file. (#2057) Whitespace and formatting changes only. It turns out that if `cargo fmt` comes across a single line it can't make fit within the mar... — committed to gfx-rs/naga by jimblandy 4 years ago
- Reformat long strings to avoid rustfmt bugs. (#580) https://github.com/rust-lang/rustfmt/issues/3863 — committed to MIERUNE/plateau-gis-converter by ciscorn 2 years ago
- style: fix incorrect indentation Turns out rustfmt just givesup on formatting if it can't ensure the code width is less than `max_width`. see: https://github.com/rust-lang/rustfmt/issues/3863 — committed to 0xAdk/mm2_tool by 0xAdk 2 years ago
- Split long line and run cargo fmt Still it does not work properly. See: * https://github.com/rust-lang/rustfmt/issues/3391 * https://github.com/rust-lang/rustfmt/issues/3863 * https://github.com/rust... — committed to tucanos/pytucanos by jeromerobert 2 years ago
- Split long line and run cargo fmt Still it does not work properly. See: * https://github.com/rust-lang/rustfmt/issues/3391 * https://github.com/rust-lang/rustfmt/issues/3863 * https://github.com/rust... — committed to tucanos/pytucanos by jeromerobert 2 years ago
- Reformat due to rustfmt bug https://github.com/rust-lang/rustfmt/issues/3863 — committed to 0xSiO/yombar by 0xSiO 2 years ago
- Rollup merge of #136606 - thaliaarchi:format-long-lines, r=ibraheemdev Fix long lines which rustfmt fails to format rustfmt fails to format this match expression, because it has several long string ... — committed to jhpratt/rust by jhpratt 2 years ago
- Rollup merge of #136606 - thaliaarchi:format-long-lines, r=ibraheemdev Fix long lines which rustfmt fails to format rustfmt fails to format this match expression, because it has several long string ... — committed to jhpratt/rust by jhpratt 2 years ago
- Unrolled build for #136606 Rollup merge of #136606 - thaliaarchi:format-long-lines, r=ibraheemdev Fix long lines which rustfmt fails to format rustfmt fails to format this match expression, because ... — committed to rust-lang-ci/rust by rust-timer 2 years ago
- Format with style_edition = 2024 Also one-time use of format_strings = true to work around https://github.com/rust-lang/rustfmt/issues/3863 — committed to valadaptive/ntsc-rs by valadaptive 2 years ago
- fix rustfmt (#3190) TIL rustfmt gives up on the entire block if it includes a string long enough to not fit on a single line: https://github.com/rust-lang/rustfmt/issues/3863 This PR makes the error... — committed to vortex-data/vortex by onursatici 2 years ago
- Move default tracing_subscriber filter to variable Allows rustfmt to format the line (see https://github.com/rust-lang/rustfmt/issues/3863) — committed to gpluscb/tetra by gpluscb 2 years ago
I very much agree with Josh here, but I think even bigger issue is that there are no visible errors/warnings that formatting is not possible and why. It just silently gives up, leaving user guessing why this particular part of the code is not formatted correctly.
How has this bug been open for almost 5 years without a fix? “Just giving up without any error” is unacceptable behavior for a formatter.
So be respectful when you speak. Don’t act like a self-entitled git (in the traditional sense of the word).
@calebcartwright In general, I’d expect rustfmt to indent everything to the correct level, and try to wrap appropriately at line-width, but whether it’s successful or not, it should continue on to wrap the rest, yes.
It’s not “except within chains”, it’s “when possible, without violating more important constraints like properly indenting”. If you have an indent-width of 4, and (say) 15 levels of indentation and a 50-character function name, you cannot format that without exceeding 100 characters, and that shouldn’t stop you from continuing on to format more of the line. And if you need to wrap the function parameters, those should still get indented 16 levels, even if one of them is a long string.
I just ran into this as well. My code:
rustfmt seems to completely give up after the line with the long help text, and doesn’t even try to reindent the
.conflicts_with, or fix the spacing in its argument, or add a trailing comma in either of the two places that should have trailing commas.I’d expect rustfmt to do best-effort on the long line, and then continue afterward.
This was affecting me for a few days, was getting around it by just manually formatting but I was getting desperate. I installed nightly and added the extra fields which ended upbreaking a string and reflowing another expression. I’ve found though that if you have highly nested match/if statements the whitespace counts towards the max width. I imagine most use spaces so that’s 2-4 characters wasted on each indent instead of 1 per tab. Indents also can’t be “reflowed” so it creates a similar problem to strings which are >97 (accounting for the "/’ and the trailing comma if used in a function argument.
I think this is an absolutely insane limitation that it doesn’t warn or error at all outside of nightly. I had been trying to find any reason this wasn’t compiling and it turns out this issue was exactly what I was running into the whole time.
@ilyvion Your snippet clued me into whitespace being an issue as well. For yours I’d expect the formatter to break the closure into multiple lines. You’re right that this silently failing is a huge problem.
I’m unsure why this has been allowed to persist for 4-5 years. Especially given that because it’s silent and it only ever doesn’t run when it can’t compile. So when it compiles and doesn’t format… It’s a game of whack a mole to figure out which fmt rule is breaking, installing nightly and developing within that isn’t feasible since it’s nightly.
Why don’t you fix it yourself? 🤔
You do not need strings to get this bug:
And the giving up propagates to the whole block.
Do I understand correctly that if I have too long string literal then rustfmt will just ignore the whole function without any warning / error, and there is nothing that stable toolchain users can do about it because format_strings and https://rust-lang.github.io/rustfmt/?version=master&search=overflow#error_on_line_overflow are all unstable? And this situation is going on for 5 years now? Great…
Here’s my suggestion:
Rustfmt should treat this case the same as Prettier, Biome, Black, Ruff, and I assume many other auto-formatters which I’m not familiar with. Namely, treat the max width as a target but not a strict maximum. They try their best to get lines to fit under that length, but if they can’t, they can’t and that’s fine. They still get the line as short as possible.
It could write a warning to stderr, with a config option disable_warning_on_ignored_line.
Both existing options, next to only existing in nightly, have the problem, that you still have the problem with code styling.
If you set a longer max line length it restyles all your code.
If you set the error on overflow, it errors and you have to fiddle around to get the line shorter.
Which is also not so easy viewable, because IDE inserts virtually the types and makes lines virtually longer they are.
And overall I don’t understand why it ignores the other lines of code block following the long line.
This looks like a bug and should have been fixed separately to introducing options as a workaround.
Using format_strings pretty much fixed it for me as strings get broken up and
rustfmtcan keep the line length belowmax_width.I strongly agree with this.
I have spent a while being very confused about why
rustfmtwasn’t formatting some trailing whitespace that broke our CI for our project, as well as not formatting some other lines. Given the robustness and friendly error reporting of Rust tools, I would have expected it to format as much as possible and at least print a warning.At the moment, it fails silently until something triggers a CI failure or an isolated line change suddenly causes an entire screenful of code to be reformatted.
Now that I know about this issue I can fix it when I see it, but I don’t see a way to identify when it happens (other than when it happens to cause trailing whitespace, which is not every time). Does anyone know any arguments to
rustfmtthat will reveal when it runs into this bug?@nazar-pc try running with
--config error_on_line_overflow=truewhich exists for precisely this purpose (make sure you’re on nightly), it’s just disabled by default. If you’d like to make a case for that being enabled by default then #3391 would be the place to do soexample output with that enabled:
It seems entire match statements’ stop getting formatted if you have a long string-literal (eg. in a todo!(…)), and if you then happen to have a trailing white-space on the end of a list of variants as shown in my example below, then rust-fmt errors out causing the rest of the code to stop formatting too (atleast in vscode/rust-analyzer):
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e9fed6e242d365cef07f21538724e3c4
i don’t have the required programming experience and the rust skills yet
I would like an option to have
max_widthbe the width of the line not including the indentation, or a switch, likerelative_max_widthYeah, AFAICT that’s because rustfmt is expecting to be able to rewrite each of the individual ChainItems to be able to format the Chain. When it attempts to rewrite that long string literal in the Chain it fails due to the length which results in the entire original chain being left as-is.
https://github.com/rust-lang/rustfmt/blob/a15e97f1e92b3621ab8c2f1585f27e8de583f74b/src/chains.rs#L419-L444
I think I see a way we could support still wrapping chain elements like your expected output, though it would mean rustfmt emitting a line that exceeds the max_width (although the original line does too, and the wrapped version is more human friendly IMO even with the one longer line).
@topecongiro @scampi - any concerns with that? if not, then I’ll take a look at implementing something for consideration.
Another example where a closure is part of the chain:
Update regarding the above: apparently raw strings are allowed to be too long, so I will just convert my long strings to raw strings now.
This means that
r#"aaa....aaa"#is formatted correctly even it’s too long, whereas"aaa....aaa"does not.Before I make this a new issue, can someone check whether or not this issue I’m running into has the same cause as this issue? This isn’t the exact code I’m having trouble with, but it behaves identically, so it’s a good MRE: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f52f18c42f46824eb308281c23ed978e
If you run
rustfmton it, you’ll see the code doesn’t get properly formatted. In particular,,dynamic_assetsremains without a space after the comma, but also any other bad formatting in the same block remains broken if you mess it up further and try to run formatting on that.To make
rustfmtwork again, simply shorten the length of the parameters in the closure. You can make it|asset: Vec<AssetServer>, dynamic: Vec<DynamicAssets>|, e.g. and it formats, or|asset_server: Vec<Asset>, dynamic_assets: Vec<Dynamic>|but it feels wrong to me that formatting entirely stops working in a whole block of code just because some names/lines end up too long.Seems weird that it tries to put as much as possible on 1 line just to break itself. E.g. here formatting doesn’t work, nothing is reported with
error_on_line_overflowIf I shorten the struct name a bit then it starts working and it puts even more stuff on that problematic line. Took me several hours to pinpoint this specific place in the long chain of methods that are needed for rspc to create the procedures handler.
This actually reports the very long line error.
Discussed above (e.g. https://github.com/rust-lang/rustfmt/issues/3863#issuecomment-700441739) and in a few other threads, but you can turn on
error_on_line_overflowanderror_on_unformattedfor this.This is a topic that pops up frequently so I’ll reiterate here that it’s important to distinguish between what rustfmt does vs. what editor plugins do, especially since we have no influence/insight into what those various plugin teams do. The decision to not apply any formatting to an entire file in some scenarios is behavior that some editor plugins have opted to apply, and is not related to rustfmt behavior in any capacity.
In these unformattable scenarios rustfmt only leaves the the top level expression as you wrote it, but will absolutely format everything else in the file.
Additionally, the aforementioned config options, though disabled by default, will highlight your lines that are too long. If you have a scenario where that’s not the case (when running
rustfmtdirectly, not editor behavior) then happy to take a look but would ask a separate issue be filed with steps to reproduce.Option (b)
Attempt to dynamically derive and then use a formatting shape with an increased width (which exceeds
max_width) which will be used for that specific chain element.The reason this would be necessary is to provide some required constraints when formatting those subexpressions to avoid horrific formatting that would try to collapse/single line everything out lines to infinity. I’d envision this operating along the lines of:
max_widthof 100long_chain_scale_factor = 20%, or rustfmt could attempt to guess one based on what it sees in the AST)This would be more complex, both from a user experience/mental model and implementation, but if we get it right would probably be much helpful for that cases with more complex args within chained calls.
With the above being said, I have finally managed to set aside some time to dig into this and believe I see a viable path forward.
As noted previously the current behavior will remain the default, but I’m planning on introducing a new config option that will provide some additional variants users can leverage to control chain formatting behavior. That would include one variant that will perform the standard chain formatting behavior, but unlike the default behavior, it will attempt to make a best effort to continue formatting chains which in part exceed the defined
max_widthvalue as requested here.We’d also most likely want to encourage users opting for this non-default behavior to strongly consider pairing it with other options like
error_on_line_overflow(which we’ll be adjusting to include off/warn/error mode variants and then hopefully stabilizing) so that you’ll be aware of cases where your formatting result exceeded your defined value formax_width.There’s only two feasible approaches I can see for handling an element within a chain that must exceed
max_width. I’d like to run a poll of sorts to gauge interest on those, so I’ll details those two in separate comments (options a and b below) and ask folks to use reactions on those comments to provide their feedback.Note that these aren’t mutually exclusive and I’m open to supporting both if there’s interest. As such I’d ask folks to primarily utilize the “thumbs up” to indicate favor/interest, and only use the “thumbs down” if you adamantally feel the option shouldn’t even exist (don’t downvote it just because you wouldn’t use it yourself).
I’d also ask that folks refrain from requesting other options on this issue. The original use case posed in the issue is pretty straightforward and the solution I have in mind will present a resolution to this. It will also create a framework of sorts through which additional variants/formatting behaviors can be added down the road, each of which should be proposed/discussed independently in a separate issue.
Finally, in cases where a chain in a right hand side (rhs) position (e.g. an assignment) is too long fit within the boundary with either shape (i.e. same line as lhs/assignment operator vs. next line block indented), rustfmt will have to try to pick where to put the chain, and I’m thinking that we should base that on which of the two will minimize the amount of formatted chain that ends up on the right of the defined
max_widthboundary. There’s not a ton of alternatives to that which come to mind for me, but if anyone has questions/alternatives to that particular piece feel free to ask/suggest.@lpil I’ve actually had this happen, I believe the reason is that the following line is concatenated to the end of the comment. When I forced it to format it would merge the commented line and potentially drop entire comments with lossy reformats. It highly depends on the syntax around the comment.
@lpil Have to find out the line length that is configured by rustfmt; I don’t remember the default off the top of my head but if you explicitly set it in
.rustfmt.tomlthen that’s it.Then plug it into either of the following for e.g. 100 characters:
I’ve also found that in many cases temporarily setting the line length to be really long - like 150 or whatever - then doing a
cargo fmt, and resetting or unsetting the line length config option sometimes will “unstuck” the long lines.Unfortunately the solution above did not actually work in my real code. Below is a reduced example with raw strings:
becomes
I just had this issue as well. I have an array which spans a lot of lines and it happens to contain some long strings, and as a result almost the entire file has no formatting. The strings contain URLs, which is why they are so long.
Here is an example:
becomes
Is this something that is planned to be fixed, or is there some reason to keep this behaviour?
rustfmt 1.7.1-nightly
I have more simple example to cause “left behind trailing whitespace” error.
No worries! It’s a rather long and fairly old thread at this point so just wanted to note there were some other posts that drilled deeper into the details since I was too lazy to fully recap 😆
Thanks, I confirm that using both
error_on_line_overflowanderror_on_unformatteddoes what I want. I’m sorry for not having properly read the previous comments about this, that was wrong of me. I’ve also edited my above comment to clarify my comment about “the entire file”.Option (a)
Use the original input for that element (whatever the user originally wrote), with some minor adjustments like indentation of the start of that element, and trimming trailing spaces.
This would directly solve the case originally reported in this issue with the desired format being produced for the provided input, as well as most other input snippets reported throughout the thread. It’s also something we could get out on nightly fairly quickly, potentially before the end of the month. However, it likely wouldn’t help all that much in cases where the exceeding chain item is caused by a more complex arg to a chained call, e.g. a closure with various other statements/expressions/etc., as that sub-content wouldn’t be processed
I appreciate those that have commented here, ostensibly in an attempt to avoid opening duplicative issues. However, I feel like in doing so the dialog has started drifting off topic.
As a reminder, rustfmt is strictly a pretty-printer style formatter that deals directly with the AST, so even if you experience similar symptoms from a different type of input AST node/syntax, it’s still a fundamentally different root cause that will almost always need a different investigation and resolution. I’d encourage folks in such a scenario (e.g. long strings but on a struct instead of within a chain) to look for/open a separate issue that’s based on your input, and simply add a link to other issues with similar symptoms if you think they may related.
Finally, while we do have some options available moving forward (which I’ll enumerate in a separate comment), I want to stress that this current behavior is unequivocally not a bug. It is intentional, it has always been the default, and it will continue to be the default.
The decision was made long, long ago to have width boundaries and associated wrapping as a core part of rustfmt when determining what the output should be for a given AST node, and in cases where it is completely impossible for rustfmt to honor the width settings (which the user controls, implicitly or explicitly via
max_width) it is up to the user to refactor their code accordingly; rustfmt can’t just categorically run past your width boundary and carry on formatting any type of AST node without any concept of bounding constraints. That’s especially relevant for chains/calls because they frequently have more complex structures beyond string lits (e.g. closures, nested chains, etc.) containing other AST nodes that also have to be formatted according to the same rules.A work around that can be beneficial in certain cases is to use the
include,include_str, andinclude_bytesmacros.What defines the relative importance of one constraint vs. another though? Is there a consensus on which constraints can be violated? Does the style guide have a prescription? The only thing I’m familiar with (and I know you’re far more familiar with the style guide than I @joshtriplett 😄) is https://github.com/rust-dev-tools/fmt-rfcs/blob/7416e12356a55aae959e9629b58eb7c7c3c86f6f/guide/guide.md#indentation-and-line-width
It’s not that we can’t technically make this change, but in these types of scenarios where rustfmt can’t satisfy all the constraints it bails and defers to the programmer to format, or refactor, as needed. This is usually quite manageable, and often accompanied with advice like
refactor your code to avoid long/complex expressions, usually by extracting a local variable or using a shorter namePrecisely and agreed, I was just trying to keep the example in the context of chains given the issue.
There’s also portions of the user base that do want to strictly enforce the width limits in their code and would leverage options like
error_on_line_overflowto ensure that any violations of the width constraints were indeed raised, and any changes to the behavior would have to continue to honor that (an implementation detail that I’m just noting for future reference).I also think that if max width were to be changed to more of a soft constraint then we’d need to explicitly convey that rustfmt will format your code within the limit, unless it really can’t, in which case it will format out indentations indefinitely as needed.
Doing something about this is still on my to-do list, though I do think it’s worth expanding a bit on what folks have in mind when we say “best effort”.
It seems this is most often encountered with a single long string, but will also note there are plenty of other scenarios that can run into this where IMO the expected behavior starts to get a little tricky. Consider for example a chain whose parent starts in a heavily indented position, and the chain element that exceeds the max width value is a closure param that itself has a sizeable body with additional nested indentation.
Would users still want rustfmt to format that closure anyway even though it blows way past the max width value? Are we changing the meaning/honoring of max width with a caveat of “except within chains”? Should rustfmt do this by default or should users have to explicitly opt-in to allowing rustfmt to exceed the max width when there are chains involved?