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

error[internal]: left behind trailing whitespace on long lines in raw string #3968

Closed
lucidBrot opened this issue Dec 17, 2019 · 15 comments
Closed

Comments

@lucidBrot
Copy link

System

$ rustfmt --version
rustfmt 1.3.0-stable (d3345024 2019-06-09)

eric@Friendly_Name /cygdrive/c/Users/eric/Downloads/rustfmt_bug_lucidbrot/ex
$ cargo --version
cargo 1.37.0 (9edd08916 2019-08-02)

eric@Friendly_Name /cygdrive/c/Users/eric/Downloads/rustfmt_bug_lucidbrot/ex
$ uname -a
CYGWIN_NT-10.0 Friendly_Name 3.0.7(0.338/5/3) 2019-04-30 18:08 x86_64 Cygwin

Reproduction

rustfmt fails on this code sample.

const CONFIG_STRING: &str = 
    r###"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
"###;
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

The error it prints is this:

eric@Friendly_Name /cygdrive/c/Users/eric/Downloads/rustfmt_bug_lucidbrot/ex
$ cargo fmt
error[internal]: left behind trailing whitespace
 --> \\?\C:\Users\eric\Downloads\rustfmt_bug_lucidbrot\ex\src\main.rs:1:1:27
  |
1 | const CONFIG_STRING: &str =
  |                            ^
  |

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

This code sample, which has one a less, is successfully formatted:

const CONFIG_STRING: &str = 
    r###"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
"###;
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

The successfully formatted version looks like this:

const CONFIG_STRING: &str =
    r###"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
"###;
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

Another version of this snippet that can successfully be formatted is the following. It has the same number of as as the problematic example, but no trailing space after the equal sign on line 1:

const CONFIG_STRING: &str =
    r###"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
"###;
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

Possibly Related

I don't know enough to say for sure. That's why I created a new issue. But these may or may not be related:
#2896 (open)
#3295 (nightly) (closed)
#2896 (open) (collection thread)

More Examples

Good (Can be formatted):

const CONFIG_STRING: &str =     r###"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
"###;
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

Bad (Can not successfully be formatted):

const CONFIG_STRING: &str = 
r#"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"#;
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

Good:

const CONFIG_STRING: &str = 
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

Bad:
Has no whitespace after the equal sign, but a long raw string

const CONFIG_STRING: &str = 
r#"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"#;
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

Good:
Just like the sample right above this one, except it has one a less:

const CONFIG_STRING: &str = 
r#"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"#;
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

Bad:
no longer a raw string

const CONFIG_STRING: &str = 
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

Splitting into multiple lines makes the bug disappear.
Good:

const CONFIG_STRING: &str = "aaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaa";
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

Conclusion

It seems like this bug has to do with all of these in some way:

  • the space after the equal sign.
  • the difference between 90 as vs 89 as (I think the counts are correct?) - or rather, the length of the line as a whole.

When there is no whitespace after the equal sign, the number of as is irrelevant and formatting works.
When there are few enough as, the formatting works even with the whitespace.

@calebcartwright
Copy link
Member

hi @lucidBrot - this error is not reproducible in the latest versions of rustfmt, so it was resolved some time back

@lucidBrot
Copy link
Author

lucidBrot commented Dec 17, 2019

Hi @calebcartwright
Thank you for your quick response. How do I update? It happens also with nightly cargo:

Eric@Sharkoon /cygdrive/n/Temp/rustbuga
$ cargo +nightly fmt
error[internal]: left behind trailing whitespace
 --> \\?\N:\Temp\rustbuga\src\main.rs:1:1:27
  |
1 | const CONFIG_STRING: &str =
  |                            ^
  |

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


Eric@Sharkoon /cygdrive/n/Temp/rustbuga
$ cargo +nightly --version
cargo 1.41.0-nightly (750cb1482 2019-11-23)

and on both of my devices. It also does not seem to be a cygwin-related bug, as it also happens in windows cmd

@calebcartwright
Copy link
Member

Easiest way would be to use rustup to upgrade to the latest stable version of rust and friends

rustup update stable (and/or nightly chanel if you prefer)

which will update you to rustfmt 1.4.8 or newer IIRC

It also does not seem to be a cygwin-related bug, as it also happens in windows cmd

Yeah these failed to format whitespace errors are internal rustfmt programming bugs that pop up from time to time

@lucidBrot
Copy link
Author

I have run rustup update and am now on rustfmt version 1.4.8-stable. I pasted the last "Bad" code snippet from above to be sure it's not a copy-paste problem. The issue is still reproducible for me :/

N:\Temp\rustbuga>rustfmt --version
rustfmt 1.4.8-stable (afb1ee1c 2019-09-08)

N:\Temp\rustbuga>cargo fmt
error[internal]: left behind trailing whitespace
 --> \\?\N:\Temp\rustbuga\src\main.rs:1:1:27
  |
1 | const CONFIG_STRING: &str =
  |                            ^
  |

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


N:\Temp\rustbuga>cat src\main.rs
const CONFIG_STRING: &str =
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

N:\Temp\rustbuga>cargo --version
cargo 1.39.0 (1c6ec66d5 2019-09-30)

@calebcartwright
Copy link
Member

