Skip to content

Commit 4567826

Browse files
committed
Restore crates.io's SourceId hash value to before
This commit restores the hash value of the crates.io `SourceId` to what it was before #9384. In #9384 the enum variants of `SourceKind` were reordered which accidentally changed the hash value of the `SourceId` for crates.io. A change here means that users with a new version of Cargo will have to redownload the index and all crates, which is something that we strive to avoid forcing. In changing this, though, it required a manual implementation of `Ord` to still contain the actual fix from #9384 which is to sort `SourceKind` differently from how it's defined. I was curious as to why this was necessary since it wasn't ever necessary in the past and this led to an odd spelunking which turned up some interesting information. Turns out Rust 1.47 and after had a breaking change where Cargo would sort dependencies differently. This means that #9334 *could* have been opened up much earlier, but it never was. We ironically only saw an issue when we fixed this regression (although we didn't realize we were fixing a regression). This means that we are now permanently codifying the regression in Cargo.
1 parent 4bcbf87 commit 4567826

File tree

1 file changed

+101
-7
lines changed

1 file changed

+101
-7
lines changed

src/cargo/core/source/source_id.rs

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,18 @@ struct SourceIdInner {
4242

4343
/// The possible kinds of code source. Along with `SourceIdInner`, this fully defines the
4444
/// source.
45-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
45+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4646
enum SourceKind {
47-
// Note that the ordering here is important for how it affects the `Ord`
48-
// implementation, notably how this affects the ordering of serialized
49-
// packages into lock files.
50-
/// A local path..
47+
/// A git repository.
48+
Git(GitReference),
49+
/// A local path.
5150
Path,
5251
/// A remote registry.
5352
Registry,
5453
/// A local filesystem-based registry.
5554
LocalRegistry,
5655
/// A directory-based registry.
5756
Directory,
58-
/// A git repository.
59-
Git(GitReference),
6057
}
6158

6259
/// Information to find a specific commit in a Git repository.
@@ -486,6 +483,103 @@ impl Hash for SourceId {
486483
}
487484
}
488485

486+
// forward to `Ord`
487+
impl PartialOrd for SourceKind {
488+
fn partial_cmp(&self, other: &SourceKind) -> Option<Ordering> {
489+
Some(self.cmp(other))
490+
}
491+
}
492+
493+
// Note that this is specifically not derived on `SourceKind` although the
494+
// implementation here is very similar to what it might look like if it were
495+
// otherwise derived.
496+
//
497+
// The reason for this is somewhat obtuse. First of all the hash value of
498+
// `SourceKind` makes its way into `~/.cargo/registry/index/github.com-XXXX`
499+
// which means that changes to the hash means that all Rust users need to
500+
// redownload the crates.io index and all their crates. If possible we strive to
501+
// not change this to make this redownloading behavior happen as little as
502+
// possible. How is this connected to `Ord` you might ask? That's a good
503+
// question!
504+
//
505+
// Since the beginning of time `SourceKind` has had `#[derive(Hash)]`. It for
506+
// the longest time *also* derived the `Ord` and `PartialOrd` traits. In #8522,
507+
// however, the implementation of `Ord` changed. This handwritten implementation
508+
// forgot to sync itself with the originally derived implementation, namely
509+
// placing git dependencies as sorted after all other dependencies instead of
510+
// first as before.
511+
//
512+
// This regression in #8522 (Rust 1.47) went unnoticed. When we switched back
513+
// to a derived implementation in #9133 (Rust 1.52 beta) we only then ironically
514+
// saw an issue (#9334). In #9334 it was observed that stable Rust at the time
515+
// (1.51) was sorting git dependencies last, whereas Rust 1.52 beta would sort
516+
// git dependencies first. This is because the `PartialOrd` implementation in
517+
// 1.51 used #8522, the buggy implementation, which put git deps last. In 1.52
518+
// it was (unknowingly) restored to the pre-1.47 behavior with git dependencies
519+
// first.
520+
//
521+
// Because the breakage was only witnessed after the original breakage, this
522+
// trait implementation is preserving the "broken" behavior. Put a different way:
523+
//
524+
// * Rust pre-1.47 sorted git deps first.
525+
// * Rust 1.47 to Rust 1.51 sorted git deps last, a breaking change that was
526+
// never noticed.
527+
// * Rust 1.52 restored the pre-1.47 behavior (without knowing it did so), and
528+
// breakage was witnessed by actual users due to difference with 1.51.
529+
// * Rust 1.52 (the source as it lives now) changed to match the 1.47-1.51
530+
// behavior, which is now considered intentionally breaking from the pre-1.47
531+
// behavior.
532+
//
533+
// That's all a long winded way of saying "it's wierd that git deps hash first
534+
// and are sorted last, but it's the way it is right now". The author of this
535+
// comment chose to handwrite the `Ord` implementation instead of the `Hash`
536+
// implementation, but it's only required that at most one of them is
537+
// hand-written because the other can be derived. Perhaps one day in
538+
// the future someone can figure out how to remove this behavior.
539+
impl Ord for SourceKind {
540+
fn cmp(&self, other: &SourceKind) -> Ordering {
541+
match (self, other) {
542+
(SourceKind::Path, SourceKind::Path) => Ordering::Equal,
543+
(SourceKind::Path, _) => Ordering::Less,
544+
(_, SourceKind::Path) => Ordering::Greater,
545+
546+
(SourceKind::Registry, SourceKind::Registry) => Ordering::Equal,
547+
(SourceKind::Registry, _) => Ordering::Less,
548+
(_, SourceKind::Registry) => Ordering::Greater,
549+
550+
(SourceKind::LocalRegistry, SourceKind::LocalRegistry) => Ordering::Equal,
551+
(SourceKind::LocalRegistry, _) => Ordering::Less,
552+
(_, SourceKind::LocalRegistry) => Ordering::Greater,
553+
554+
(SourceKind::Directory, SourceKind::Directory) => Ordering::Equal,
555+
(SourceKind::Directory, _) => Ordering::Less,
556+
(_, SourceKind::Directory) => Ordering::Greater,
557+
558+
(SourceKind::Git(a), SourceKind::Git(b)) => a.cmp(b),
559+
}
560+
}
561+
}
562+
563+
// This is a test that the hash of the `SourceId` for crates.io is a well-known
564+
// value.
565+
//
566+
// Note that the hash value matches what the crates.io source id has hashed
567+
// since long before Rust 1.30. We strive to keep this value the same across
568+
// versions of Cargo because changing it means that users will need to
569+
// redownload the index and all crates they use when using a new Cargo version.
570+
//
571+
// This isn't to say that this hash can *never* change, only that when changing
572+
// this it should be explicitly done. If this hash changes accidentally and
573+
// you're able to restore the hash to its original value, please do so!
574+
// Otherwise please just leave a comment in your PR as to why the hash value is
575+
// changing and why the old value can't be easily preserved.
576+
#[test]
577+
fn test_cratesio_hash() {
578+
let config = Config::default().unwrap();
579+
let crates_io = SourceId::crates_io(&config).unwrap();
580+
assert_eq!(crate::util::hex::short_hash(&crates_io), "1ecc6299db9ec823");
581+
}
582+
489583
/// A `Display`able view into a `SourceId` that will write it as a url
490584
pub struct SourceIdAsUrl<'a> {
491585
inner: &'a SourceIdInner,

0 commit comments

Comments
 (0)