Commit 321262b
authored
Validate that package name starts with an alphabetic character (#480)
# Objective
In #450 we had the issue that we created an empty directory with an
error message like
```
Runtime error: System command `cargo update --package 2d-game` failed to execute: error: invalid character `2` in package name: `2d-game`, the name cannot start with a digit
--> Cargo.toml:2:8
|
2 | name = "2d-game"
| ^^^^^^^^^
|
```
This is no longer the case since now the template is created correctly,
but we should still try to remove easy pitfalls like packages that start
with a non-alphabetic character.
# Solution
The simplest (but not exhaustive) solution is to just check if the
starting character is alphabetic. This does not prevent package names
like `a2%d-game` (they get created correctly):
```sh
a2%d-game on main is 📦 v0.1.0 via 🦀 v1.86.0
❯ ls
assets Cargo.lock Cargo.toml clippy.toml README.md src
```
and the `cargo` error message is very clear:
```sh
❯ cargo run
error: invalid character `%` in package name: `a2%d-game`, characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)
--> Cargo.toml:2:8
|
2 | name = "a2%d-game"
| ^^^^^^^^^^^
|
```
# Alternatives
We could also use `https://github.com/dtolnay/unicode-ident`, but it
feels a bit much for a very niche use case that has a very good error
message from `cargo`. In my opinion, as long as `cargo generate` can
create the template correctly, we are good.
Closes #4501 parent e61be1e commit 321262b
1 file changed
+8
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
25 | 33 | | |
26 | 34 | | |
27 | 35 | | |
| |||
0 commit comments