Skip to content

Commit e33881b

Browse files
committed
Add debug assertions for cache contents
This will largely only get tested during Cargo's own tests, but this commit adds debug assertions where the cache of registry JSON files is always valid and up to date when we consider it being up to date.
1 parent 1daff03 commit e33881b

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/cargo/sources/registry/index.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,9 @@ impl Summaries {
456456
/// for `relative` from the underlying index (aka typically libgit2 with
457457
/// crates.io) and then parse everything in there.
458458
///
459-
/// * `last_index_update` - this is a file modification time where if any cache
460-
/// file is older than this the cache should be considered out of date and
461-
/// needs to be rebuilt.
459+
/// * `index_version` - a version string to describe the current state of
460+
/// the index which for remote registries is the current git sha and
461+
/// for local registries is not available.
462462
/// * `root` - this is the root argument passed to `load`
463463
/// * `cache_root` - this is the root on the filesystem itself of where to
464464
/// store cache files.
@@ -481,12 +481,17 @@ impl Summaries {
481481
// of reasons, but consider all of them non-fatal and just log their
482482
// occurrence in case anyone is debugging anything.
483483
let cache_path = cache_root.join(relative);
484+
let mut cache_contents = None;
484485
if let Some(index_version) = index_version {
485486
match fs::read(&cache_path) {
486487
Ok(contents) => match Summaries::parse_cache(contents, index_version) {
487488
Ok(s) => {
488489
log::debug!("fast path for registry cache of {:?}", relative);
489-
return Ok(Some(s))
490+
if cfg!(debug_assertions) {
491+
cache_contents = Some(s.raw_data);
492+
} else {
493+
return Ok(Some(s))
494+
}
490495
}
491496
Err(e) => {
492497
log::debug!("failed to parse {:?} cache: {}", relative, e);
@@ -537,10 +542,19 @@ impl Summaries {
537542
// or we haven't updated the registry yet. If we actually ran the
538543
// closure though then we care about those errors.
539544
if !hit_closure {
545+
debug_assert!(cache_contents.is_none());
540546
return Ok(None);
541547
}
542548
err?;
543549

550+
// If we've got debug assertions enabled and the cache was previously
551+
// present and considered fresh this is where the debug assertions
552+
// actually happens to verify that our cache is indeed fresh and
553+
// computes exactly the same value as before.
554+
if cfg!(debug_assertions) && cache_contents.is_some() {
555+
assert_eq!(cache_bytes, cache_contents);
556+
}
557+
544558
// Once we have our `cache_bytes` which represents the `Summaries` we're
545559
// about to return, write that back out to disk so future Cargo
546560
// invocations can use it.
@@ -556,6 +570,7 @@ impl Summaries {
556570
}
557571
}
558572
}
573+
559574
Ok(Some(ret))
560575
}
561576

@@ -589,17 +604,25 @@ impl Summaries {
589604
// Implementation of serializing/deserializing the cache of summaries on disk.
590605
// Currently the format looks like:
591606
//
592-
// +--------------+----------------+---+----------------+---+
593-
// | version byte | version string | 0 | JSON blob ... | 0 | ...
594-
// +--------------+----------------+---+----------------+---+
607+
// +--------------+-------------+---+
608+
// | version byte | git sha rev | 0 |
609+
// +--------------+-------------+---+
610+
//
611+
// followed by...
612+
//
613+
// +----------------+---+------------+---+
614+
// | semver version | 0 | JSON blob | 0 | ...
615+
// +----------------+---+------------+---+
595616
//
596617
// The idea is that this is a very easy file for Cargo to parse in future
597618
// invocations. The read from disk should be quite fast and then afterwards all
598619
// we need to know is what versions correspond to which JSON blob.
599620
//
600621
// The leading version byte is intended to ensure that there's some level of
601622
// future compatibility against changes to this cache format so if different
602-
// versions of Cargo share the same cache they don't get too confused.
623+
// versions of Cargo share the same cache they don't get too confused. The git
624+
// sha lets us know when the file needs to be regenerated (it needs regeneration
625+
// whenever the index itself updates).
603626

604627
const CURRENT_CACHE_VERSION: u8 = 1;
605628

src/cargo/util/config.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,8 @@ use crate::util::errors::{self, internal, CargoResult, CargoResultExt};
2929
use crate::util::toml as cargo_toml;
3030
use crate::util::Filesystem;
3131
use crate::util::Rustc;
32-
<<<<<<< HEAD
33-
use crate::util::{paths, validate_package_name};
34-
use crate::util::{ToUrl, ToUrlWithBase};
35-
=======
36-
use crate::util::{ToUrl, ToUrlWithBase};
3732
use crate::util::{paths, validate_package_name, FileLock};
38-
>>>>>>> Make registry locking more coarse
33+
use crate::util::{ToUrl, ToUrlWithBase};
3934

4035
/// Configuration information for cargo. This is not specific to a build, it is information
4136
/// relating to cargo itself.

0 commit comments

Comments
 (0)