rust-lang / cargo

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

`cargo add` doesn't work with local and directory registries #10926

Open jonhoo opened 2 years ago

jonhoo commented 2 years ago

Problem

cargo add is unable to add crates when told to use a registry that is a local or directory registry.

Steps

  1. cargo new --lib foo && cd foo
  2. cargo add libc
  3. cargo check
  4. mkdir .cargo
  5. cargo local-registry -s Cargo.lock local-registry > .cargo/config.toml
  6. sed '/itoa/d' Cargo.toml
  7. cargo add --registry local-registry itoa

Produces

error: no index found for registry: `local-registry`

Alternatively, if registry.default is set to local-registry in .cargo/config.toml, the same error occurs without passing --registry local-registry.

Possible Solution(s)

cargo add --registry foo bar should (assuming that bar exists in the registry foo) add

bar = { version = "...", registry = "foo" }

to Cargo.toml.

Ideally, I think if there's a source replacement of crates-io to foo, cargo add should be smart enough to realize the registry = "foo" part should not be necessary.

Notes

No response

Version

cargo 1.62.1 (a748cf5a3 2022-06-08)
release: 1.62.1
commit-hash: a748cf5a3e666bc2dcdf54f37adef8ef22196452
commit-date: 2022-06-08
host: aarch64-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: Amazon Linux AMI 2.0.0 [64-bit]
jonhoo commented 2 years ago

Just for reference, the error originates here: https://github.com/rust-lang/cargo/blob/0fb9e85e45f5ebd67ae3f9582b4323f6ac91b1db/src/cargo/util/config/mod.rs#L1396