Skip to content

#! (shebang) stripping doesn't account for spaces. #70528

Closed
@eddyb

Description

@eddyb

A file containing only this correctly errors:

#![bad_attribute]

But this doesn't error, as it's stripped the same way #!/usr/bin/env ... (i.e. "shebang") would be:

#! [bad_attribute]

In this third example, it also strips just the #!, breaking the attribute:

#!
[bad_attribute]

The code responsible is this:

/// Line won't be skipped if it represents a valid Rust syntax
/// (e.g. "#![deny(missing_docs)]").
pub fn strip_shebang(input: &str) -> Option<usize> {
debug_assert!(!input.is_empty());
if !input.starts_with("#!") || input.starts_with("#![") {

It doesn't seem to account for any whitespace between the ! and [.
I believe that we should allow any characters c where c != '\n' && is_whitespace(c) after the #!, and determine whether this is a shebang, by the next character on the same line:

  • if there are no non-whitespace characters, it's not a valid shebang
  • if the next character is [, this is the start of an inner comment
  • otherwise, it's probably a shebang

I have no idea what to do for Rust comment syntax (#!//... and #!/*...), however.

And, of course, at the end of the day, this might be a backwards-incompatible change to make.

cc @matklad @petrochenkov

This issue has been assigned to @rcoh via this comment.

Metadata

Metadata

Assignees

Labels

A-parserArea: The lexing & parsing of Rust source code to an ASTC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions