-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Fix #! (shebang) stripping account space issue #71372
Conversation
r? @estebank (rust_highfive has picked a reviewer for you, use r? to override) |
Also, it'd be great that if you could also include tests. |
@bors r+ rollup |
📌 Commit 1b362cd has been approved by |
Rollup of 7 pull requests Successful merges: - rust-lang#70998 (Suggest `-> impl Trait` and `-> Box<dyn Trait>` on fn that doesn't return) - rust-lang#71236 (Remove unused rustc_serialize::hex module) - rust-lang#71366 (Use assoc int consts3) - rust-lang#71372 (Fix #! (shebang) stripping account space issue) - rust-lang#71384 (Fix stage0.txt version number comment) - rust-lang#71390 (Fix incorrect description of E0690) - rust-lang#71399 (Clean up E0554 explanation) Failed merges: r? @ghost
return None; | ||
} | ||
Some(input.find('\n').unwrap_or(input.len())) | ||
} | ||
|
||
fn remove_whitespace(s: &str) -> String { | ||
s.chars().filter(|c| !c.is_whitespace()).collect() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use is_whitespace
from rustc_lexer
instead of the one from libstd
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It also doesn't seem reasonable to filter and re-collect all the program text to check for something that almost never happens, or requires checking a couple of characters when it does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like in this case
#!
[bad_attribute]
#!
is no longer treated as a shebang anymore, which also seems incorrect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think an empty shebang shouldn't be treated as a shebang, especially as it can be part of valid Rust syntax.
But I agree with everything else, and I wish this PR had been assigned to you...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just found #71487, will leave comments there.
#[test] | ||
fn test_valid_shebang() { | ||
// https://github.com/rust-lang/rust/issues/70528 | ||
let input = "#!/usr/bin/rustrun"; | ||
let actual = strip_shebang(input); | ||
let expected: Option<usize> = Some(18); | ||
assert_eq!(expected, actual); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_shebang_valid_rust_syntax() { | ||
// https://github.com/rust-lang/rust/issues/70528 | ||
let input = "#! [bad_attribute]"; | ||
let actual = strip_shebang(input); | ||
let expected: Option<usize> = None; | ||
assert_eq!(expected, actual); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be UI tests as well.
Revert rust-lang#71372 ("Fix #! (shebang) stripping account space issue"). While rust-lang#71372 fixed some of the problems `#!`-stripping had, it introduced others: * inefficient implementation (`.chars().filter(...).collect()` on the entire input file) * this also means the length returned isn't always correct, leading to e.g. rust-lang#71471 * it ignores whitespace anywhere, stripping ` # ! ...` which isn't a valid shebang * the definition of "whitespace" it uses includes newlines, which means even `\n#\n!\n...` is stripped as a shebang (and anything matching the regex `\s*#\s*!\s*`, and not followed by `[`, really) * it's backward-incompatible but didn't go through Crater Now, rust-lang#71487 is already open and will solve all of these issues. But for running Crater, and just in case rust-lang#71487 takes a bit longer, I decided it's safer to just revert rust-lang#71372. This will also make rust-lang#71372's diff clearer, as it will start again from the original whitespace-unaware version. r? @petrochenkov
Rollup of 5 pull requests Successful merges: - rust-lang#71311 (On `FnDef` type annotation suggestion, use fn-pointer output) - rust-lang#71488 (normalize field projection ty to fix broken MIR issue) - rust-lang#71489 (Fix off by one in treat err as bug) - rust-lang#71585 (remove obsolete comment) - rust-lang#71634 (Revert rust-lang#71372 ("Fix #! (shebang) stripping account space issue").) Failed merges: r? @ghost
Shebang handling was too agressive in stripping out the first line in cases where it is actually _not_ a shebang, but instead, valid rust (rust-lang#70528). This is a second attempt at resolving this issue (the first attempt was flawed, for, among other reasons, causing an ICE in certain cases (rust-lang#71372, rust-lang#71471). The behavior is now codified by a number of UI tests, but simply: For the first line to be a shebang, the following must all be true: 1. The line must start with `#!` 2. The line must contain a non whitespace character after `#!` 3. The next character in the file, ignoring comments & whitespace must not be `[` I believe this is a strict superset of what we used to allow, so perhaps a crater run is unnecessary, but probably not a terrible idea.
Fix bug in shebang handling Shebang handling was too agressive in stripping out the first line in cases where it is actually _not_ a shebang, but instead, valid rust (rust-lang#70528). This is a second attempt at resolving this issue (the first attempt was reverted, for, among other reasons, causing an ICE in certain cases (rust-lang#71372, rust-lang#71471). The behavior is now codified by a number of UI tests, but simply: For the first line to be a shebang, the following must all be true: 1. The line must start with `#!` 2. The line must contain a non-whitespace character after `#!` 3. The next character in the file, ignoring comments & whitespace must not be `[` I believe this is a strict superset of what we used to allow, so perhaps a crater run is unnecessary, but probably not a terrible idea. Fixes rust-lang#70528
#70528