Skip to content

feat(Cache): accept downstream modules as cache get arguments for partial fetching - #45905

Open
marcelolynch wants to merge 13 commits into
leanprover-community:masterfrom
marcelolynch:cache/downstream-partial-get
Open

marcelolynch wants to merge 13 commits into
leanprover-community:masterfrom
marcelolynch:cache/downstream-partial-get

Conversation

@marcelolynch

@marcelolynch marcelolynch commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

This PR lets lake exe cache get MyProject.Basic work in a downstream project. It builds on #35415 by Jon Eugster. Without arguments, cache get gets the cache for Mathlib.

Changes to the lookup:

  • Selection: for a module outside the Mathlib cache, get selects the cache modules that it imports, also through other downstream modules, and that Mathlib also imports. CI uploads the cache only for the closure of Mathlib, and Lake builds the other modules.
  • Import graph: getHash records the imports of each visited module, so the walk continues through downstream modules. From a downstream module, it follows all imports outside Lean core. An unknown import gets a warning, and cache skips the cache modules that import it.
  • Source files: a module resolves to the first package in the search path that has its .lean file. Dependencies come first in the search path, and Mathlib has a directory DownstreamTest/. The file check resolves DownstreamTest.Partial to the downstream package.

A downstream project runs MATHLIB_NO_CACHE_ON_UPDATE=1 lake update for a partial get, because Mathlib's post_update hook gets the whole cache.

Cache/test/downstream.sh partial runs on Linux and Windows. It checks that cache get DownstreamTest.Partial downloads only the files of cache get- Mathlib.Data.Nat.Basic, and that lake build then builds only the two downstream modules. The main CI runs the existing downstream check through the same script.

🤖

`lake exe cache get MyProject.Basic` in a downstream project gets the olean
cache for the Mathlib cache modules that `MyProject.Basic` imports, also
through other modules of the downstream project. A downstream module gets
no hash, so the cache never asks for files of its own. Without arguments,
`cache get` gets the cache for `Mathlib` only.

Co-authored-by: Jon Eugster <[email protected]>
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

PR summary 5c0fcd8d0e

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff (regex)

+ HashMemo.closure
+ findLeanSource
+ findLeanSource?
+ helper
+ isInLeanCore
+ test_filterByRootModules_downstream
+ test_leanModulesFromSpec_shadowedRoot
+ two_add_two
- getFileImports

You can run this locally as follows
## from your `mathlib4` directory:
git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci

## summary with just the declaration names:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh in the mathlib-ci repository contains some details about this script.

Declarations diff (Lean)

✅ Lean-aware diff — post-build, computed from the Lean environment (commit 5c0fcd8).

  • +0 new declarations
  • −0 removed declarations

No declaration differences.


No changes to strong technical debt.
No changes to weak technical debt.

Current commit 5c0fcd8d0e
Reference commit 2e9cd83667

This script lives in the mathlib-ci repository. To run it locally, from your mathlib4 directory:

git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci
../mathlib-ci/scripts/reporting/technical-debt-metrics.py pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

⚠️ Workflow documentation reminder

This PR modifies files under .github/workflows/.
Please update docs/workflows.md if the workflow inventory, triggers, or behavior changed.

Modified workflow files:

  • .github/workflows/build_template.yml
  • .github/workflows/cache_test.yml

@github-actions github-actions Bot added the CI Modifies the continuous integration setup or other automation label Nov 11, 2026
marcelolynch and others added 9 commits November 11, 2026 12:36
…`get` end to end

`getHash` visits every module the same way: it records the imports that
`cache` follows in `depsMap`, and only a module in the Mathlib cache gets a
hash. `filterByRootModules` collects the hashes reachable from the roots.
This replaces the separate walk for downstream modules, the `cache` memo,
and `insertDeps`.

`Cache/test/downstream.sh` runs `cache get` in a downstream project that
requires the checkout, and checks that `lake build` then builds only the
downstream modules. The `cache-get-downstream` job runs it on Linux and
Windows.

