diff --git a/src/cache.rs b/src/cache.rs index 753137a00f..b6854f76e7 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -12,7 +12,7 @@ impl Cache { &self, config: &Config, key: CacheKey, - outputs: &BTreeMap, + outputs: BTreeMap, ) -> RunResult<'static, CacheStatus> { let hash = Hash::json(&key).map_err(|source| Error::CacheKeySerialize { source })?; @@ -47,6 +47,7 @@ impl Cache { let Some(entry) = CacheEntry::load(&file, &path)? else { return Ok(CacheStatus::Miss(CacheLock { file, + outputs, path, recipe: key.recipe.clone(), })); @@ -58,11 +59,12 @@ impl Cache { .map(|(name, output)| (name, Some(output))) .collect(); - if Self::outputs(outputs)? == expected { + if Self::outputs(&outputs)? == expected { Ok(CacheStatus::Hit) } else { Ok(CacheStatus::Miss(CacheLock { file, + outputs, path, recipe: key.recipe.clone(), })) diff --git a/src/cache_lock.rs b/src/cache_lock.rs index f80232906f..8734775d0b 100644 --- a/src/cache_lock.rs +++ b/src/cache_lock.rs @@ -2,12 +2,24 @@ use super::*; pub(crate) struct CacheLock { pub(crate) file: File, + pub(crate) outputs: BTreeMap, pub(crate) path: Utf8PathBuf, pub(crate) recipe: Modulepath, } impl CacheLock { - pub(crate) fn save(mut self, outputs: BTreeMap) -> RunResult<'static> { + pub(crate) fn save(mut self, recipe: &str) -> RunResult<'_> { + let outputs = Cache::outputs(&self.outputs)? + .into_iter() + .map(|(name, output)| match output { + Some(output) => Ok((name, output)), + None => Err(Error::CacheOutputMissing { + recipe, + output: name, + }), + }) + .collect::>>()?; + let context = |source| Error::FilesystemIo { source, path: self.path.clone(), diff --git a/src/recipe.rs b/src/recipe.rs index 287cc8eb3b..1175a8c4cc 100644 --- a/src/recipe.rs +++ b/src/recipe.rs @@ -567,7 +567,7 @@ impl<'src> Recipe<'src> { } }); - let (cache_lock, outputs) = if !config.no_cache + let cache_lock = if !config.no_cache && let Some(Attribute::Cache { environment: environment_attribute, extra, @@ -651,7 +651,7 @@ impl<'src> Recipe<'src> { working_directory: Some(&working_directory), }; - let lock = match cache.status(config, key, &outputs)? { + let lock = match cache.status(config, key, outputs)? { CacheStatus::Hit => { if config.verbosity.loquacious() { eprintln!( @@ -669,9 +669,9 @@ impl<'src> Recipe<'src> { CacheStatus::Miss(lock) => lock, }; - (Some(lock), outputs) + Some(lock) } else { - (None, BTreeMap::new()) + None }; let tempdir = context.tempdir(self)?; @@ -740,18 +740,9 @@ impl<'src> Recipe<'src> { } } - let outputs = Cache::outputs(&outputs)? - .into_iter() - .map(|(name, output)| match output { - Some(output) => Ok((name, output)), - None => Err(Error::CacheOutputMissing { - recipe: self.name(), - output: name, - }), - }) - .collect::>>()?; - - cache_lock.map(|lock| lock.save(outputs)).transpose()?; + if let Some(cache_lock) = cache_lock { + cache_lock.save(self.name())?; + } Ok(()) }