5
votes

Cargo stores the source code of downloaded crates in $HOME/.cargo/registry/src (or the appropriate $CARGO_HOME directory). I get that each unique registry I use will create a separate directory, presumably to prevent conflicts.

What I don't understand is why the same repository would create multiple directories:

$ ls -ld ~/.cargo/registry/src/github.com-*
drwxr-xr-x   473 shep  staff  16082 Dec 27  2015 ~/.cargo/registry/src/github.com-0a35038f75765ae4
drwxr-xr-x  1187 shep  staff  40358 Feb  8 19:10 ~/.cargo/registry/src/github.com-1ecc6299db9ec823
drwxr-xr-x   380 shep  staff  12920 Sep 11 12:16 ~/.cargo/registry/src/github.com-88ac128001ac3a9a

Presumably the hash that is appended to the end plays some role in this. What causes Cargo to create one of these directories? Will it ever go back to a previous directory and continue using it?

1
I think one directory is created for each version of Rust.kennytm
@kennytm if that were the case, I'd expect to have over 15 versions (up to the current stable 1.15, plus beta, nightly and pre-1.0 versions), not just the 3.Shepmaster
Interesting, the users of cargo-update also report that the newest one sometimes is not the one used by cargo.набиячлэвэли

1 Answers

6
votes

The hash is based on two fields: the kind of source (central repository, git reference, local path, etc.) and the source's URL.

Cargo used to delegate to rust-url's implementation of Hash for Url, which changed over time. Cargo's implementation was last changed in pull request 2737 in order to fix issue 1710; Cargo now hashes the URL string, rather than the Url object, in order to produce a more stable hash that doesn't depend on rust-url's behavior.

Also, Cargo uses the now-deprecated SipHasher as the hash function. Because it's deprecated, it's possible that Cargo might change to another hash function in the future (or SipHasher might move to a crate and Cargo will use that, who knows).