rust-lang / cargo

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

registry: use crates.io-index and crates.io-index.git interchangeably? #8856

Open d-e-s-o opened 3 years ago

d-e-s-o commented 3 years ago

Please have a look at this workspace containing a binary mybin, a library mylib, and a local registry. mybin depends on mylib.

Here is the kicker: mybin also depends on a crates.io published crate (uid) and so does mylib. In order to make this work I need to set "registry":"https://github.com/rust-lang/crates.io-index.git" for the uid dependency in the index entry for mylib.

When I build I get:

$ cargo build
    Updating `/tmp/workspace/myregistry` index
    Updating crates.io index
    Updating `https://github.com/rust-lang/crates.io-index.git` index
   Compiling uid v0.1.4 (registry `https://github.com/rust-lang/crates.io-index.git`)
   Compiling uid v0.1.4
   Compiling mylib v0.1.0 (registry `/tmp/workspace/myregistry`)
   Compiling mybin v0.1.0 (/tmp/workspace/mybin)
error[E0308]: mismatched types
  --> src/main.rs:10:7
   |
10 |   foo(mylibfn())
   |       ^^^^^^^^^ expected struct `uid::Id`, found a different struct `uid::Id`
   |
   = note: expected struct `uid::Id<u64>` (struct `uid::Id`)
              found struct `uid::Id<u64>` (struct `uid::Id`)
   = note: perhaps two different versions of crate `uid` are being used?

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `mybin`.

To learn more, run the command again with --verbose.

That's the typical version mismatch error and we can see where it comes from: uid is taken from both crates.io and https://github.com/rust-lang/crates.io-index.git.

So why did I specify https://github.com/rust-lang/crates.io-index.git as the registry to begin with? Because crates.io (or crates-io) does not work:

$ cargo build
    Updating `/tmp/workspace/myregistry` index
    Updating crates.io index
error: no matching package named `mylib` found
location searched: registry `/tmp/workspace/myregistry`
required by package `mybin v0.1.0 (/tmp/workspace/mybin)`

For some reason now mylib (???) would not be found.

So my question is why is crates.io seemingly not recognized properly for the registry key for dependencies? https://doc.rust-lang.org/cargo/reference/registries.html#index-format

I think we probably should special case it there or, alternatively, dedup https://github.com/rust-lang/crates.io-index.git to crates.io (the standard registry).

Notes

Output of cargo version:

$ cargo version
cargo 1.47.0
d-e-s-o commented 3 years ago

Uhm. Figured out a solution. Using https://github.com/rust-lang/crates.io-index instead of https://github.com/rust-lang/crates.io-index.git does the trick. I think this UX really could be improved.

d-e-s-o commented 3 years ago

Updated the title to reflect the issue. While I mention crates.io-index.git the problem likely applies to all registries. Not sure if there is a way to "canonicalize" a URL. Feel free to close otherwise.