From 2f5a545997034c531f136e794d0f55eaa0269c07 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 11 Jun 2025 16:13:40 +0200 Subject: [PATCH] fetchToStore(): Improve caching in dry-run mode In dry-run mode, we don't need to require a valid path. --- src/libfetchers/cache.cc | 7 ++++--- src/libfetchers/fetch-to-store.cc | 2 +- src/libfetchers/include/nix/fetchers/cache.hh | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/libfetchers/cache.cc b/src/libfetchers/cache.cc index d369d213f..9e339134b 100644 --- a/src/libfetchers/cache.cc +++ b/src/libfetchers/cache.cc @@ -122,7 +122,8 @@ struct CacheImpl : Cache std::optional lookupStorePath( Key key, - Store & store) override + Store & store, + bool allowInvalid) override { key.second.insert_or_assign("store", store.storeDir); @@ -135,7 +136,7 @@ struct CacheImpl : Cache ResultWithStorePath res2(*res, StorePath(storePathS)); store.addTempRoot(res2.storePath); - if (!store.isValidPath(res2.storePath)) { + if (!allowInvalid && !store.isValidPath(res2.storePath)) { // FIXME: we could try to substitute 'storePath'. debug("ignoring disappeared cache entry '%s:%s' -> '%s'", key.first, @@ -157,7 +158,7 @@ struct CacheImpl : Cache Key key, Store & store) override { - auto res = lookupStorePath(std::move(key), store); + auto res = lookupStorePath(std::move(key), store, false); return res && !res->expired ? res : std::nullopt; } }; diff --git a/src/libfetchers/fetch-to-store.cc b/src/libfetchers/fetch-to-store.cc index d00c2edfb..9a861a11d 100644 --- a/src/libfetchers/fetch-to-store.cc +++ b/src/libfetchers/fetch-to-store.cc @@ -35,7 +35,7 @@ StorePath fetchToStore( if (!filter && (fingerprint = path.accessor->getFingerprint(path.path))) { cacheKey = makeFetchToStoreCacheKey(std::string{name}, *fingerprint, method, path.path.abs()); - if (auto res = fetchers::getCache()->lookupStorePath(*cacheKey, store)) { + if (auto res = fetchers::getCache()->lookupStorePath(*cacheKey, store, mode == FetchMode::DryRun)) { debug("store path cache hit for '%s'", path); return res->storePath; } diff --git a/src/libfetchers/include/nix/fetchers/cache.hh b/src/libfetchers/include/nix/fetchers/cache.hh index 5b9319d77..4be6b2095 100644 --- a/src/libfetchers/include/nix/fetchers/cache.hh +++ b/src/libfetchers/include/nix/fetchers/cache.hh @@ -76,11 +76,12 @@ struct Cache /** * Look up a store path in the cache. The returned store path will - * be valid, but it may be expired. + * be valid (unless `allowInvalid` is true), but it may be expired. */ virtual std::optional lookupStorePath( Key key, - Store & store) = 0; + Store & store, + bool allowInvalid = false) = 0; /** * Look up a store path in the cache. Return nothing if its TTL