Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustfmt fails to format long strings in a struct #4800

Closed
xscd opened this issue Apr 14, 2021 · 11 comments
Closed

rustfmt fails to format long strings in a struct #4800

xscd opened this issue Apr 14, 2021 · 11 comments
Labels
1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release help wanted

Comments

@xscd
Copy link

xscd commented Apr 14, 2021

Describe the bug

rustfmt fails to format long strings in variable declarations.

To Reproduce

  1. Go to the playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=934f95c1ee7c244bd171e2f8b211fb54

  2. From Tools section run rustfmt

  3. See that when the strings are short there are no issues but if they are long they are not formatted.

Expected behavior

I expected that rustfmt can format long strings too.

Meta

  • rustfmt version: rustfmt 1.4.36-stable (7de6968 2021-02-07)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: directly from commandline "rustfmt" and/or inside vscode
@xscd xscd added the bug Panic, non-idempotency, invalid code, etc. label Apr 14, 2021
@calebcartwright calebcartwright added 1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release help wanted and removed bug Panic, non-idempotency, invalid code, etc. labels Apr 16, 2021
@calebcartwright
Copy link
Member

Thanks for reaching out. This is fixed in source (apparently) but hasn't been pulled into a release branch.

For anyone interested in working on this please note that the fix would need to be versioned gated to avoid violating the formatting stability guarantee. It was an intentional decision early on in rustfmt to bail in cases like this when it's completely impossible to format an element within the max_width constraint, so even though it's preferable now to format anyway (and let users turn on config options like error_on_line_overflow) we still have to gate that change

@joakin
Copy link

joakin commented Jun 9, 2021

I've hit this too when writing unit tests with structs that have fields with longs strings. I was going to make a new issue but I found this one thankfully.

Here is my example:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=c1da9e431970417426af1bd25ab458c3

And here is a workaround I found by using a block and declaring the string inside.

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=22e7e3c4eab0a90046dcf1148e59bb8a

This way at least the rest of the file seems to keep formatting.

@joakin
Copy link

joakin commented Jun 9, 2021

For another piece of code where I was having issues with another long string, I ended up splitting it with \ and that also made rustfmt happier and formatted the body of my function. Different tricks for different parts of the code I guess. Very disruptive :(

@ValHeimer
Copy link

ValHeimer commented Jul 30, 2021

same as #4797

I can't format the first for_each() , string too long (no problem with the second one)

here it seems to happen if the string in the println!("") is longer than 85 characters (remove 1 letter in my example and it will indent the code correctly)

fn main() {
    println!("Hello, world!");

    vec![""].into_iter().for_each(|_| {
            println!("indent ind qqsde indent me indent me indent me indent me indent me indent me indent me ");
        println!("2");
    });

    vec![""].into_iter().for_each(|_| {
        println!("indent me");
        println!("2");
    });
}

i'm using Rust 1.54.0

@calebcartwright
Copy link
Member

@ValHeimer - that's a duplicate of #3863 which is separate from this issue

@benma
Copy link

benma commented Oct 2, 2021

Any update on this? My string is around 100 chars long and rustfmt does not format it nor the surrounding code (other fields in the struct besides the one with the long string).

@tkkcc
Copy link

tkkcc commented Sep 2, 2022

So currently the only way is to manually split long string?

@ytmimi
Copy link
Contributor

ytmimi commented Sep 2, 2022

So currently the only way is to manually split long string?

@tkkcc If you're using a nightly rustfmt you can set format_strings=true to allow rustfmt to break long strings for you.

@nsunderland1
Copy link

@Fabus1184
Copy link

Still no fix? This is a major issue, especially when generating Rust code with TokenStreams which need to be formatted, but this causes otherwise rather large sections to remain in one line.

let request = client . request (reqwest :: Method :: GET , reqwest :: Url :: parse_with_params (format ! ("https://apis.website.com/xy-api-abc/apis/api-stuff-url-things/v2/more-things/{stuff}") . as_str () , & [("" , "")] ,) . unwrap () ,) . headers (headers) ;

Obviously this is completely unreadable

@calebcartwright calebcartwright changed the title rustfmt fails to format long strings rustfmt fails to format long strings in a struct Sep 20, 2023
@calebcartwright
Copy link
Member

This is fixed but gated behind the version=Two flag as required per the stability guarantee.

As noted above the other comments relative to chains are unrelated to this issue and are duplicates of #3863

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release help wanted
Projects
None yet
Development

No branches or pull requests

9 participants