Thanks! Seems like this was one of the more recent fixes then since it's working for me on 1.4.11.

What about cargo +nightly fmt?

@lucidBrot
Copy link
Author

Same thing:

N:\Temp\rustbuga>cargo +nightly fmt
error[internal]: left behind trailing whitespace
 --> \\?\N:\Temp\rustbuga\src\main.rs:1:1:27
  |
1 | const CONFIG_STRING: &str =
  |                            ^
  |

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


N:\Temp\rustbuga>cargo +nightly --version
cargo 1.41.0-nightly (626f0f40e 2019-12-03)

N:\Temp\rustbuga>rustfmt --version
rustfmt 1.4.8-stable (afb1ee1c 2019-09-08)

N:\Temp\rustbuga>rustfmt +nightly --version
rustfmt 1.4.11-nightly (18382352 2019-12-03)

@calebcartwright
Copy link
Member

👀 yikes.

I'll take a closer look into it later today. I'm using the latest version of rustfmt off the master branch in my env where I cannot reproduce, so either there's something else going on, or perhaps a very recent fix has resolved this one already 🤞

We're primarily focused on rustfmt 2.x right now so there's quite a few changes on the master branch that weren't in rustfmt 1.4.11 (which was released relatively recently with an urgent bug fix) so I'm hopeful that's what's happening

@lucidBrot
Copy link
Author

Thanks a lot!

It's not urgent for me personally, since I can fix my use case simply by removing the trailing space manually. But I'm happy I did not end up being the stupid dude who did not update before reporting a bug (although I did not update at first 😅).

@calebcartwright
Copy link
Member

This gets even stranger 🤔 I still can't reproduce this on Ubuntu but I can on my Windows machine

@lucidBrot
Copy link
Author

lucidBrot commented Dec 18, 2019

I'll play around with CR/LF line endings if I get to it this evening.

Edit: vim src/main.rs -c "set ff=dos" -c ":wq" does not affect cargo fmt

@topecongiro
Copy link
Contributor

I cannot reproduce this as of rustfmt 1.4.10-nightly (e3bb1c1 2019-12-16) on both my Linux and Windows machines. As such, I will be closing this PR. Please feel free to reopen this if you find any problem. Thank you for filing an issue!

@lucidBrot
Copy link
Author

lucidBrot commented Dec 20, 2019

I updated again using rustup and am now on rustfmt 1.4.11-nightly.
It is still reproducible on one of my windows machines. I have not tried on the other one.


N:\Temp\rustbuga>cat src/main.rs
const CONFIG_STRING: &str =
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

N:\Temp\rustbuga>cargo +nightly fmt
error[internal]: left behind trailing whitespace
 --> \\?\N:\Temp\rustbuga\src\main.rs:1:1:27
  |
1 | const CONFIG_STRING: &str =
  |                            ^
  |

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


N:\Temp\rustbuga>cargo +nightly fmt --version
rustfmt 1.4.11-nightly (18382352 2019-12-03)

N:\Temp\rustbuga>cargo +nightly --version
cargo 1.41.0-nightly (626f0f40e 2019-12-03)

To be clear, I am currently using the last "bad" of my examples. Which is this:

const CONFIG_STRING: &str = 
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
fn main() {
    println!("Hello, world: {}", CONFIG_STRING);
}

@calebcartwright
Copy link
Member

calebcartwright commented Dec 21, 2019

@lucidBrot - one admittedly confusing thing here is that the released 1.4.11 version of rustfmt that you are getting from rustup is a hotfix version of rustfmt that's actually older than the current rustfmt on master branch (master is targeting a planned 2.0 release of rustfmt, but the version in Cargo.toml is still at 1.4.10 at the moment)

After re-checking that I had the latest rustfmt version (on master) on my Windows machine, I can't reproduce this one either (Seems I was using a slightly older rustfmt version on Windows before when I was able to reproduce).

As such I believe this bug has already been fixed in the most recent version of rustfmt on master, but that fix hasn't been released yet.

If you wanted to test the latest version of rustfmt on your machine against this snippet, you could try installing rustfmt directly from source. To do so, clone this repo and then run the below command in the root directory:

cargo +nightly install --path . --force --locked

@lucidBrot
Copy link
Author

Thanks for the clear instructions!
I have tried this with rustfmt 1.4.10-nightly (1e04b5e 2019-12-20) and the bug is indeed fixed.

What surprised me a bit is that cargo +nightly install --path . --locked --force modified not only the version of cargo +nightly fmt --version but also cargo fmt --version:

Eric@Sharkoon /cygdrive/n/Temp/rustbuga
$ cargo fmt --version
rustfmt 1.4.10-nightly (1e04b5ee 2019-12-20)

Eric@Sharkoon /cygdrive/n/Temp/rustbuga
$ cargo +nightly fmt --version
rustfmt 1.4.10-nightly (1e04b5ee 2019-12-20)

If you happen to know whether that is intentional, let me know. Otherwise I won't bother with it.

Thank you two for your time and help!

@calebcartwright
Copy link
Member

If you happen to know whether that is intentional, let me know. Otherwise I won't bother with it.

Assuming you'd prefer to switch back to the bundled/released version of rustfmt, the below commands should get you reset

cargo +nightly uninstall
rustup update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants