Skip to content

Commit

Permalink
fix(embedded): Extend sanitization rules to cover cargo-new validation
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jun 13, 2023
1 parent dd69350 commit 80c1ad5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions) -> CargoResult<&'a str> {
})
}

/// See also `util::toml::embedded::sanitize_name`
fn check_name(
name: &str,
show_name_help: bool,
Expand Down
35 changes: 27 additions & 8 deletions src/cargo/util/toml/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn write(
// more common convention for CLIs
'-'
};
let name = sanitize_package_name(file_name.as_ref(), separator);
let name = sanitize_name(file_name.as_ref(), separator);

let mut workspace_root = target_dir.to_owned();
workspace_root.push("eval");
Expand Down Expand Up @@ -154,7 +154,7 @@ fn expand_manifest_(script: &RawScript, config: &Config) -> CargoResult<toml::Ta
// more common convention for CLIs
'-'
};
let name = sanitize_package_name(file_name.as_ref(), separator);
let name = sanitize_name(file_name.as_ref(), separator);
let hash = hash(script);
let bin_name = format!("{name}{separator}{hash}");
package
Expand Down Expand Up @@ -207,8 +207,27 @@ fn expand_manifest_(script: &RawScript, config: &Config) -> CargoResult<toml::Ta
Ok(manifest)
}

fn sanitize_package_name(name: &str, placeholder: char) -> String {
restricted_names::sanitize_package_name(name, placeholder)
/// Ensure the package name matches the validation from `opt::cargo_new::check_name`
fn sanitize_name(name: &str, placeholder: char) -> String {
let mut name = restricted_names::sanitize_package_name(name, placeholder);

loop {
if restricted_names::is_keyword(&name) {
name.push(placeholder);
} else if restricted_names::is_conflicting_artifact_name(&name) {
// Being an embedded manifest, we always assume it is a `[[bin]]`
name.push(placeholder);
} else if name == "test" {
name.push(placeholder);
} else if restricted_names::is_windows_reserved(&name) {
// Go ahead and be consistent across platforms
name.push(placeholder);
} else {
break;
}
}

name
}

fn hash(script: &RawScript) -> blake3::Hash {
Expand Down Expand Up @@ -443,12 +462,12 @@ mod test_expand {
fn test_default() {
snapbox::assert_eq(
r#"[[bin]]
name = "test-a472c7a31645d310613df407eab80844346938a3b8fe4f392cae059cb181aa85"
name = "test--a472c7a31645d310613df407eab80844346938a3b8fe4f392cae059cb181aa85"
path = "/home/me/test.rs"
[package]
edition = "2021"
name = "test"
name = "test-"
publish = false
version = "0.0.0"
Expand All @@ -465,15 +484,15 @@ strip = true
fn test_dependencies() {
snapbox::assert_eq(
r#"[[bin]]
name = "test-3a1fa07700654ea2e893f70bb422efa7884eb1021ccacabc5466efe545da8a0b"
name = "test--3a1fa07700654ea2e893f70bb422efa7884eb1021ccacabc5466efe545da8a0b"
path = "/home/me/test.rs"
[dependencies]
time = "0.1.25"
[package]
edition = "2021"
name = "test"
name = "test-"
publish = false
version = "0.0.0"
Expand Down

0 comments on commit 80c1ad5

Please sign in to comment.