Skip to content

Commit 3ca04e2

Browse files
committed
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.
1 parent 8b2cc62 commit 3ca04e2

File tree

6 files changed

+23
-19
lines changed

6 files changed

+23
-19
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,20 @@ fn clean_lib(
154154
let lib = match toml_lib {
155155
Some(lib) => {
156156
if let Some(ref name) = lib.name {
157-
// XXX: other code paths dodge this validation
158157
if name.contains('-') {
159158
anyhow::bail!("library target names cannot contain hyphens: {}", name)
160159
}
161160
}
162161
Some(TomlTarget {
163-
name: lib.name.clone().or_else(|| Some(package_name.to_owned())),
162+
name: lib
163+
.name
164+
.clone()
165+
.or_else(|| Some(package_name.replace("-", "_"))),
164166
..lib.clone()
165167
})
166168
}
167169
None => inferred.as_ref().map(|lib| TomlTarget {
168-
name: Some(package_name.to_string()),
170+
name: Some(package_name.replace("-", "_")),
169171
path: Some(PathValue(lib.clone())),
170172
..TomlTarget::new()
171173
}),

tests/testsuite/artifact_dep.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,8 +817,10 @@ fn lib_with_selected_dashed_bin_artifact_and_lib_true() {
817817
let _b = include_bytes!(env!("CARGO_BIN_FILE_BAR_BAZ_baz-suffix"));
818818
let _b = include_bytes!(env!("CARGO_STATICLIB_FILE_BAR_BAZ"));
819819
let _b = include_bytes!(env!("CARGO_STATICLIB_FILE_BAR_BAZ_bar-baz"));
820+
let _b = include_bytes!(env!("CARGO_STATICLIB_FILE_BAR_BAZ_bar_baz"));
820821
let _b = include_bytes!(env!("CARGO_CDYLIB_FILE_BAR_BAZ"));
821822
let _b = include_bytes!(env!("CARGO_CDYLIB_FILE_BAR_BAZ_bar-baz"));
823+
let _b = include_bytes!(env!("CARGO_CDYLIB_FILE_BAR_BAZ_bar_baz"));
822824
}
823825
"#,
824826
)

tests/testsuite/collisions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ fn collision_with_root() {
534534
[DOWNLOADING] crates ...
535535
[DOWNLOADED] foo-macro v1.0.0 [..]
536536
warning: output filename collision.
537-
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 [..]`.
537+
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 [..]`.
538538
Colliding filename is: [CWD]/target/doc/foo_macro/index.html
539539
The targets should have unique names.
540540
This is a known bug where multiple crates with the same name use

tests/testsuite/doc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,7 +2089,7 @@ fn doc_test_in_workspace() {
20892089
)
20902090
.build();
20912091
p.cargo("test --doc -vv")
2092-
.with_stderr_contains("[DOCTEST] crate-a")
2092+
.with_stderr_contains("[DOCTEST] crate_a")
20932093
.with_stdout_contains(
20942094
"
20952095
running 1 test
@@ -2098,7 +2098,7 @@ test crate-a/src/lib.rs - (line 1) ... ok
20982098
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
20992099
",
21002100
)
2101-
.with_stderr_contains("[DOCTEST] crate-b")
2101+
.with_stderr_contains("[DOCTEST] crate_b")
21022102
.with_stdout_contains(
21032103
"
21042104
running 1 test

tests/testsuite/metadata.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ fn workspace_metadata_with_dependencies_and_resolve() {
16161616
"kind": [
16171617
"lib"
16181618
],
1619-
"name": "non-artifact",
1619+
"name": "non_artifact",
16201620
"src_path": "[..]/foo/non-artifact/src/lib.rs",
16211621
"test": true
16221622
}
@@ -3110,7 +3110,7 @@ fn filter_platform() {
31103110
"crate_types": [
31113111
"lib"
31123112
],
3113-
"name": "alt-dep",
3113+
"name": "alt_dep",
31143114
"src_path": "[..]/alt-dep-0.0.1/src/lib.rs",
31153115
"edition": "2015",
31163116
"test": true,
@@ -3154,7 +3154,7 @@ fn filter_platform() {
31543154
"crate_types": [
31553155
"lib"
31563156
],
3157-
"name": "cfg-dep",
3157+
"name": "cfg_dep",
31583158
"src_path": "[..]/cfg-dep-0.0.1/src/lib.rs",
31593159
"edition": "2015",
31603160
"test": true,
@@ -3198,7 +3198,7 @@ fn filter_platform() {
31983198
"crate_types": [
31993199
"lib"
32003200
],
3201-
"name": "host-dep",
3201+
"name": "host_dep",
32023202
"src_path": "[..]/host-dep-0.0.1/src/lib.rs",
32033203
"edition": "2015",
32043204
"test": true,
@@ -3242,7 +3242,7 @@ fn filter_platform() {
32423242
"crate_types": [
32433243
"lib"
32443244
],
3245-
"name": "normal-dep",
3245+
"name": "normal_dep",
32463246
"src_path": "[..]/normal-dep-0.0.1/src/lib.rs",
32473247
"edition": "2015",
32483248
"test": true,

tests/testsuite/test.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4671,9 +4671,9 @@ fn test_workspaces_cwd() {
46714671
.build();
46724672

46734673
p.cargo("test --workspace --all")
4674-
.with_stderr_contains("[DOCTEST] root-crate")
4675-
.with_stderr_contains("[DOCTEST] nested-crate")
4676-
.with_stderr_contains("[DOCTEST] deep-crate")
4674+
.with_stderr_contains("[DOCTEST] root_crate")
4675+
.with_stderr_contains("[DOCTEST] nested_crate")
4676+
.with_stderr_contains("[DOCTEST] deep_crate")
46774677
.with_stdout_contains("test test_unit_root_cwd ... ok")
46784678
.with_stdout_contains("test test_unit_nested_cwd ... ok")
46794679
.with_stdout_contains("test test_unit_deep_cwd ... ok")
@@ -4683,33 +4683,33 @@ fn test_workspaces_cwd() {
46834683
.run();
46844684

46854685
p.cargo("test -p root-crate --all")
4686-
.with_stderr_contains("[DOCTEST] root-crate")
4686+
.with_stderr_contains("[DOCTEST] root_crate")
46874687
.with_stdout_contains("test test_unit_root_cwd ... ok")
46884688
.with_stdout_contains("test test_integration_root_cwd ... ok")
46894689
.run();
46904690

46914691
p.cargo("test -p nested-crate --all")
4692-
.with_stderr_contains("[DOCTEST] nested-crate")
4692+
.with_stderr_contains("[DOCTEST] nested_crate")
46934693
.with_stdout_contains("test test_unit_nested_cwd ... ok")
46944694
.with_stdout_contains("test test_integration_nested_cwd ... ok")
46954695
.run();
46964696

46974697
p.cargo("test -p deep-crate --all")
4698-
.with_stderr_contains("[DOCTEST] deep-crate")
4698+
.with_stderr_contains("[DOCTEST] deep_crate")
46994699
.with_stdout_contains("test test_unit_deep_cwd ... ok")
47004700
.with_stdout_contains("test test_integration_deep_cwd ... ok")
47014701
.run();
47024702

47034703
p.cargo("test --all")
47044704
.cwd("nested-crate")
4705-
.with_stderr_contains("[DOCTEST] nested-crate")
4705+
.with_stderr_contains("[DOCTEST] nested_crate")
47064706
.with_stdout_contains("test test_unit_nested_cwd ... ok")
47074707
.with_stdout_contains("test test_integration_nested_cwd ... ok")
47084708
.run();
47094709

47104710
p.cargo("test --all")
47114711
.cwd("very/deeply/nested/deep-crate")
4712-
.with_stderr_contains("[DOCTEST] deep-crate")
4712+
.with_stderr_contains("[DOCTEST] deep_crate")
47134713
.with_stdout_contains("test test_unit_deep_cwd ... ok")
47144714
.with_stdout_contains("test test_integration_deep_cwd ... ok")
47154715
.run();

0 commit comments

Comments
 (0)