Co-authored-by: Jon Eugster <[email protected]>
`findWithExtBase` returns the first entry of the search path that has the
module's file, and only falls back to a directory when no entry has the file.
Dependencies come first in the search path, so a top-level directory of a
dependency (such as Mathlib's `DownstreamTest/`) no longer hides a downstream
package with the same name. All lookups of a module's source use
`findLeanSource`.

An import of a downstream module that no package in the search path has is
skipped with a warning, and does not stop `cache get`.
… with a git dependency

The test copies `DownstreamTest/` and requires Mathlib by git from a
one-commit copy of HEAD, so Mathlib is in `.lake/packages/mathlib`, as in a
real downstream project. Lake cannot clone the shallow CI checkout itself.
The downstream `cache get` starts from an empty cache directory, and must
download as many files as `cache get- Mathlib.Data.Nat.Basic`. A second
`cache get` must download and unpack nothing. The `cache-get` job runs it.
…hange

The docstring of `getFileImports` states what happens to an unknown import
and which messages name `fileName`. The `cache-get` job comment covers the
downstream test. The messages for a downstream argument and an unknown import
name `cache` as the agent. The Lean core case of the unit test checks that
`cache` does not visit the imports of a Lean core module.
…streamTest/`

`Cache/test/downstream.sh SCENARIO` sets up `DownstreamTest/`, which requires
the checkout by path, and runs a scenario:
- `full` is the check of the main CI: `cache get`, then
  `lake build --no-build Mathlib`.
- `partial` checks `cache get DownstreamTest.Partial` against the reference
  `cache get- Mathlib.Data.Nat.Basic` in the checkout, then checks that
  `lake build` builds only the downstream modules and that a second `get`
  does nothing.

The main CI runs the script from `workflow-actions/`, so that a fork PR branch
gets the script of the workflow revision. The `cache-get` job runs `partial`
before any step unpacks Mathlib oleans.
…`Mathlib` imports

CI uploads the cache for the closure of `Mathlib`. A downstream module can
import other modules of the dependencies, and the server has no files for them.

`getHash` resolves the imports itself, `lookup` names a module without a
cache file, and the help text documents MATHLIB_NO_CACHE_ON_UPDATE.
Inline the single-use helpers in `Cache/Hashing.lean`, shorten the docs and
the help text, and keep one unit test assertion per case that the `partial`
scenario does not check. The `partial` scenario drops the second `cache get`.
@marcelolynch marcelolynch changed the title feat(Cache): get the olean cache for the imports of downstream modules feat(Cache): accept downstream modules as cache get arguments Nov 12, 2026
@marcelolynch marcelolynch changed the title feat(Cache): accept downstream modules as cache get arguments feat(Cache): accept downstream modules as cache get arguments for partial fetching Nov 12, 2026
Comment thread Cache/Hashing.lean
cache := stt.cache.insert mod (some fileHash)
depsMap := stt.depsMap.insert mod (fileImports.map (·.1)) })
let stt ← get
if stt.depsMap.contains mod then return stt.hashMap[mod]?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the imports get recorded before the hash check now. Before, depsMap was only written on the success path, so a downstream module importing another downstream module never got its imports recorded and get MyProject.Basic would have selected nothing. With depsMap as the visited marker the cache field was redundant, so we drop

@joneugster joneugster left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this now with a new repo pointing at this branch and it was working as expected!

The changes look reasonable to me (I did only skim over the changes to tests though). Please mark this as non-draft when you don't intent to make further changes :)

@marcelolynch
marcelolynch marked this pull request as ready for review November 22, 2026 20:45
@joneugster

Copy link
Copy Markdown
Member

Thanks!

maintainer merge

disclosure: As mentioned above, I did originally write a version of this about 2 years ago (originally this was #21238). While said PR was the inspiration for this PR, I do not remember or recognise much of that code, so I thought it adequate to review and test Marcelo's implementation.

@github-actions

Copy link
Copy Markdown

🚀 Pull request has been placed on the maintainer queue by joneugster.

@mathlib-triage mathlib-triage Bot added the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Nov 22, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI Modifies the continuous integration setup or other automation maintainer-merge A reviewer has approved the changed; awaiting maintainer approval.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants