Lake: facet keys in `needs` can create duplicate build jobs
Prerequisites
- [X] Check that your issue is not already filed: https://github.com/leanprover/lean4/issues
- [X] Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
- [X] Test your test case against the latest nightly release, for example on https://live.lean-lang.org/#project=lean-nightly (You can also use the settings there to switch to “Lean nightly”)
Description
When a configuration names a facet by key, for example needs = ["a:exe"], Lake can run a second job for that facet in the same build. The two jobs write the same output file. For an executable they are two concurrent links of one path; for a module they can be two concurrent lean runs on one source file.
Reading PartialBuildKey.fetchInCoreAux in src/lake/Lake/Build/Target/Fetch.lean, I see two separate causes, both in its .facet branch:
| .facet target shortFacet =>
let ⟨key, job⟩ ← PartialBuildKey.fetchInCoreAux target false
...
let job ← (job.cast h).bindM (kind := cfg.outKind) fun data =>
fetch (.facet target kind data facet)
return ⟨.facet target facet, cast (by simp) job⟩
-
The facet is stored under the unresolved key. The recursive call returns the resolved
key, which is then unused. The fetch usestarget, the key as written, sorecBuildWithIndexmemoizes the job under.facet target facet. Every other route (a command-line target, an import,LeanExe.exe,Module.facetCore) memoizes the same facet under the resolved key, such as.facet (.packageTarget pkg.keyName a) lean_exe.exe. The two keys differ whenever the written key elides its package (a:exe), names it (test/a:exe, becausekeyNameisbaseName.num wsIdx), or names a module (+Lib:c.o.noexport, because module keys are scoped by package).BuildData (.facet _ f)does not depend on the target, so the mismatch type-checks. -
The facet is fetched from an asynchronous continuation.
bindMruns here with the defaultsync := false, so each such key is fetched on a pool thread, concurrently with the continuations of the other keys in the same array. The build store is a plainIO.RefandfetchOrCreatereads and then writes it. Two continuations that reach the same key can both miss and both create the job, and the same holds for every fetch nested below it.
Context
I found this through an intermittent CI failure in a project where one executable has needs on another's exe facet and the build requests both (Lean v4.34.0, ubuntu-latest):
✖ [222/232] Building «acorn-core»:exe (216ms)
error: no such file or directory (error code: 2)
file: /home/runner/work/acorn/acorn/lean/.lake/build/bin/acorn-core
✔ [223/232] Built «acorn-core»:exe (228ms)
Both jobs appear in that project's passing builds as well as the failing one. Each one removes the output, links it and then hashes it (buildArtifactUnlessUpToDate), so one job can remove the file between the other's link and hash.
Possibly related: the reproducer of #13598 also uses a facet key in needs (@verso/+Verso.Code.External:highlighted), and #13601 addressed it by awaiting needs before header processing. A duplicate job for a module would also let mod.input.fetch return a different job from the one leanArts awaited, which is the situation #14612 describes. I have not reproduced either panic, so I am only noting the connection.
Steps to Reproduce
-
Create these four files:
lean-toolchainleanprover/lean4:nightly-2026-10-01lakefile.tomlname = "repro" [[lean_exe]] name = "a" root = "A" [[lean_exe]] name = "b" root = "B" needs = ["a:exe"]A.leandef main : IO Unit := IO.println "a"B.leandef main : IO Unit := IO.println "b" -
Run
lake build a b. This shows cause 1. -
Change the last line of
lakefile.tomltoneeds = ["a:exe", "a:exe"], then runrm -rf .lake && lake build b. This shows cause 2 on its own: both keys are identical, so they share one store key.
Expected behavior: Each build runs one a:exe job, as it does with needs = ["a"]:
✔ [1/5] Built A (482ms)
✔ [3/5] Built A:c.o (141ms)
✔ [5/9] Built a:exe (248ms)
✔ [6/9] Built B (493ms)
✔ [8/9] Built B:c.o (59ms)
✔ [9/9] Built b:exe (108ms)
Build completed successfully (9 jobs).
Actual behavior: Step 2 links a twice:
✔ [1/6] Built A (264ms)
✔ [3/6] Built A:c.o (61ms)
✔ [5/10] Built a:exe (108ms)
✔ [6/10] Built a:exe (107ms)
✔ [7/10] Built B (297ms)
✔ [9/10] Built B:c.o (57ms)
✔ [10/10] Built b:exe (105ms)
Build completed successfully (10 jobs).
Step 3 elaborates, compiles and links A twice:
✔ [1/9] Built A (369ms)
✔ [2/9] Built A (367ms)
✔ [5/9] Built A:c.o (58ms)
✔ [6/9] Built A:c.o (60ms)
✔ [8/13] Built a:exe (108ms)
✔ [9/13] Built a:exe (108ms)
✔ [10/13] Built B (225ms)
✔ [12/13] Built B:c.o (57ms)
✔ [13/13] Built b:exe (102ms)
Build completed successfully (13 jobs).
Step 2 gave this output in 10 of 10 runs, and so did step 3. With LEAN_NUM_THREADS=1, step 3 gave no duplicate job in 7 of 10 runs and only a second a:exe in the other 3. On an otherwise idle machine neither step failed for me. While another build was loading the machine, step 2 failed on a build of master (7c383e2824) with the same error as the CI run:
ℹ [5/10] Built a:exe (533ms)
✖ [6/10] Building a:exe (523ms)
error: no such file or directory (error code: 2)
file: .../repro/.lake/build/bin/a
Versions
Lean (version 4.36.0-nightly-2026-10-01, arm64-apple-darwin24.6.0, commit 77f336f7ae6a60419d3882e0d5ca7ac3a2155528, Release), macOS 27.0.
Step 2 also runs two a:exe jobs on v4.34.0 and v4.35.0-rc2, and Fetch.lean is unchanged on master at 7c383e2824.
Additional Information
The .facet branch has fetched with the unresolved target since at least #7763, so the form that elides its package is the oldest. #11500 (v4.27.0) extended the mismatch to keys that name their package.
PR #15474 addresses cause 1 by using the resolved target key when fetching the facet and returning its key. Its regression tests cover package-elided, package-named, and module facet requests in both TOML and Lean configurations. The PR does not address cause 2, the concurrent fetch/create/store behavior.
For cause 2, I would appreciate direction before proposing a change. Two options I can see are to fetch synchronously whenever the inner target's data is already known, as the facetless branch does for a configured target, or to await the inner job on the fetching thread as #13601 does for needs. BuildKey.fetchCore has the same bindM.
I used an AI coding assistant (Claude Code) to investigate this and to draft the report. The reproduction and the outputs above were run on the versions stated.
Impact
Add :+1: to issues you consider important. If others are impacted by this issue, please ask them to add :+1: to it.
plugins reaches cause 2 as well, and there it can fail builds. Each module of a library fetches the library's plugins in Module.recFetchPreSetup, through TargetArray.fetchIn and the same .facet branch, so the plugin's facet is fetched in a bindM continuation once per module. A library of N modules with plugins = ["Plug:shared"] can start up to N jobs for Plug:shared, and each of them builds Plug's modules too. dynlibs take the same route.
In a larger project (macOS, Lake v4.34.1), the duplicate jobs corrupted outputs: a plugin module's C file began with 16384 NUL bytes (warning: null character ignored, then a compile error), and a shared library was linked from an empty response file (clang: error: no input files). With one library of 16 modules loading the plugin, 3 of 3 fresh builds failed, with 7 to 9 jobs for the :shared facet.
A smaller reproduction:
lakefile.toml
name = "repro"
[[lean_lib]]
name = "Plug"
[[lean_lib]]
name = "A"
plugins = ["Plug:shared"]
Plug.lean
def plug : Nat := 0
A.lean
import A.M1
import A.M2
import A.M3
import A.M4
A/M1.lean to A/M4.lean: def m1 : Nat := 1 and so on.
rm -rf .lake && lake build A on nightly-2026-10-14 (macOS arm64):
✔ [2/32] Built Plug (1.7s)
✔ [3/32] Built Plug (1.7s)
✔ [4/32] Built Plug (1.7s)
✔ [7/32] Built Plug (1.7s)
✔ [9/32] Built Plug (1.7s)
✔ [12/32] Built Plug:c.o (1.2s)
✔ [13/32] Built Plug:c.o (1.2s)
✔ [14/32] Built Plug:c.o (1.2s)
✔ [15/32] Built Plug:c.o (1.2s)
✔ [16/32] Built Plug:c.o (1.2s)
✔ [17/32] Built Plug:shared (233ms)
✔ [18/32] Built Plug:shared (233ms)
✔ [19/32] Built Plug:shared (233ms)
✔ [20/32] Built Plug:shared (233ms)
✔ [21/32] Built Plug:shared (231ms)
✔ [22/32] Built A.M1 (358ms)
✔ [24/32] Built A.M2 (360ms)
✔ [26/32] Built A.M3 (358ms)
✔ [28/32] Built A.M4 (358ms)
✔ [30/32] Built A (181ms)
Build completed successfully (32 jobs).
In five such builds, Plug, Plug:c.o and Plug:shared each ran 3 to 5 times, and all five succeeded. In eleven more builds, with LEAN_NUM_THREADS=4, they each ran 2 to 5 times, and two of the eleven failed, each with four failed Plug:c.o and Plug:shared jobs (excerpt from one, paths shortened):
✔ [2/28] Built Plug (283ms)
✔ [4/28] Built Plug (285ms)
✔ [6/28] Built Plug (283ms)
✖ [14/28] Building Plug:c.o (55ms)
trace: .> clang -c -o .lake/build/ir/Plug.c.o.export .lake/build/ir/Plug.c ...
error: no such file or directory (error code: 2)
file: .../.lake/build/ir/Plug.c.o.export
✔ [20/28] Built Plug:c.o (55ms)
✖ [22/28] Building Plug:shared (37ms)
trace: .> clang -shared -o .lake/build/lib/librepro_Plug.dylib @.lake/build/lib/librepro_Plug.dylib.rsp
clang: error: no such file or directory: '.../.lake/build/ir/Plug.c.o.export'
Adding needs = ["Plug:shared"] to A avoids the duplicates (one job each in 5 of 5 builds), because recFetchPreSetup awaits needs before it fetches plugins (#13601).
#15696 fixes this for plugins and dynlibs, and for moreLinkObjs and moreLinkLibs where Lake links an executable or a module's shared library. There, after the module's imports have been waited for, the PR fetches Lake's own shared library, static library and object file facets in place when the job of their target has finished, as a library's always has. The facet's fetch function then runs in place, with the fetch functions it calls in place, and that can block where the fetch did not block before: code that fetches a module's presetup from a (sync := true) continuation can now panic, as code that has to await needs can since #13601, and if a job or fetch function that the plugin's build waits for depends on something that code does only after the fetch returns, the build now hangs. A package's own facets, an extern_lib's facets and a facet of a target whose job is still running, such as a custom target, are still fetched in a continuation, because their fetch functions or jobs can wait for anything, so they can still be built twice. The PR's first comment asks whether you would rather have a fix in the build store. With it, the reproduction above runs Plug, Plug:c.o and Plug:shared once each. It leaves needs as it is, because LeanLib.recBuildExtraDepTargets has no earlier wait, and the PR's first comment offers it as a follow-up. Until then, needs = ["a"] avoids the second a:exe job, since Lake fetches a target's default facet in place.