Skip to content

Commit 39c26fc

Browse files
authored
feat: lake: disabling the artifact cache also disables fetching (#12300)
This PR makes disabling the artifact cache (e.g., via `LAKE_ARTIFACT_CACHE=false` or `enableArtifactCache = false`) now stop Lake from fetching from the cache (whereas it previously only stopped writing to it). There are now 3 possible configuration of the local artifact cache for a package: * `true`: Artifacts will be fetched from the cache before building (if available) and built artifacts will be cached. * `false:`: Lake will neither fetch artifacts from the cache or store them into it. * **default** (no configuration set): Lake will fetch artifacts from the cache but not store them into it. A key motivation for this is to, by default, reuse artifacts downloaded into the cache from a remote service.
1 parent f2ed53a commit 39c26fc

File tree

12 files changed

+67
-21
lines changed

12 files changed

+67
-21
lines changed

src/lake/Lake/Build/Common.lean

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ in either the saved trace file or in the cached input-to-content mapping.
497497
(inputHash : Hash) (savedTrace : SavedTrace)
498498
(cache : Cache) (pkg : Package)
499499
: JobM (Option α) := do
500-
let updateCache ← pkg.isArtifactCacheEnabled
500+
let updateCache ← pkg.isArtifactCacheWritable
501501
if let some out ← cache.readOutputs? pkg.cacheScope inputHash then
502502
match (← resolveOutputs? out) with
503503
| .ok arts =>
@@ -577,7 +577,7 @@ public def buildArtifactUnlessUpToDate
577577
else
578578
return some art
579579
let art ← id do
580-
if (← pkg.isArtifactCacheEnabled) then
580+
if (← pkg.isArtifactCacheWritable) then
581581
let restore := restore || pkg.restoreAllArtifacts
582582
if let some art ← fetchArt? restore then
583583
return art
@@ -593,9 +593,10 @@ public def buildArtifactUnlessUpToDate
593593
computeArtifact file ext text
594594
else if (← savedTrace.replayIfUpToDate file depTrace) then
595595
computeArtifact file ext text
596-
else if let some art ← fetchArt? (restore := true) then
597-
return art
598596
else
597+
if (← pkg.isArtifactCacheReadable) then
598+
if let some art ← fetchArt? (restore := true) then
599+
return art
599600
doBuild depTrace traceFile
600601
if let some outputsRef := pkg.outputsRef? then
601602
outputsRef.insert inputHash art.descr

src/lake/Lake/Build/Module.lean

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ private def Module.recBuildLean (mod : Module) : FetchM (Job ModuleOutputArtifac
761761
else
762762
some <$> mod.restoreNeededArtifacts arts
763763
let arts ← id do
764-
if (← mod.pkg.isArtifactCacheEnabled) then
764+
if (← mod.pkg.isArtifactCacheWritable) then
765765
if let some arts ← fetchArtsFromCache? mod.pkg.restoreAllArtifacts then
766766
return arts
767767
else
@@ -776,9 +776,10 @@ private def Module.recBuildLean (mod : Module) : FetchM (Job ModuleOutputArtifac
776776
mod.computeArtifacts setup.isModule
777777
else if (← savedTrace.replayIfUpToDate (oldTrace := srcTrace.mtime) mod depTrace) then
778778
mod.computeArtifacts setup.isModule
779-
else if let some arts ← fetchArtsFromCache? true then
780-
return arts
781779
else
780+
if (← mod.pkg.isArtifactCacheReadable) then
781+
if let some arts ← fetchArtsFromCache? true then
782+
return arts
782783
mod.buildLean depTrace srcFile setup
783784
if let some ref := mod.pkg.outputsRef? then
784785
ref.insert inputHash arts.descrs

src/lake/Lake/Build/Run.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def Workspace.saveOutputs
263263
[logger : MonadLog BaseIO] (ws : Workspace)
264264
(out : IO.FS.Stream) (outputsFile : FilePath) (isVerbose : Bool)
265265
: BaseIO Unit := do
266-
unless ws.isRootArtifactCacheEnabled do
266+
unless ws.isRootArtifactCacheWritable do
267267
logWarning s!"{ws.root.prettyName}: \
268268
the artifact cache is not enabled for this package, so the artifacts described \
269269
by the mappings produced by `-o` will not necessarily be available in the cache."

src/lake/Lake/Config/Monad.lean

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,26 @@ public def getArtifact? [Bind m] [MonadLiftT BaseIO m] (descr : ArtifactDescr) :
171171
getLakeCache >>= (·.getArtifact? descr)
172172

173173
/--
174-
Returns whether the package the artifact cache is enabled for the package.
174+
Returns whether the package should store its artifacts in the Lake artifact cache.
175175
176176
If the package has not configured the artifact cache itself through
177177
{lean}`Package.enableArtifactCache?`, this will default to the workspace configuration.
178178
-/
179-
public def Package.isArtifactCacheEnabled [MonadWorkspace m] (self : Package) : m Bool :=
180-
(self.enableArtifactCache?.getD ·.enableArtifactCache) <$> getWorkspace
179+
@[inline] public def Package.isArtifactCacheReadable [MonadWorkspace m] (self : Package) : m Bool :=
180+
(self.enableArtifactCache? <|> ·.enableArtifactCache? |>.getD true) <$> getWorkspace
181+
182+
/--
183+
Returns whether the package should restore its artifacts from the Lake artifact cache.
184+
185+
If the package has not configured the artifact cache itself through
186+
{lean}`Package.enableArtifactCache?`, this will default to the workspace configuration.
187+
-/
188+
@[inline] public def Package.isArtifactCacheWritable [MonadWorkspace m] (self : Package) : m Bool :=
189+
(self.enableArtifactCache? <|> ·.enableArtifactCache? |>.getD false) <$> getWorkspace
190+
191+
@[inherit_doc isArtifactCacheWritable, deprecated isArtifactCacheWritable (since := "2026-02-03")]
192+
public abbrev Package.isArtifactCacheEnabled [MonadWorkspace m] (self : Package) : m Bool :=
193+
self.isArtifactCacheWritable
181194

182195
end
183196

src/lake/Lake/Config/PackageConfig.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,9 @@ public configuration PackageConfig (p : Name) (n : Name) extends WorkspaceConfig
292292
scripts that rely on specific location of artifacts may wish to disable this feature.
293293
294294
If `none` (the default), this will fallback to (in order):
295-
* The `LAKE_ARTIFACT_CACHE` environment variable (if set)
296-
* The workspace root's `enableArtifactCache` configuration (if set and this package is a dependency)
297-
* Lake's default: `false`
295+
* The `LAKE_ARTIFACT_CACHE` environment variable (if set).
296+
* The workspace root's `enableArtifactCache` configuration (if set and this package is a dependency).
297+
* **Lake's default**: The package can use artifacts from the cache, but cannot write to it.
298298
-/
299299
enableArtifactCache?, enableArtifactCache : Option Bool := none
300300

src/lake/Lake/Config/Workspace.lean

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,22 @@ namespace Workspace
8787
self.root.lakeDir
8888

8989
/-- Whether the Lake artifact cache should be enabled by default for packages in the workspace. -/
90+
@[inline] public def enableArtifactCache? (ws : Workspace) : Option Bool :=
91+
ws.lakeEnv.enableArtifactCache? <|> ws.root.enableArtifactCache?
92+
93+
/-- Whether the Lake artifact cache should be enabled by default for packages in the workspace. -/
94+
@[deprecated enableArtifactCache? (since := "2026-02-03")]
9095
public def enableArtifactCache (ws : Workspace) : Bool :=
91-
ws.lakeEnv.enableArtifactCache? <|> ws.root.enableArtifactCache? |>.getD false
96+
ws.enableArtifactCache?.getD false
97+
98+
/-- Whether the Lake artifact cache should is enabled for workspace's root package. -/
99+
public def isRootArtifactCacheWritable (ws : Workspace) : Bool :=
100+
ws.enableArtifactCache?.getD false
92101

93102
/-- Whether the Lake artifact cache should is enabled for workspace's root package. -/
94-
public def isRootArtifactCacheEnabled (ws : Workspace) : Bool :=
95-
ws.root.enableArtifactCache? <|> ws.lakeEnv.enableArtifactCache? |>.getD false
103+
@[deprecated isRootArtifactCacheWritable (since := "2026-02-03")]
104+
public abbrev isRootArtifactCacheEnabled (ws : Workspace) : Bool :=
105+
ws.isRootArtifactCacheWritable
96106

97107
/-- The path to the workspace's remote packages directory relative to {lean}`dir`. -/
98108
@[inline] public def relPkgsDir (self : Workspace) : FilePath :=
@@ -302,7 +312,7 @@ to run executables.
302312
public def augmentedEnvVars (self : Workspace) : Array (String × Option String) :=
303313
let vars := self.lakeEnv.baseVars ++ #[
304314
("LAKE_CACHE_DIR", some self.lakeCache.dir.toString),
305-
("LAKE_ARTIFACT_CACHE", toString self.enableArtifactCache),
315+
("LAKE_ARTIFACT_CACHE", if let some b := self.enableArtifactCache? then toString b else ""),
306316
("LEAN_PATH", some self.augmentedLeanPath.toString),
307317
("LEAN_SRC_PATH", some self.augmentedLeanSrcPath.toString),
308318
-- Allow the Lean version to change dynamically within core

src/lake/schemas/lakefile-toml-schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@
464464
},
465465
"enableArtifactCache": {
466466
"type": "boolean",
467-
"description": "Whether to enables Lake's local, offline artifact cache for the package.\n\nArtifacts (i.e., build products) of packages will be shared across local copies by storing them in a cache associated with the Lean toolchain.\nThis can significantly reduce initial build times and disk space usage when working with multiple copies of large projects or large dependencies.\n\nAs a caveat, build targets which support the artifact cache will not be stored in their usual location within the build directory. Thus, projects with custom build scripts that rely on specific location of artifacts may wish to disable this feature.\n\nIf not set, If `none` (the default), this will fallback to (in order):\n* The `LAKE_ARTIFACT_CACHE` environment variable (if set)\n* The workspace root's `enableArtifactCache` configuration (if set and this package is a dependency)\n* Lake's default: `false`"
467+
"description": "Whether to enables Lake's local, offline artifact cache for the package.\n\nArtifacts (i.e., build products) of packages will be shared across local copies by storing them in a cache associated with the Lean toolchain.\nThis can significantly reduce initial build times and disk space usage when working with multiple copies of large projects or large dependencies.\n\nAs a caveat, build targets which support the artifact cache will not be stored in their usual location within the build directory. Thus, projects with custom build scripts that rely on specific location of artifacts may wish to disable this feature.\n\nIf `none` (the default), this will fallback to (in order):\n* The `LAKE_ARTIFACT_CACHE` environment variable (if set).\n* The workspace root's `enableArtifactCache` configuration (if set and this package is a dependency).\n* **Lake's default**: The package can use artifacts from the cache, but cannot write to it."
468468
},
469469
"restoreAllArtifacts": {
470470
"type": "boolean",

tests/lake/tests/cache/disabled.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ enableArtifactCache = false
33

44
[[lean_lib]]
55
name = "Test"
6+
7+
[[lean_lib]]
8+
name = "Ignored"

tests/lake/tests/cache/test.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env bash
22
source ../common.sh
3+
NO_BUILD_CODE=3
34

45
./clean.sh
56

@@ -83,8 +84,19 @@ check_diff /dev/null <(ls -1 "$CACHE_DIR/*.hash" 2>/dev/null)
8384
# Verify that the executable has the right permissions to be run
8485
test_run exe test
8586

86-
# Verify that fetching from the cache creates a trace file that does not replay
87+
# Create a test module that can be arbitrarily edited and cached
88+
# The `Test` module's artifacts are more carefully managed throught this test
8789
touch Ignored.lean
90+
test_run -v build +Ignored
91+
test_cmd rm -f .lake/build/lib/lean/Ignored.trace
92+
93+
# Verify that fetching from the cache can be disabled
94+
test_cmd rm -f .lake/build/lib/lean/Ignored.trace
95+
test_status $NO_BUILD_CODE -v -f disabled.toml build +Ignored --no-build
96+
LAKE_ARTIFACT_CACHE=false test_status $NO_BUILD_CODE -v \
97+
-f unset.toml build +Ignored --no-build
98+
99+
# Verify that fetching from the cache creates a trace file that does not replay
88100
test_out "Fetched Ignored" -v build +Ignored
89101
test_exp -f .lake/build/lib/lean/Ignored.trace
90102
test_out "Fetched Ignored" -v build +Ignored

tests/lake/tests/cache/unset.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ name = "test"
22

33
[[lean_lib]]
44
name = "Test"
5+
6+
[[lean_lib]]
7+
name = "Ignored"

0 commit comments

Comments
 (0)