Any workaround to having examples in a workspace with the same name?

I have a workspace crate that collects together a few crates that have examples named example.rs. Crates also typically have examples (and binaries) with common names like client, server, or app, etc. However if there exist multiple workspace members with examples that have the same name, the following command:

$ cargo test --workspace

Will generate:

warning: output filename collision.
...
The targets should have unique names.
Consider changing their names to be unique or compiling them separately.
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.

I looked through the related issues but I didn't really see a workaround besides ensuring that every member of a workspace has unique names for every example.

This was my conclusion as well when I first encountered that warning.

It would be neat if it could automatically generate example names in workspaces by joining the crates’ names and the example’s name: So the example bar under crate foo becomes foo-bar.

Even if there wasn’t this limitation, it's useful to give examples unique names because it allows them to be run with just cargo run --example <example-name> instead of having to specify the package too.

I recommend giving examples names that describe what they do. And if they don’t exactly do anything specific, consider whether the code in them should be documentation tests/examples instead of separate example targets.

1 Like

Something like that, or why not just have subfolders for projects in a workspace, like examples/foo/bar.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.