Skip to content

cargo -Zscript uses invalid package names when the file name starts with a digit #12330

Closed
@figsoda

Description

@figsoda

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bugS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.S-triageStatus: This issue is waiting on initial triage.Z-scriptNightly: cargo script

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions