Skip to content

Commit

Permalink
fix(embedded): Don't generate package names with invalid leading digit
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jul 11, 2023
1 parent 09d6c79 commit 87be661
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
12 changes: 4 additions & 8 deletions src/cargo/util/restricted_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,13 @@ pub fn validate_package_name(name: &str, what: &str, help: &str) -> CargoResult<
pub fn sanitize_package_name(name: &str, placeholder: char) -> String {
let mut slug = String::new();
let mut chars = name.chars();
if let Some(ch) = chars.next() {
if ch.is_digit(10) {
slug.push(placeholder);
slug.push(ch);
} else if unicode_xid::UnicodeXID::is_xid_start(ch) || ch == '_' {
while let Some(ch) = chars.next() {
if (unicode_xid::UnicodeXID::is_xid_start(ch) || ch == '_') && !ch.is_digit(10) {
slug.push(ch);
} else {
slug.push(placeholder);
break;
}
}
for ch in chars {
while let Some(ch) = chars.next() {
if unicode_xid::UnicodeXID::is_xid_continue(ch) || ch == '-' {
slug.push(ch);
} else {
Expand Down
15 changes: 9 additions & 6 deletions tests/testsuite/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,16 @@ fn test_name_has_leading_number() {

p.cargo("-Zscript -v 42answer.rs")
.masquerade_as_nightly_cargo(&["script"])
.with_status(101)
.with_stdout(
r#"bin: [..]/debug/answer[EXE]
args: []
"#,
)
.with_stderr(
r#"[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
[ERROR] failed to parse manifest at `[ROOT]/foo/42answer.rs`
Caused by:
invalid character `-` in package name: `-42answer`, the first character must be a Unicode XID start character (most letters or `_`)
[COMPILING] answer v0.0.0 ([ROOT]/foo)
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
[RUNNING] `[..]/debug/answer[EXE]`
"#,
)
.run();
Expand All @@ -577,7 +580,7 @@ fn test_name_is_number() {
[ERROR] failed to parse manifest at `[ROOT]/foo/42.rs`
Caused by:
invalid character `-` in package name: `-42`, the first character must be a Unicode XID start character (most letters or `_`)
package name cannot be an empty string
"#,
)
.run();
Expand Down

0 comments on commit 87be661

Please sign in to comment.