Question: How is the index.crates.io-xxxxxxxx directory name generated?

Hi Cargo team,

When working with crates stored in $CARGO_HOME/registry/src/,
I noticed that the source path is typically under a directory named like index.crates.io-xxxxxxxx.

However, on my machine, there are multiple such directories, for example:

  • index.crates.io-1949cf8c6b5b557f
  • index.crates.io-6f17d22bba15001f

I would like to understand:

  1. How exactly is the xxxxxxxx part (the hash suffix) generated?
  2. Why might there be multiple index.crates.io-* directories on the same machine?
  3. When Cargo needs to resolve a crate during build, how does it determine which index.crates.io-* folder to use?

From reading the source code, it seems related to CanonicalUrl and some hashing mechanism,
but I'm not entirely sure about the complete process.

Thank you very much for your help!

Something like a year ago cargo accidentally changed the input format for this hash slightly. Cargo versions before this version use one directory while newer versions use the other directory.

Edit: Seems like I misremembered. This change was intentional: feature(SourceId): use stable hash from rustc-stable-hash by weihanglo · Pull Request #14917 · rust-lang/cargo · GitHub

1 Like

Thanks a lot for the clear explanation! That helps me understand the situation better. :folded_hands: