Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo add disables previously enabled features when converting between _ and - #10680

Closed
Aloso opened this issue May 20, 2022 · 4 comments · Fixed by #13765
Closed

cargo add disables previously enabled features when converting between _ and - #10680

Aloso opened this issue May 20, 2022 · 4 comments · Fixed by #13765
Labels
C-bug Category: bug Command-add S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review

Comments

@Aloso
Copy link

Aloso commented May 20, 2022

Problem

cargo add can be used to add an optional feature to an existing dependency:

> cargo add once_cell -F unstable
    Updating crates.io index
      Adding once_cell v1.11.0 to dependencies.
             Features:
             + alloc
             + race
             + std
             + unstable
             - atomic-polyfill
             - parking_lot
             - parking_lot_core

> cargo add once_cell -F parking_lot
    Updating crates.io index
      Adding once_cell v1.11.0 to dependencies.
             Features:
             + alloc
             + parking_lot
             + parking_lot_core
             + race
             + std
             + unstable
             - atomic-polyfill
# note that `unstable` is still enabled from before!

Furthermore, cargo add can convert _ to - and vice versa in crate names. However, when doing this translation, existing features are dropped:

> cargo add once-cell -F parking_lot
    Updating crates.io index
warning: translating `once-cell` to `once_cell`
      Adding once_cell v1.11.0 to dependencies.
             Features:
             + alloc
             + parking_lot
             + parking_lot_core
             + race
             + std
             - atomic-polyfill
             - unstable
# note that `unstable` is no longer enabled!

cargo add should behave the same whether the crate name was written in its canonical form or not.

Version

cargo 1.63.0-nightly (a4c1cd0eb 2022-05-18)
release: 1.63.0-nightly
commit-hash: a4c1cd0eb6b18082a7e693f5a665548fe1534be4
commit-date: 2022-05-18
host: x86_64-unknown-linux-gnu
libgit2: 1.4.2 (sys:0.14.2 vendored)
libcurl: 7.80.0-DEV (sys:0.4.51+curl-7.80.0 vendored ssl:OpenSSL/1.1.1n)
os: Manjaro 21.2.6 (Qonos) [64-bit]
@Aloso Aloso added the C-bug Category: bug label May 20, 2022
@epage
Copy link
Contributor

epage commented May 20, 2022

What's happening is that the name as it exists on the command line was not found in the manifest, so we think its a new dependency and look it up in the registry. We do a fuzzy look up in the registry so that it will canonicalize it which causes it to match with an entry in the manifest and overwrites it.

Preferably we would not disable the canonicalization but extend it to checking existing dependencies. The code for this is currently embedded in the registry index code and would need to be pulled out to be reused.

We do not canonicalize git and path dependencies. It could be said that we should drop canonicalization completely to have parity between the code paths but it is less important for git / path dependencies because

  • They will error if the name doesn't match
  • The dependency name is optional, we will instead pull from the dependency's manifest.

@epage
Copy link
Contributor

epage commented May 20, 2022

The part I'm a little uncertain of is if we allow fuzzy lookups into the manifest file

  • Should we only allow that for registry dependencies or all dependencies?
  • Should we prioritize the highest priority table or the closest match?

@epage
Copy link
Contributor

epage commented Apr 10, 2024

#13702 is another manifestation of this

dohse pushed a commit to dohse/cargo that referenced this issue Apr 13, 2024
dohse pushed a commit to dohse/cargo that referenced this issue Apr 17, 2024
dohse pushed a commit to dohse/cargo that referenced this issue Apr 17, 2024
dohse added a commit to dohse/cargo that referenced this issue Apr 17, 2024
dohse added a commit to dohse/cargo that referenced this issue Apr 17, 2024
dohse added a commit to dohse/cargo that referenced this issue Apr 17, 2024
dohse added a commit to dohse/cargo that referenced this issue Apr 17, 2024
@dohse
Copy link
Contributor

dohse commented Apr 17, 2024

I created a fix for this issue. I did not follow the route of doing fuzzy lookups within cargo add, but just repeat the workspace existing dependency lookup and workspace dependency lookup when the latest dependency lookup returns a different name.

dohse added a commit to dohse/cargo that referenced this issue Apr 17, 2024
dohse added a commit to dohse/cargo that referenced this issue Apr 17, 2024
dohse added a commit to dohse/cargo that referenced this issue Apr 17, 2024
dohse added a commit to dohse/cargo that referenced this issue May 1, 2024
dohse added a commit to dohse/cargo that referenced this issue May 1, 2024
dohse added a commit to dohse/cargo that referenced this issue May 1, 2024
dohse added a commit to dohse/cargo that referenced this issue May 2, 2024
dohse added a commit to dohse/cargo that referenced this issue May 2, 2024
epage pushed a commit to dohse/cargo that referenced this issue Aug 28, 2024
epage pushed a commit to dohse/cargo that referenced this issue Aug 28, 2024
dohse added a commit to dohse/cargo that referenced this issue Aug 29, 2024
dohse added a commit to dohse/cargo that referenced this issue Aug 29, 2024
dohse added a commit to dohse/cargo that referenced this issue Aug 29, 2024
dohse added a commit to dohse/cargo that referenced this issue Aug 29, 2024
dohse added a commit to dohse/cargo that referenced this issue Aug 29, 2024
dohse added a commit to dohse/cargo that referenced this issue Aug 29, 2024
dohse added a commit to dohse/cargo that referenced this issue Aug 29, 2024
dohse added a commit to dohse/cargo that referenced this issue Aug 29, 2024
dohse added a commit to dohse/cargo that referenced this issue Aug 30, 2024
dohse added a commit to dohse/cargo that referenced this issue Aug 30, 2024
dohse added a commit to dohse/cargo that referenced this issue Aug 30, 2024
bors added a commit that referenced this issue Sep 3, 2024
Fix cargo add behaving different when translating package name

Fixes #13702
Fixes #10680

TODOs

- [x] ~Fuzzy match registry dependencies in `cargo remove` as well~
     `cargo remove` does not need fuzzy matching, because there is no unexpected behavior for the user
- [x] ~Don't duplicate name permutation generation~
     Unsure whether this is worth it
- [ ] Shall we reject cases where multiple different permutations match?
- [x] Add comments that explain the behavior
bors added a commit that referenced this issue Sep 4, 2024
Fix cargo add behaving different when translating package name

Fixes #13702
Fixes #10680

TODOs

- [x] ~Fuzzy match registry dependencies in `cargo remove` as well~
     `cargo remove` does not need fuzzy matching, because there is no unexpected behavior for the user
- [x] ~Don't duplicate name permutation generation~
     Unsure whether this is worth it
- [ ] Shall we reject cases where multiple different permutations match?
- [x] Add comments that explain the behavior
@bors bors closed this as completed in 9391bfb Sep 4, 2024
dingxiangfei2009 pushed a commit to dingxiangfei2009/cargo that referenced this issue Sep 17, 2024
dingxiangfei2009 pushed a commit to dingxiangfei2009/cargo that referenced this issue Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug Command-add S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants