Skip to content

Commit 570c5df

Browse files
committed
fix(embedded): Extend sanitization rules to cover cargo-add validation
1 parent 3e540b2 commit 570c5df

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/cargo/ops/cargo_new.rs

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions) -> CargoResult<&'a str> {
163163
})
164164
}
165165

166+
/// See also `util::toml::embedded::sanitize_name`
166167
fn check_name(
167168
name: &str,
168169
show_name_help: bool,

src/cargo/util/toml/embedded.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl RawScript {
143143
// more common convention for CLIs
144144
'-'
145145
};
146-
let name = restricted_names::sanitize_package_name(&file_name, separator);
146+
let name = sanitize_name(&file_name, separator);
147147
let hash = self.hash();
148148
let bin_name = format!("{name}{separator}{hash}");
149149
package
@@ -200,6 +200,29 @@ impl RawScript {
200200
}
201201
}
202202

203+
/// Ensure the package name matches the validation from `opt::cargo_new::check_name`
204+
fn sanitize_name(name: &str, placeholder: char) -> String {
205+
let mut name = restricted_names::sanitize_package_name(name, placeholder);
206+
207+
loop {
208+
if restricted_names::is_keyword(&name) {
209+
name.push(placeholder);
210+
} else if restricted_names::is_conflicting_artifact_name(&name) {
211+
// Being an embedded manifest, we always assume it is a `[[bin]]`
212+
name.push(placeholder);
213+
} else if name == "test" {
214+
name.push(placeholder);
215+
} else if restricted_names::is_windows_reserved(&name) {
216+
// Go ahead and be consistent across platforms
217+
name.push(placeholder);
218+
} else {
219+
break;
220+
}
221+
}
222+
223+
name
224+
}
225+
203226
fn default_target_dir() -> CargoResult<std::path::PathBuf> {
204227
let mut cargo_home = home::cargo_home()?;
205228
cargo_home.push("eval");
@@ -428,12 +451,12 @@ mod test_expand {
428451
fn test_default() {
429452
snapbox::assert_eq(
430453
r#"[[bin]]
431-
name = "test-a472c7a31645d310613df407eab80844346938a3b8fe4f392cae059cb181aa85"
454+
name = "test--a472c7a31645d310613df407eab80844346938a3b8fe4f392cae059cb181aa85"
432455
path = "/home/me/test.rs"
433456
434457
[package]
435458
edition = "2021"
436-
name = "test"
459+
name = "test-"
437460
publish = false
438461
version = "0.0.0"
439462
@@ -450,15 +473,15 @@ strip = true
450473
fn test_dependencies() {
451474
snapbox::assert_eq(
452475
r#"[[bin]]
453-
name = "test-3a1fa07700654ea2e893f70bb422efa7884eb1021ccacabc5466efe545da8a0b"
476+
name = "test--3a1fa07700654ea2e893f70bb422efa7884eb1021ccacabc5466efe545da8a0b"
454477
path = "/home/me/test.rs"
455478
456479
[dependencies]
457480
time = "0.1.25"
458481
459482
[package]
460483
edition = "2021"
461-
name = "test"
484+
name = "test-"
462485
publish = false
463486
version = "0.0.0"
464487

0 commit comments

Comments
 (0)