rust-lang / rust-lang/rust

Provide a more helpful diagnostic for shebangs inside of doctests

Open
#148,053 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-doctests T-compiler T-rustdoc
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
/// 
/// #!/usr/bin/env cargo
///
/// println!("hello");
/// 
pub struct Foo;
Current output
---- $DIR/failed-doctest-shebang.rs - Foo (line 6) stdout ----
error: macro expansion ignores `{` and any tokens following
  --> $SRC_DIR/std/src/macros.rs:LL:COL
   |
  ::: $DIR/failed-doctest-shebang.rs:9:1
   |
LL | println!("hello");
   | ----------------- caused by the macro expansion here
   |
   = note: the usage of `println!` is likely invalid in item context

error[E0601]: `main` function not found in crate `rust_out`
  --> $DIR/failed-doctest-shebang.rs:9:19
   |
LL | println!("hello");
   |                   ^ consider adding a `main` function to `$DIR/failed-doctest-shebang.rs`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0601`.
Couldn't compile the test.

failures:
    $DIR/failed-doctest-shebang.rs - Foo (line 6)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
Desired output
to include:

  = note: the token sequence `#!` here looks like the start of a shebang interpreter directive but it is not
  = help: if you meant this to be a shebang interpreter directive, move it to the very start of the file


See also #137619.
Rationale and extra context

See #137249

Other cases
Potential variants:
- With `main`
- With and without merging

Potentially frontmatter
Rust Version
rustc 1.92.0-nightly (4068bafed 2025-10-20)
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the doctest example from the issue, including the variants with main and with or without merging. Read the related issues #137619 and #137249 for context, then make the failure include the requested shebang note and help text; done means the diagnostic is helpful for the listed cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.