Lake: per-module `*.setup.json` lists every transitive import and dominates build output on large projects

Open
#16,658 0 comments 2 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
22/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
json
Domain
build-system

Research direction

Start with the reproduction: a chain of 2,000 trivial modules where each .lake/build/ir/Chain/M<k>.setup.json has an importArts list with one entry per transitive import, so the total size grows quadratically. Look at how Lake writes this file and what the --setup consumer reads from it. Done means the setup file size stops scaling with the import closure, and the chain repro's total setup.json output is roughly proportional to the module count. Agreeing on the format change first is part of the work.

Written by the indexing model from the issue text.

Description

bug
Prerequisites
  • Check that your issue is not already filed:
    https://github.com/leanprover/lean4/issues
  • Reduce the issue to a minimal, self-contained, reproducible test case.
    Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release (4.38.0-nightly-2026-11-20, also v4.37.0-rc1).
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    N/A: not a soundness report.
Description

For each module Lake writes .lake/build/ir/<Module>.setup.json, and its importArts field has one entry per transitive import, with absolute paths. So the file grows with the module's import closure, and the total over a project grows with modules x closure size.

Context

I noticed this building a 175,187-module project on v4.33.1 (https://alpo.ge/lean.tar.gz.part01 ... part14), where most modules import Mathlib. After lake build the setup files were 983 GiB against 88 GiB of .olean files.

On a small scale: a module that only does import Mathlib gets a 6.2 MB setup file on v4.36.0 (6.4 MB on v4.37.0-rc1), next to a 5 KB .olean.

Steps to Reproduce
  1. Build a chain of 2,000 trivial modules with no dependencies, where module k imports module k-1:
mkdir repro && cd repro
echo 'leanprover/lean4:nightly' > lean-toolchain
cat > lakefile.toml <<'EOF'
name = "repro"
defaultTargets = ["Chain"]

[[lean_lib]]
name = "Chain"
EOF
mkdir Chain
printf 'def c0001 : Nat := 1\n' > Chain/M0001.lean
for k in $(seq 2 2000); do
  printf 'import Chain.M%04d\n\ndef c%04d : Nat := c%04d + 1\n' $((k-1)) $k $((k-1)) > $(printf 'Chain/M%04d.lean' $k)
done
echo 'import Chain.M2000' > Chain.lean
lake build
  1. Compare the setup files with the compiled modules:
ls -l .lake/build/ir/Chain/M2000.setup.json .lake/build/lib/lean/Chain/M2000.olean
du -cb .lake/build/ir/Chain/*.setup.json | tail -1
du -cb .lake/build/lib/lean/Chain/*.olean | tail -1

Expected behavior: build output roughly proportional to the compiled modules.

Actual behavior (nightly): each .olean is 5,976 bytes, while M2000.setup.json is 184,036 bytes (1,999 entries, about 92 bytes each). The total is quadratic in the chain length:

First n modules all setup.json all .olean
500 11.5 MB 3.0 MB
1,000 46.1 MB 6.0 MB
2,000 184.2 MB 12.0 MB

v4.37.0-rc1 behaves the same.

Versions

Lean 4.38.0-nightly-2026-11-20 (commit 82bff16a3463) and 4.37.0-rc1, Lake 5.0.0, Ubuntu 24.04 x86-64. The large project was on v4.33.1.

Additional Information

I don't know the constraints on the --setup format. Things that might work: storing a shared closure's artifact map once and referencing it, listing only direct imports, or deleting the file once the module is built.

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Dominant language
Lean
Stars
10.3k
Forks
1.1k
Avg merge
19h 8m
Merged PRs (30d)
421

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Build System issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.