rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.74k stars 2.42k forks source link

Workspace with multiple members using the same example name results in filename conflict #14769

Closed sdroege closed 38 minutes ago

sdroege commented 2 hours ago

Problem

If a cargo workspace contains multiple member crates that have an example with the same name then the executable of that example is stored place for all of them.

Steps

  1. Create a workspace of the following shape (actual file content doesn't really matter)
a/Cargo.toml
a/src
a/src/lib.rs
a/examples
a/examples/dummy.rs
b/Cargo.toml
b/examples
b/examples/dummy.rs
b/src
b/src/lib.rs
Cargo.toml
  1. $ cargo build -p a --example dummy
$ ls -al target/debug/examples
drwxr-xr-x. slomo slomo 116 B  Fri Nov  1 12:49:25 2024  .
drwxr-xr-x. slomo slomo 102 B  Fri Nov  1 12:49:25 2024  ..
.rwxr-xr-x. slomo slomo 3.7 MB Fri Nov  1 12:49:25 2024  dummy
.rwxr-xr-x. slomo slomo 3.7 MB Fri Nov  1 12:49:25 2024  dummy-d4e2cacc3295a333
.rw-r--r--. slomo slomo 227 B  Fri Nov  1 12:49:25 2024  dummy-d4e2cacc3295a333.d
.rw-r--r--. slomo slomo 167 B  Fri Nov  1 12:49:25 2024  dummy.d
  1. $ cargo build -p b --example dummy
$ ls -al target/debug/examples
drwxr-xr-x. slomo slomo 208 B  Fri Nov  1 12:49:37 2024  .
drwxr-xr-x. slomo slomo 102 B  Fri Nov  1 12:49:25 2024  ..
.rwxr-xr-x. slomo slomo 3.7 MB Fri Nov  1 12:49:37 2024  dummy
.rwxr-xr-x. slomo slomo 3.7 MB Fri Nov  1 12:49:25 2024  dummy-d4e2cacc3295a333
.rw-r--r--. slomo slomo 227 B  Fri Nov  1 12:49:25 2024  dummy-d4e2cacc3295a333.d
.rwxr-xr-x. slomo slomo 3.7 MB Fri Nov  1 12:49:37 2024  dummy-dc40cbdf25da73d7
.rw-r--r--. slomo slomo 227 B  Fri Nov  1 12:49:37 2024  dummy-dc40cbdf25da73d7.d
.rw-r--r--. slomo slomo 167 B  Fri Nov  1 12:49:37 2024  dummy.d

Note that target/debug/examples/dummy and target/debug/examples/dummy.d is used by both crates. That's obviously not optimal because only one example can exist at a time like this, but it also means that parallel builds on Windows fail randomly with a linker error if both crates are built in parallel and two link.exe are trying to access target/debug/example/dummy.exe at the same time.

Possible Solution(s)

Store examples in subdirectories based on the crates they're from

Notes

No response

Version

cargo 1.82.0 (8f40fc59f 2024-08-21) release: 1.82.0 commit-hash: 8f40fc59fb0c8df91c97405785197f3c630304ea commit-date: 2024-08-21 host: x86_64-unknown-linux-gnu libgit2: 1.8.1 (sys:0.19.0 vendored) libcurl: 8.9.0-DEV (sys:0.4.74+curl-8.9.0 vendored ssl:OpenSSL/1.1.1w) ssl: OpenSSL 1.1.1w 11 Sep 2023 os: Fedora 41.0.0 [64-bit]

sdroege commented 2 hours ago

FWIW, I ran into this when trying to work around https://github.com/rust-lang/cargo/issues/13447 by simply placing a examples/dummy.rs into all crates that don't have any example to force the publishing of its Cargo.lock.

ehuss commented 38 minutes ago

Thanks for the report! This is a known issue, and if you build both examples at the same time you should get a warning of warning: output filename collision. along with an explanation. Since this is tracked in #6313, closing as a duplicate of that.

sdroege commented 5 minutes ago

Thanks, sorry for missing that. Searched for the wrong keywords apparently :)