Skip to content

The dependency resolution is confused when using git dependency and there's a lockfile #11490

@StyMaar

Description

@StyMaar

Problem

I'm working on some piece of proprietary code, and for the internal dependencies, we use git repositories instead of crates on crates.io or some internal registry.

Today I faced I really annoying error message:

error[E0308]: mismatched types
 --> src/main.rs:6:9
  |
6 |     foo(x);
  |     --- ^ expected enum `lower::X`, found enum `upper::X`
  |     |
  |     arguments to this function are incorrect
  |
  = note: enum `upper::X` and enum `lower::X` have similar names, but are actually distinct types
note: enum `upper::X` is defined in crate `lower`
 --> /home/stymaar/.cargo/git/checkouts/repro_cargo_lower-dc970fbba3cb3609/5914bef/src/lib.rs:1:1
  |
1 | pub enum X{
  | ^^^^^^^^^^
note: enum `lower::X` is defined in crate `lower`
 --> /home/stymaar/.cargo/git/checkouts/repro_cargo_lower-dc970fbba3cb3609/5914bef/src/lib.rs:1:1
  |
1 | pub enum X{
  | ^^^^^^^^^^
  = note: perhaps two different versions of crate `lower` are being used?
note: function defined here
 --> /home/stymaar/.cargo/git/checkouts/repro_cargo_lower-dc970fbba3cb3609/5914bef/src/lib.rs:6:8
  |
6 | pub fn foo(x:X){
  |        ^^^

So it complains that the type comes from different versions of the lower crate, whereas it comes from the same version as we can see in the error message itself (for both, it's repro_cargo_lower-dc970fbba3cb3609/5914bef).

I was very confused for a while and then I managed to understand what's going on an reproduce it:

Steps

  1. git clone [email protected]:StyMaar/repro_cargo_top.git
  2. cargo check

Notes

The problem comes from the upper dependency which does not specify the target commit (there is no rev in the declaration of the lower dependency in upper) but there's a Cargo.lock file which points to the same revision of lower as the one being used in the top-level crate.

In the end, the lower dependency is imported twice for the same revision, since cargo doesn't realize that it's in fact the same revision, and then rustc complains about the presence of two different types.

Version

cargo 1.67.0-nightly (eb5d35917 2022-11-17)
release: 1.67.0-nightly
commit-hash: eb5d35917b2395194593c9ca70c3778f60c1573b
commit-date: 2022-11-17
host: x86_64-unknown-linux-gnu
libgit2: 1.5.0 (sys:0.15.0 vendored)
libcurl: 7.86.0-DEV (sys:0.4.59+curl-7.86.0 vendored ssl:OpenSSL/1.1.1q)
os: Linux 19.3 (tricia) [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-gitArea: anything dealing with gitA-lockfileArea: Cargo.lock issuesC-bugCategory: bugS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions