Closed
Description
Problem
When the file name starts with a digit, e.g. 0.rs
, cargo -Zscript 0.rs
defaults the package name to -0
and fails
Steps
echo 'fn main() {}' > 0.rs
cargo -Zscript 0.rs
warning: `package.edition` is unspecifiead, defaulting to `2021`
error: failed to parse manifest at `<...>/0.rs`
Caused by:
invalid character `-` in package name: `-0`, the first character must be a Unicode XID start character (most letters or `_`)
Possible Solution(s)
When the first character of the file name is invalid, insert _
instead of -
, which makes the package name valid. So for 0.rs
, the package name would default to _0
. This is #12329's approach
Notes
Related: #12207, #12329 (comment)
Version
cargo 1.72.0-nightly (5b377cece 2023-06-30)
release: 1.72.0-nightly
commit-hash: 5b377cece0e0dd0af686cf53ce4637d5d85c2a10
commit-date: 2023-06-30
host: x86_64-unknown-linux-gnu
libgit2: 1.6.4 (sys:0.17.2 vendored)
libcurl: 8.1.2-DEV (sys:0.4.63+curl-8.1.2 vendored ssl:OpenSSL/1.1.1u)
ssl: OpenSSL 1.1.1u 30 May 2023
os: NixOS 23.11.0 [64-bit]