Skip to content

Commit 9dd09aa

Browse files
committed
feat: --keep-local
1 parent e5de77e commit 9dd09aa

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

src/lake/Lake/CLI/Main.lean

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public structure LakeOptions where
6565
outFormat : OutFormat := .text
6666
offline : Bool := false
6767
outputsFile? : Option FilePath := none
68+
overwriteInputs : Bool := true
6869
forceDownload : Bool := false
6970
mappingsOnly : Bool := false
7071
service? : Option String := none
@@ -285,6 +286,7 @@ def lakeLongOption : (opt : String) → CliM PUnit
285286
| "--offline" => modifyThe LakeOptions ({· with offline := true})
286287
| "--wfail" => modifyThe LakeOptions ({· with failLv := .warning})
287288
| "--iofail" => modifyThe LakeOptions ({· with failLv := .info})
289+
| "--keep-local" => modifyThe LakeOptions ({· with overwriteInputs := false})
288290
| "--force-download" => modifyThe LakeOptions ({· with forceDownload := true})
289291
| "--download-arts" => modifyThe LakeOptions ({· with mappingsOnly := false})
290292
| "--mappings-only" => modifyThe LakeOptions ({· with mappingsOnly := true})
@@ -485,7 +487,7 @@ protected def get : CliM PUnit := do
485487
else
486488
return ws.defaultCacheService
487489
let map ← CacheMap.load file
488-
cache.writeMap ws.root.cacheScope map service.name? (some remoteScope)
490+
cache.writeMap ws.root.cacheScope map service.name? (some remoteScope) opts.overwriteInputs
489491
let descrs ← map.collectOutputDescrs
490492
service.downloadArtifacts descrs cache remoteScope opts.forceDownload
491493
else
@@ -530,7 +532,7 @@ protected def get : CliM PUnit := do
530532
return map
531533
else
532534
findOutputs cache service pkg remoteScope opts platform toolchain
533-
cache.writeMap pkg.cacheScope map service.name? (some remoteScope)
535+
cache.writeMap pkg.cacheScope map service.name? (some remoteScope) opts.overwriteInputs
534536
unless opts.mappingsOnly do
535537
let descrs ← map.collectOutputDescrs
536538
service.downloadArtifacts descrs cache remoteScope opts.forceDownload
@@ -544,7 +546,7 @@ protected def get : CliM PUnit := do
544546
let toolchain := cacheToolchain pkg toolchain
545547
try
546548
let map ← findOutputs cache service pkg remoteScope opts platform toolchain
547-
cache.writeMap pkg.cacheScope map service.name? (some remoteScope)
549+
cache.writeMap pkg.cacheScope map service.name? (some remoteScope) opts.overwriteInputs
548550
unless opts.mappingsOnly do
549551
let descrs ← map.collectOutputDescrs
550552
service.downloadArtifacts descrs cache remoteScope opts.forceDownload
@@ -677,7 +679,7 @@ protected def add : CliM PUnit := do
677679
error (serviceNotFound service ws.lakeConfig.config.cache.services)
678680
return some (.ofString service)
679681
let map ← CacheMap.load file
680-
ws.lakeCache.writeMap localScope map service? opts.scope?
682+
ws.lakeCache.writeMap localScope map service? opts.scope? opts.overwriteInputs
681683

682684
private def stagingOutputsFile := "outputs.jsonl"
683685

@@ -754,7 +756,7 @@ protected def unstage : CliM PUnit := do
754756
unless ok do
755757
logError "failed to copy all outputs to the staging directory"
756758
exit 1
757-
ws.lakeCache.writeMap localScope map service? opts.scope?
759+
ws.lakeCache.writeMap localScope map service? opts.scope? opts.overwriteInputs
758760

759761
protected def putStaged : CliM PUnit := do
760762
processOptions lakeOption

src/lake/Lake/Config/Cache.lean

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,22 +434,28 @@ public def getArtifact (cache : Cache) (descr : ArtifactDescr) : EIO String Arti
434434
def writeOutputsCore
435435
(cache : Cache) (scope : String) (inputHash : Hash) (out : Json)
436436
(service? : Option CacheServiceName) (remoteScope? : Option CacheServiceScope)
437+
(overwrite : Bool)
437438
: IO Unit := do
438439
let file := cache.outputsFile scope inputHash
439440
createParentDirs file
440441
let out := {service?, scope? := remoteScope?, data := out : CacheOutput}
441-
IO.FS.writeFile file (toJson out).pretty
442+
let contents := (toJson out).pretty
443+
if overwrite then
444+
IO.FS.writeFile file contents
445+
else
446+
writeFileIfNew file contents
442447

443448
/-- Cache the outputs corresponding to the given input for the package. -/
444449
@[inline] public def writeOutputs
445-
[ToJson α] (cache : Cache) (scope : String) (inputHash : Hash) (outputs : α)
446-
: IO Unit := cache.writeOutputsCore scope inputHash (toJson outputs) none none
450+
[ToJson α] (cache : Cache) (scope : String) (inputHash : Hash) (outputs : α) (update := true)
451+
: IO Unit := cache.writeOutputsCore scope inputHash (toJson outputs) none none update
447452

448453
/-- Cache the input-to-outputs mappings from a `CacheMap`. -/
449454
public def writeMap
450455
(cache : Cache) (scope : String) (map : CacheMap)
451456
(service? : Option CacheServiceName := none) (remoteScope? : Option CacheServiceScope := none)
452-
: IO Unit := map.forM fun i e => cache.writeOutputsCore scope i e.out service? remoteScope?
457+
(overwrite := true)
458+
: IO Unit := map.forM fun i e => cache.writeOutputsCore scope i e.out service? remoteScope? overwrite
453459

454460
/-- Retrieve the cached outputs corresponding to the given input for the package (if any). -/
455461
public def readOutputs? (cache : Cache) (scope : String) (inputHash : Hash) : LogIO (Option CacheOutput) := do

tests/lake/tests/ltar/test.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ test_run -f restoreAll.toml build +Test -v --wfail -o .lake/outputs.jsonl
5454
test_cmd ls .lake/cache/artifacts/*.ltar
5555
test_exp -f .lake/build/ir/Test.ltar
5656

57+
# Test that the cache input-to-output mapping is overwritten without
58+
# `--keep-local`. Since the build mapping includes module outputs and the
59+
# tracked mapping only includes the `ltar`, the tracking mapping should
60+
# require an unpack, whereas the build mapping should not.
61+
test_run cache add .lake/outputs.jsonl --keep-local
62+
LAKE_ARTIFACT_CACHE=true test_not_out "leantar" build +Test --no-build -v
63+
test_run cache add .lake/outputs.jsonl
64+
LAKE_ARTIFACT_CACHE=true test_out "leantar" build +Test --no-build -v
65+
5766
# Test producing an `ltar` without already restored artifacts
5867
rm -rf .lake/cache .lake/build
5968
LAKE_ARTIFACT_CACHE=true test_run build +Test -v

0 commit comments

Comments
 (0)