Skip to content

Commit 694a579

Browse files
committed
Auto merge of #12349 - epage:sanitize, r=weihanglo
fix(embedded): Always generate valid package names ### What does this PR try to resolve? The sanitization logic uses a placeholder for the first character that isn't valid in the first character position. #12329 took the approach of always using `_` which has the problem of mixing separators if the user used `-` or we had other placeholders to insert. Instead, this takes the approach of stripping the leading invalid characters and using a placeholder name if nothing is left. Fixes #12330 ### How should we test and review this PR? Per-commit. The first adds tests so the change in behavior can be observed over each additional commit. ### Additional information I was also hoping to make the binary name not use placeholders by setting `bin.name` to `file_stem` but then I got ``` Compiling s-h-w-c- v0.0.0 (/home/epage/src/personal/cargo/target/tmp/cit/t133/foo) error: invalid character `'.'` in crate name: `s_h.w§c!` error: invalid character `'§'` in crate name: `s_h.w§c!` error: invalid character `'!'` in crate name: `s_h.w§c!` error: could not compile `s-h-w-c-` (bin "s-h.w§c!") due to 3 previous errors ``` I decided to not get into what are or aren't valid characters according to rustc.
2 parents e64975c + 7d4e39c commit 694a579

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

src/cargo/util/restricted_names.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,22 @@ 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 {
104100
slug.push(placeholder);
105101
}
106102
}
103+
if slug.is_empty() {
104+
slug.push_str("package");
105+
}
107106
slug
108107
}
109108

tests/testsuite/script.rs

+46
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,52 @@ args: []
543543
.run();
544544
}
545545

546+
#[cargo_test]
547+
fn test_name_has_leading_number() {
548+
let script = ECHO_SCRIPT;
549+
let p = cargo_test_support::project()
550+
.file("42answer.rs", script)
551+
.build();
552+
553+
p.cargo("-Zscript -v 42answer.rs")
554+
.masquerade_as_nightly_cargo(&["script"])
555+
.with_stdout(
556+
r#"bin: [..]/debug/answer[EXE]
557+
args: []
558+
"#,
559+
)
560+
.with_stderr(
561+
r#"[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
562+
[COMPILING] answer v0.0.0 ([ROOT]/foo)
563+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
564+
[RUNNING] `[..]/debug/answer[EXE]`
565+
"#,
566+
)
567+
.run();
568+
}
569+
570+
#[cargo_test]
571+
fn test_name_is_number() {
572+
let script = ECHO_SCRIPT;
573+
let p = cargo_test_support::project().file("42.rs", script).build();
574+
575+
p.cargo("-Zscript -v 42.rs")
576+
.masquerade_as_nightly_cargo(&["script"])
577+
.with_stdout(
578+
r#"bin: [..]/debug/package[EXE]
579+
args: []
580+
"#,
581+
)
582+
.with_stderr(
583+
r#"[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
584+
[COMPILING] package v0.0.0 ([ROOT]/foo)
585+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
586+
[RUNNING] `[..]/debug/package[EXE]`
587+
"#,
588+
)
589+
.run();
590+
}
591+
546592
#[cargo_test]
547593
fn script_like_dir() {
548594
let p = cargo_test_support::project()

0 commit comments

Comments
 (0)