$ 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
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);
}
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:
https://github.com/rust-lang/rustfmt/issues/2896 (open)
https://github.com/rust-lang/rustfmt/issues/3295 (nightly) (closed)
https://github.com/rust-lang/rustfmt/issues/2896 (open) (collection thread)
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);
}
It seems like this bug has to do with all of these in some way:
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.
hi @lucidBrot - this error is not reproducible in the latest versions of rustfmt, so it was resolved some time back
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
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
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)
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?
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)
馃憖 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
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 馃槄).
This gets even stranger 馃 I still can't reproduce this on Ubuntu but I _can_ on my Windows machine
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
I cannot reproduce this as of rustfmt 1.4.10-nightly (e3bb1c19 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!
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);
}
@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
Thanks for the clear instructions!
I have tried this with rustfmt 1.4.10-nightly (1e04b5ee 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!
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
Most helpful comment
@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
versioninCargo.tomlis 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: