cargo: prevent dashes in lib.name

The TOML parser of Cargo currently refuses `lib.name` entries that
contain dashes. Unfortunately, it uses the package-name as default if no
explicit `lib.name` entry is specified. This package-name, however, can
contain dashes.

Cargo documentation states that the package name is converted first, yet
this was never implemented by the code-base.

Fix this inconsistency and convert the package name to a suitable
crate-name first.
This commit is contained in:
David Rheinsberg 2023-10-06 14:59:58 +02:00
parent 8b2cc62d37
commit 3ca04e261e
6 changed files with 23 additions and 19 deletions

View File

@ -154,18 +154,20 @@ fn clean_lib(
let lib = match toml_lib { let lib = match toml_lib {
Some(lib) => { Some(lib) => {
if let Some(ref name) = lib.name { if let Some(ref name) = lib.name {
// XXX: other code paths dodge this validation
if name.contains('-') { if name.contains('-') {
anyhow::bail!("library target names cannot contain hyphens: {}", name) anyhow::bail!("library target names cannot contain hyphens: {}", name)
} }
} }
Some(TomlTarget { Some(TomlTarget {
name: lib.name.clone().or_else(|| Some(package_name.to_owned())), name: lib
.name
.clone()
.or_else(|| Some(package_name.replace("-", "_"))),
..lib.clone() ..lib.clone()
}) })
} }
None => inferred.as_ref().map(|lib| TomlTarget { None => inferred.as_ref().map(|lib| TomlTarget {
name: Some(package_name.to_string()), name: Some(package_name.replace("-", "_")),
path: Some(PathValue(lib.clone())), path: Some(PathValue(lib.clone())),
..TomlTarget::new() ..TomlTarget::new()
}), }),

View File

@ -817,8 +817,10 @@ fn lib_with_selected_dashed_bin_artifact_and_lib_true() {
let _b = include_bytes!(env!("CARGO_BIN_FILE_BAR_BAZ_baz-suffix")); let _b = include_bytes!(env!("CARGO_BIN_FILE_BAR_BAZ_baz-suffix"));
let _b = include_bytes!(env!("CARGO_STATICLIB_FILE_BAR_BAZ")); let _b = include_bytes!(env!("CARGO_STATICLIB_FILE_BAR_BAZ"));
let _b = include_bytes!(env!("CARGO_STATICLIB_FILE_BAR_BAZ_bar-baz")); let _b = include_bytes!(env!("CARGO_STATICLIB_FILE_BAR_BAZ_bar-baz"));
let _b = include_bytes!(env!("CARGO_STATICLIB_FILE_BAR_BAZ_bar_baz"));
let _b = include_bytes!(env!("CARGO_CDYLIB_FILE_BAR_BAZ")); let _b = include_bytes!(env!("CARGO_CDYLIB_FILE_BAR_BAZ"));
let _b = include_bytes!(env!("CARGO_CDYLIB_FILE_BAR_BAZ_bar-baz")); let _b = include_bytes!(env!("CARGO_CDYLIB_FILE_BAR_BAZ_bar-baz"));
let _b = include_bytes!(env!("CARGO_CDYLIB_FILE_BAR_BAZ_bar_baz"));
} }
"#, "#,
) )

View File

@ -534,7 +534,7 @@ fn collision_with_root() {
[DOWNLOADING] crates ... [DOWNLOADING] crates ...
[DOWNLOADED] foo-macro v1.0.0 [..] [DOWNLOADED] foo-macro v1.0.0 [..]
warning: output filename collision. warning: output filename collision.
The lib target `foo-macro` in package `foo-macro v1.0.0` has the same output filename as the lib target `foo-macro` in package `foo-macro v1.0.0 [..]`. The lib target `foo_macro` in package `foo-macro v1.0.0` has the same output filename as the lib target `foo_macro` in package `foo-macro v1.0.0 [..]`.
Colliding filename is: [CWD]/target/doc/foo_macro/index.html Colliding filename is: [CWD]/target/doc/foo_macro/index.html
The targets should have unique names. The targets should have unique names.
This is a known bug where multiple crates with the same name use This is a known bug where multiple crates with the same name use

View File

@ -2089,7 +2089,7 @@ fn doc_test_in_workspace() {
) )
.build(); .build();
p.cargo("test --doc -vv") p.cargo("test --doc -vv")
.with_stderr_contains("[DOCTEST] crate-a") .with_stderr_contains("[DOCTEST] crate_a")
.with_stdout_contains( .with_stdout_contains(
" "
running 1 test running 1 test
@ -2098,7 +2098,7 @@ test crate-a/src/lib.rs - (line 1) ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..] test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
", ",
) )
.with_stderr_contains("[DOCTEST] crate-b") .with_stderr_contains("[DOCTEST] crate_b")
.with_stdout_contains( .with_stdout_contains(
" "
running 1 test running 1 test

View File

@ -1616,7 +1616,7 @@ fn workspace_metadata_with_dependencies_and_resolve() {
"kind": [ "kind": [
"lib" "lib"
], ],
"name": "non-artifact", "name": "non_artifact",
"src_path": "[..]/foo/non-artifact/src/lib.rs", "src_path": "[..]/foo/non-artifact/src/lib.rs",
"test": true "test": true
} }
@ -3110,7 +3110,7 @@ fn filter_platform() {
"crate_types": [ "crate_types": [
"lib" "lib"
], ],
"name": "alt-dep", "name": "alt_dep",
"src_path": "[..]/alt-dep-0.0.1/src/lib.rs", "src_path": "[..]/alt-dep-0.0.1/src/lib.rs",
"edition": "2015", "edition": "2015",
"test": true, "test": true,
@ -3154,7 +3154,7 @@ fn filter_platform() {
"crate_types": [ "crate_types": [
"lib" "lib"
], ],
"name": "cfg-dep", "name": "cfg_dep",
"src_path": "[..]/cfg-dep-0.0.1/src/lib.rs", "src_path": "[..]/cfg-dep-0.0.1/src/lib.rs",
"edition": "2015", "edition": "2015",
"test": true, "test": true,
@ -3198,7 +3198,7 @@ fn filter_platform() {
"crate_types": [ "crate_types": [
"lib" "lib"
], ],
"name": "host-dep", "name": "host_dep",
"src_path": "[..]/host-dep-0.0.1/src/lib.rs", "src_path": "[..]/host-dep-0.0.1/src/lib.rs",
"edition": "2015", "edition": "2015",
"test": true, "test": true,
@ -3242,7 +3242,7 @@ fn filter_platform() {
"crate_types": [ "crate_types": [
"lib" "lib"
], ],
"name": "normal-dep", "name": "normal_dep",
"src_path": "[..]/normal-dep-0.0.1/src/lib.rs", "src_path": "[..]/normal-dep-0.0.1/src/lib.rs",
"edition": "2015", "edition": "2015",
"test": true, "test": true,

View File

@ -4671,9 +4671,9 @@ fn test_workspaces_cwd() {
.build(); .build();
p.cargo("test --workspace --all") p.cargo("test --workspace --all")
.with_stderr_contains("[DOCTEST] root-crate") .with_stderr_contains("[DOCTEST] root_crate")
.with_stderr_contains("[DOCTEST] nested-crate") .with_stderr_contains("[DOCTEST] nested_crate")
.with_stderr_contains("[DOCTEST] deep-crate") .with_stderr_contains("[DOCTEST] deep_crate")
.with_stdout_contains("test test_unit_root_cwd ... ok") .with_stdout_contains("test test_unit_root_cwd ... ok")
.with_stdout_contains("test test_unit_nested_cwd ... ok") .with_stdout_contains("test test_unit_nested_cwd ... ok")
.with_stdout_contains("test test_unit_deep_cwd ... ok") .with_stdout_contains("test test_unit_deep_cwd ... ok")
@ -4683,33 +4683,33 @@ fn test_workspaces_cwd() {
.run(); .run();
p.cargo("test -p root-crate --all") p.cargo("test -p root-crate --all")
.with_stderr_contains("[DOCTEST] root-crate") .with_stderr_contains("[DOCTEST] root_crate")
.with_stdout_contains("test test_unit_root_cwd ... ok") .with_stdout_contains("test test_unit_root_cwd ... ok")
.with_stdout_contains("test test_integration_root_cwd ... ok") .with_stdout_contains("test test_integration_root_cwd ... ok")
.run(); .run();
p.cargo("test -p nested-crate --all") p.cargo("test -p nested-crate --all")
.with_stderr_contains("[DOCTEST] nested-crate") .with_stderr_contains("[DOCTEST] nested_crate")
.with_stdout_contains("test test_unit_nested_cwd ... ok") .with_stdout_contains("test test_unit_nested_cwd ... ok")
.with_stdout_contains("test test_integration_nested_cwd ... ok") .with_stdout_contains("test test_integration_nested_cwd ... ok")
.run(); .run();
p.cargo("test -p deep-crate --all") p.cargo("test -p deep-crate --all")
.with_stderr_contains("[DOCTEST] deep-crate") .with_stderr_contains("[DOCTEST] deep_crate")
.with_stdout_contains("test test_unit_deep_cwd ... ok") .with_stdout_contains("test test_unit_deep_cwd ... ok")
.with_stdout_contains("test test_integration_deep_cwd ... ok") .with_stdout_contains("test test_integration_deep_cwd ... ok")
.run(); .run();
p.cargo("test --all") p.cargo("test --all")
.cwd("nested-crate") .cwd("nested-crate")
.with_stderr_contains("[DOCTEST] nested-crate") .with_stderr_contains("[DOCTEST] nested_crate")
.with_stdout_contains("test test_unit_nested_cwd ... ok") .with_stdout_contains("test test_unit_nested_cwd ... ok")
.with_stdout_contains("test test_integration_nested_cwd ... ok") .with_stdout_contains("test test_integration_nested_cwd ... ok")
.run(); .run();
p.cargo("test --all") p.cargo("test --all")
.cwd("very/deeply/nested/deep-crate") .cwd("very/deeply/nested/deep-crate")
.with_stderr_contains("[DOCTEST] deep-crate") .with_stderr_contains("[DOCTEST] deep_crate")
.with_stdout_contains("test test_unit_deep_cwd ... ok") .with_stdout_contains("test test_unit_deep_cwd ... ok")
.with_stdout_contains("test test_integration_deep_cwd ... ok") .with_stdout_contains("test test_integration_deep_cwd ... ok")
.run(); .run();