Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bust fast check cache on deno_graph version change #491

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/fast_check/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ pub struct FastCheckCacheKey(u64);
impl FastCheckCacheKey {
#[cfg(feature = "fast_check")]
pub fn build(
hash_seed: &'static str,
package_nv: &PackageNv,
entrypoints: &BTreeSet<ModuleSpecifier>,
) -> Self {
use std::hash::Hash;
use std::hash::Hasher;
let mut hasher = twox_hash::XxHash64::default();
hash_seed.hash(&mut hasher);
package_nv.hash(&mut hasher);
for value in entrypoints {
value.hash(&mut hasher);
Expand Down Expand Up @@ -83,9 +85,15 @@ pub struct FastCheckCacheModuleItemDiagnostic {
}

/// Cache for storing the results of fast checks based on a package.
///
/// Note: Implementors should bust their cache when their version changes.
pub trait FastCheckCache {
/// Seed that is provided to the hash in order to cache bust
/// it on version changes.
///
/// This defaults to the current deno_graph version.
fn hash_seed(&self) -> &'static str {
env!("CARGO_PKG_VERSION")
}

fn get(&self, key: FastCheckCacheKey) -> Option<FastCheckCacheItem>;
fn set(&self, key: FastCheckCacheKey, value: FastCheckCacheItem);
}
Expand Down
6 changes: 5 additions & 1 deletion src/fast_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,11 @@ pub fn build_fast_check_type_graph<'a>(
),
));
}
let cache_key = FastCheckCacheKey::build(&nv, &package.entrypoints);
let cache_key = FastCheckCacheKey::build(
fast_check_cache.hash_seed(),
&nv,
&package.entrypoints,
);
fast_check_cache.set(
cache_key,
FastCheckCacheItem {
Expand Down
3 changes: 2 additions & 1 deletion src/fast_check/range_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ impl<'a> PublicRangeFinder<'a> {
let Some(fast_check_cache) = &self.fast_check_cache else {
return None;
};
let cache_key = FastCheckCacheKey::build(nv, entrypoints);
let cache_key =
FastCheckCacheKey::build(fast_check_cache.hash_seed(), nv, entrypoints);
let Some(cache_item) = fast_check_cache.get(cache_key) else {
return None;
};
Expand Down
4 changes: 4 additions & 0 deletions tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ pub struct TestFastCheckCache {
}

impl FastCheckCache for TestFastCheckCache {
fn hash_seed(&self) -> &'static str {
"stable-for-tests"
}

fn get(
&self,
key: deno_graph::FastCheckCacheKey,
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/graph/fast_check/cache__basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,6 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
}

== fast check cache ==
FastCheckCacheKey(4741815020074927300):
FastCheckCacheKey(13212997092325944472):
Deps - []
Modules: [["https://jsr.io/@scope/a/1.0.0/mod.ts","info"],["https://jsr.io/@scope/a/1.0.0/a.ts","info"],["https://jsr.io/@scope/a/1.0.0/a4.ts","info"],["https://jsr.io/@scope/a/1.0.0/a6.ts","info"]]
2 changes: 1 addition & 1 deletion tests/specs/graph/fast_check/cache__diagnostic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,6 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:


== fast check cache ==
FastCheckCacheKey(4741815020074927300):
FastCheckCacheKey(13212997092325944472):
Deps - []
Modules: [["https://jsr.io/@scope/a/1.0.0/mod.ts","diagnostic"],["https://jsr.io/@scope/a/1.0.0/c.ts","diagnostic"]]
2 changes: 1 addition & 1 deletion tests/specs/graph/fast_check/cache__diagnostic_deep.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,6 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:


== fast check cache ==
FastCheckCacheKey(4741815020074927300):
FastCheckCacheKey(13212997092325944472):
Deps - []
Modules: [["https://jsr.io/@scope/a/1.0.0/mod.ts","diagnostic"],["https://jsr.io/@scope/a/1.0.0/a.ts","diagnostic"],["https://jsr.io/@scope/a/1.0.0/b.ts","diagnostic"],["https://jsr.io/@scope/a/1.0.0/c.ts","diagnostic"]]
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,6 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:


== fast check cache ==
FastCheckCacheKey(2640350593211828171):
FastCheckCacheKey(17039939699199502831):
Deps - []
Modules: [["https://jsr.io/@scope/a/1.0.0/mod.ts","diagnostic"],["https://jsr.io/@scope/a/1.0.0/b.ts","diagnostic"],["https://jsr.io/@scope/a/1.0.0/a.ts","diagnostic"]]
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ Fast check https://jsr.io/@scope/b/1.0.0/mod.ts:
}

== fast check cache ==
FastCheckCacheKey(4741815020074927300):
Deps - ["@scope/b@1.0.0"]
Modules: [["https://jsr.io/@scope/a/1.0.0/mod.ts","diagnostic"]]
FastCheckCacheKey(15065614787034843742):
FastCheckCacheKey(3278580195394936988):
Deps - []
Modules: [["https://jsr.io/@scope/b/1.0.0/mod.ts","info"]]
FastCheckCacheKey(13212997092325944472):
Deps - ["@scope/b@1.0.0"]
Modules: [["https://jsr.io/@scope/a/1.0.0/mod.ts","diagnostic"]]