Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't Scaffold A New Project With Name Beginning With Number? #8613

Closed
JimLynchCodes opened this issue Aug 11, 2020 · 6 comments · Fixed by #8675
Closed

Can't Scaffold A New Project With Name Beginning With Number? #8613

JimLynchCodes opened this issue Aug 11, 2020 · 6 comments · Fixed by #8675
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. C-bug Category: bug

Comments

@JimLynchCodes
Copy link

Problem
Problem is that cargo gives an error "error: Package names starting with a digit cannot be used as a crate name" rather than creating a project with name beginning with number.

Steps

run this:

cargo new 1-hello-world

Possible Solution(s)

Notes

There is the additional output in the console, "use --name to override crate name", but I'm not really sure how to change my command to make it work. 🤔

Output of cargo version: cargo 1.42.0 (8633429 2020-01-31)

@JimLynchCodes JimLynchCodes added the C-bug Category: bug label Aug 11, 2020
@hbina
Copy link
Contributor

hbina commented Aug 11, 2020

I am not sure I understand this issue. cargo simply does not allow crates to have names starting with a digit. This is a reasonable restriction imo.

In particular, here's the exact code that checks against this

pub fn validate_package_name(name: &str, what: &str, help: &str) -> CargoResult<()> {
let mut chars = name.chars();
if let Some(ch) = chars.next() {
if ch.is_digit(10) {
// A specific error for a potentially common case.
bail!(
"the name `{}` cannot be used as a {}, \
the name cannot start with a digit{}",
name,
what,
help
);
}

Or did I misunderstood you?

@JimLynchCodes
Copy link
Author

JimLynchCodes commented Aug 18, 2020

@hbina "This is a reasonable restriction imo."

To me is seems like a completely unnecessary restriction. Folder names beginning with numbers are perfectly valid, and in this case I do want to create a folder name that begins with a number (mostly because these folders represent chapters, and I want them to be ordered automatically by the file system).

Can we remove this digit check altogether? I don't see what purpose or benefit it serves.

@Eh2406
Copy link
Contributor

Eh2406 commented Aug 18, 2020

This works if you want the folder to start with a number:
cargo new 1-hello-world --name hello-world
or
cargo new hello-world && mv hello-world 1-hello-world
The fact that the name field in Cargo.toml can not start with a digit is connected to the fact that in Rust identifiers can not start with a digit. That would be hard to change.

Now, I'd be excited for a PR that improved the error message to something like:

> cargo new 1-hello-world
error: Package names starting with a digit cannot be used as a crate name
did you mean "cargo new 1-hello-world --name hello-world"?

I don't know how hard that would be.

@ehuss ehuss added the A-diagnostics Area: Error and warning messages generated by Cargo itself. label Aug 19, 2020
@JimLynchCodes
Copy link
Author

Thanks for the explanation @Eh2406 🙏

I agree that your proposed error message would be a nicer UX.

@lofiguy
Copy link

lofiguy commented Jun 26, 2022

I know that it's been a long time @JimLynchCodes. However maybe someone encounter this problem again so the solution is:

$ cargo new hello-world

And you can simply change the directory's name:

$ mv hello-world/ 1-hello-world/

And after that operation if you go to project dir:

$ cd 1-hello-world/

You can simply run the project without any problems:

┌─(~/dev/rust//0-hello-world)
└─(12:01:55 on master ✭)──> cargo run                                                                                                                                                                                                                                  ──(Sun,Jun26)─┘
   Compiling hello-world v0.1.0 (/home/ax/dev/rust/book-1.58/by-example/0-hello-world)
    Finished dev [unoptimized + debuginfo] target(s) in 0.13s
     Running `target/debug/hello-world
Hello, world!

Important:

You shouldn't change the name field in Cargo.toml for 1-hello-world. The file should looks like this:

[package]
name = "hello-world"
version = "0.1.0"
edition = "2021"

@Anynomouss
Copy link

@lofiguy Is your answer not unnecessary long and complex?
The simple answer is that you can specify a project folder with a number, but not a crate name with a number using the cargo --name option:

cargo new 1-hello-world --name hello-world

The above code will create a folder 1-hello-world with a crate with the name hello-world

The code in the directory 1-hello-world can be run like any project using cargo run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants