Skip to content

Commit 56e199c

Browse files
committed
fix: lake: do not cache files already in the cache
1 parent 66caf8f commit 56e199c

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/lake/Lake/Build/Common.lean

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,14 @@ public def Cache.saveArtifact
453453
let hash := Hash.ofString normalized
454454
let descr := artifactWithExt hash ext
455455
let cacheFile := cache.artifactDir / descr.relPath
456-
createParentDirs cacheFile
457456
-- make the local file unwritable where possible to discourage users from
458457
-- writing to such paths as this can corrupt the cache if the file was hard linked
459458
let r := {read := true, write := false, execution := false}
460459
IO.setAccessRights file ⟨r, r, r⟩
461-
IO.FS.writeFile cacheFile normalized
462-
IO.setAccessRights cacheFile ⟨r, r, r⟩
460+
unless (← cacheFile.pathExists) do
461+
createParentDirs cacheFile
462+
IO.FS.writeFile cacheFile normalized
463+
IO.setAccessRights cacheFile ⟨r, r, r⟩
463464
writeFileHash file hash
464465
let mtime := (← getMTime cacheFile |>.toBaseIO).toOption.getD 0
465466
let path := if useLocalFile then file else cacheFile
@@ -469,14 +470,15 @@ public def Cache.saveArtifact
469470
let hash := Hash.ofByteArray contents
470471
let descr := artifactWithExt hash ext
471472
let cacheFile := cache.artifactDir / descr.relPath
472-
createParentDirs cacheFile
473473
-- make the local file unwritable where possible to discourage users from
474474
-- writing to such paths as this can corrupt the cache if the file is hard linked
475475
let r := {read := true, write := false, execution := exe}
476476
IO.setAccessRights file ⟨r, r, r⟩
477-
if let .error _ ← (IO.FS.hardLink file cacheFile).toBaseIO then
478-
IO.FS.writeBinFile cacheFile contents
479-
IO.setAccessRights cacheFile ⟨r, r, r⟩
477+
unless (← cacheFile.pathExists) do
478+
createParentDirs cacheFile
479+
if let .error _ ← (IO.FS.hardLink file cacheFile).toBaseIO then
480+
IO.FS.writeBinFile cacheFile contents
481+
IO.setAccessRights cacheFile ⟨r, r, r⟩
480482
writeFileHash file hash
481483
let mtime := (← getMTime cacheFile |>.toBaseIO).toOption.getD 0
482484
let path := if useLocalFile then file else cacheFile

tests/lake/tests/cache/test.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ test_run exe test
9090
# The `Test` module's artifacts are more carefully managed throught this test
9191
touch Ignored.lean
9292
test_run -v build +Ignored
93-
test_cmd rm -f .lake/build/lib/lean/Ignored.trace
9493

9594
# Verify that fetching from the cache can be disabled
9695
test_cmd rm -f .lake/build/lib/lean/Ignored.trace
@@ -159,6 +158,10 @@ test_out "Fetched Test:c.o" build +Test:o -v --no-build
159158
test_cmd rm -rf "$CACHE_DIR/outputs" .lake/build/ir/Test.c
160159
test_run -v build +Test:c --no-build
161160

161+
# Verify that Lake does not attempt overwrite an existing artifact
162+
test_cmd rm -rf "$CACHE_DIR/outputs" .lake/build/lib/lean/Ignored.trace
163+
test_run -v build +Ignored
164+
162165
# Verify that the olean does not need to be present in the build directory
163166
test_cmd rm -f .lake/build/lib/lean/Test.olean .lake/build/lib/lean/Test/Imported.olean
164167
test_run -v build +Test.Imported --no-build --wfail

0 commit comments

Comments
 (0)