cargo icon indicating copy to clipboard operation
cargo copied to clipboard

cargo add to package within a virtual workspace behaves differently when translating package name

Open dohse opened this issue 1 year ago • 1 comments

Problem

cargo add behaves differently when operating on a package in a virtual workspace depending whether the package name is not translated or translated (cargo shows warning: translating …).

In the first case cargo adds a reference to dependency declared in [workspace.dependencies] - which I believe is the desired result.

In the later case cargo adds the dependency and the version, but does not reference [workspace.dependencies] - I believe this is not desired.

Steps

Reproduction script when adding parking-lot:

> mkdir workspace
> cd workspace
> cat > Cargo.toml
[workspace]
resolver = "2"

[workspace.dependencies]
parking_lot = "0.12.1"
> cargo new x
     Created binary (application) `x` package
> cargo add -p x parking-lot
    Updating crates.io index
warning: translating `parking-lot` to `parking_lot`
      Adding parking_lot v0.12.1 to dependencies.
             Features:
             - arc_lock
             - deadlock_detection
             - hardware-lock-elision
             - nightly
             - owning_ref
             - send_guard
             - serde
    Updating crates.io index
> cat x/Cargo.toml
[package]
name = "x"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
parking_lot = "0.12.1"

Reproduction script when adding parking_lot:

> mkdir workspace
> cd workspace
> cat > Cargo.toml
[workspace]
resolver = "2"

[workspace.dependencies]
parking_lot = "0.12.1"
> cargo new x
     Created binary (application) `x` package
> cargo add -p x parking_lot
    Updating crates.io index
      Adding parking_lot (workspace) to dependencies.
             Features as of v0.12.1:
             - arc_lock
             - deadlock_detection
             - hardware-lock-elision
             - nightly
             - owning_ref
             - send_guard
             - serde
    Updating crates.io index
> cat x/Cargo.toml
[package]
name = "x"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
parking_lot.workspace = true

Possible Solution(s)

No response

Notes

I tested this with the latest rust release.

This might be related to this issue: https://github.com/rust-lang/cargo/issues/10680

Version

> cargo version --verbose
cargo 1.77.1 (e52e36006 2024-03-26)
release: 1.77.1
commit-hash: e52e360061cacbbeac79f7f1215a7a90b6f08442
commit-date: 2024-03-26
host: x86_64-unknown-linux-gnu
libgit2: 1.7.2 (sys:0.18.2 vendored)
libcurl: 8.5.0-DEV (sys:0.4.70+curl-8.5.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Ubuntu 20.04 (focal) [64-bit]

dohse avatar Apr 04 '24 20:04 dohse

The root cause is the same as #10680. See that for more insight

epage avatar Apr 10 '24 15:04 epage