Skip to content

Commit 87be661

Browse files
committed
fix(embedded): Don't generate package names with invalid leading digit
1 parent 09d6c79 commit 87be661

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/cargo/util/restricted_names.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,13 @@ pub fn validate_package_name(name: &str, what: &str, help: &str) -> CargoResult<
8787
pub fn sanitize_package_name(name: &str, placeholder: char) -> String {
8888
let mut slug = String::new();
8989
let mut chars = name.chars();
90-
if let Some(ch) = chars.next() {
91-
if ch.is_digit(10) {
92-
slug.push(placeholder);
93-
slug.push(ch);
94-
} else if unicode_xid::UnicodeXID::is_xid_start(ch) || ch == '_' {
90+
while let Some(ch) = chars.next() {
91+
if (unicode_xid::UnicodeXID::is_xid_start(ch) || ch == '_') && !ch.is_digit(10) {
9592
slug.push(ch);
96-
} else {
97-
slug.push(placeholder);
93+
break;
9894
}
9995
}
100-
for ch in chars {
96+
while let Some(ch) = chars.next() {
10197
if unicode_xid::UnicodeXID::is_xid_continue(ch) || ch == '-' {
10298
slug.push(ch);
10399
} else {

tests/testsuite/script.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -552,13 +552,16 @@ fn test_name_has_leading_number() {
552552

553553
p.cargo("-Zscript -v 42answer.rs")
554554
.masquerade_as_nightly_cargo(&["script"])
555-
.with_status(101)
555+
.with_stdout(
556+
r#"bin: [..]/debug/answer[EXE]
557+
args: []
558+
"#,
559+
)
556560
.with_stderr(
557561
r#"[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
558-
[ERROR] failed to parse manifest at `[ROOT]/foo/42answer.rs`
559-
560-
Caused by:
561-
invalid character `-` in package name: `-42answer`, the first character must be a Unicode XID start character (most letters or `_`)
562+
[COMPILING] answer v0.0.0 ([ROOT]/foo)
563+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
564+
[RUNNING] `[..]/debug/answer[EXE]`
562565
"#,
563566
)
564567
.run();
@@ -577,7 +580,7 @@ fn test_name_is_number() {
577580
[ERROR] failed to parse manifest at `[ROOT]/foo/42.rs`
578581
579582
Caused by:
580-
invalid character `-` in package name: `-42`, the first character must be a Unicode XID start character (most letters or `_`)
583+
package name cannot be an empty string
581584
"#,
582585
)
583586
.run();

0 commit comments

Comments
 (0)