Description
Problem
When I declare a dependency that includes an underscore (_
) in its name under [workspace.dependencies]
in the manifest of my virtual workspace and then add that dependency to a member crate using cargo add
, I get different results depending on how I invoke the command:
- If I specify the dependency with an underscore, it is added to the member manifest as
<dependency>.workspace = true
- If I specify the dependency with a hyphen, it is translated but then added to the member manifest as
<dependency> = "version"
Expected behavior: Since the dependencies are the same in both cases, I would expect both invocations to result in <dependency>.workspace = true
.
Steps
Folder structure
|-- Cargo.toml
|-- crate/
|---- Cargo.toml
Cargo.toml (workspace)
[workspace]
members = ["crate"]
resolver = "2"
[workspace.dependencies]
env_logger = "0.11.5"
Cargo.toml (crate)
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
[dependencies]
env_logger.workspace = true # cargo add env_logger
env_logger = "0.11.5" # cargo add env-logger
Possible Solution(s)
It seems like Cargo compares the desired dependency to the dependencies listed in the workspace manifest at some point, to decide when to use <dependency>.workspace = true
. It also seems like Cargo translates hyphens to underscores when appropriate. But perhaps the comparison happens before the translation, so we see this behavior (just my guess). If we reverse the order of those operations, then that should solve the issue.
Notes
No response
Version
cargo 1.76.0 (c84b367 2024-01-18)
release: 1.76.0
commit-hash: c84b367
commit-date: 2024-01-18
host: x86_64-apple-darwin
libgit2: 1.7.1 (sys:0.18.1 vendored)
libcurl: 8.7.1 (sys:0.4.70+curl-8.5.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 1.1.1w 11 Sep 2023
os: Mac OS 14.6.1 [64-bit]