Skip to content

Bogus suggestion of #![crate_name = "…"] when trying to build binary like "udpinject.rs" #150023

@nabijaczleweli

Description

@nabijaczleweli

Code

$ cargo init udpinject.rs
    Creating binary (application) package
error: invalid character `.` in package name: `udpinject.rs`, characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)
If you need a package name to not match the directory name, consider using --name flag.
If you need a binary with the name "udpinject.rs", use a valid package name, and set the binary name to be different from the package. This can be done by setting the binary filename to `src/bin/udpinject.rs.rs` or change the name in Cargo.toml with:

    [[bin]]
    name = "udpinject.rs"
    path = "src/main.rs"

$ cargo init udpinject-rs
    Creating binary (application) package
$ cd udpinject-rs/
udpinject-rs$ subl Cargo.toml
udpinject-rs$ cat Cargo.toml
[package]
name = "udpinject-rs"
version = "0.1.0"
edition = "2024"

[[bin]]
name = "udpinject.rs"
path = "src/main.rs"

[dependencies]
udpinject-rs$ cargo build

Current output

Compiling udpinject-rs v0.1.0 (/home/nabijaczleweli/uwu/udpinject-rs)
error: invalid character '.' in crate name: `udpinject.rs`
  |
  = help: you can either pass `--crate-name` on the command line or add `#![crate_name = "…"]` to set the crate name

error: could not compile `udpinject-rs` (bin "udpinject.rs") due to 1 previous error
udpinject-rs$ subl src/main.rs
udpinject-rs$ head -n1 src/main.rs
#![crate_name="udpinject-rs"]
udpinject-rs$ cargo build
   Compiling udpinject-rs v0.1.0 (/home/nabijaczleweli/uwu/udpinject-rs)
error: `--crate-name` and `#[crate_name]` are required to match, but `udpinject.rs` != `udpinject-rs`
 --> src/main.rs:1:1
  |
1 | #![crate_name="udpinject-rs"]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: invalid character '.' in crate name: `udpinject.rs`
  |
  = help: you can either pass `--crate-name` on the command line or add `#![crate_name = "…"]` to set the crate name

error: could not compile `udpinject-rs` (bin "udpinject.rs") due to 2 previous errors

Desired output

Don't suggest it if you can't actually set it? idk

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-unknown-linux-gnu
release: 1.88.0
LLVM version: 20.1.5

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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