Core guide
Import workflows
Preview one file, inspect the proposed imports, and apply the rewrite inside a recoverable transaction. A failing module does not leave the rest of a batch half-written.
Test one file first
Preview what lev would do to one file before applying a project-wide edit. The examples use the direct lev shake spelling, but lev inspect imports accepts the same options and produces the same lev.cli.shake/v1 report.
| Tool | What it gives you | When to use it |
|---|---|---|
lake shake | The underlying import minimization analysis from the selected Lake toolchain. | Useful when you only want the raw proposal and you already know how you will validate it. |
lev shake | The same analysis wrapped in preview, rollback, batch isolation, coverage, JSON/SARIF, and policy flags. | Use it when the repository needs explainable, reversible edits rather than a raw proposal. |
| Task | Command or flag | Result |
|---|---|---|
| Preview one module before touching source | lev shake MyProject.Proof --explain | See the exact replacement before lev writes a byte. |
| Keep good edits from a mixed batch | --isolate | A bad rewrite in one file does not have to discard independently verified edits elsewhere. |
| Prove what was actually analyzed | --coverage | Coverage turns "looks clean" into evidence about the files lev really inspected. |
| Preserve API or namespace intent | --keep-public --keep-implied | Those flags carry repository policy that compiler evidence alone cannot infer. |
Preview, check, and apply
lev shake MyProject.Proof --explain
lev shake MyProject.Proof --check
lev shake MyProject.Proof --fix
# The grouped form accepts the same options
lev inspect imports MyProject.Proof --explainMyProject/Proof.lean:
remove #[import Mathlib]
add #[import Mathlib.Data.Nat.Prime.Basic]The module names come from the project's pinned Lake and dependencies.
Previews and --check both leave source unchanged and return status 1 when drift is found. --check also labels JSON output as check mode for CI. --fix applies and validates; --apply is an equivalent spelling. --isolate turns a failing batch into smaller verification units so accepted edits do not have to be discarded with an unrelated rejected file.
The transaction boundary is the main safety property. A failed verification restores the Lean source files selected for rewriting instead of leaving a partial import edit in the branch.
$ lev inspect imports --fix
Successfully applied 1 suggestions.
lev: verifying rewritten imports
Build completed successfully.
$ lev inspect imports --fix
lev: imports are already minimal$ lev inspect imports --fix
lev: verifying rewritten imports
error: unknown constant 'Nat.prime_five'
lev: import verification failed; restoring the original Lean sources
$ echo $?
1Running the command again should produce no new edits. That fixed point is a useful check that the accepted source and the analyzer agree. On failure, lev restores the original bytes before returning, so the working tree is not left halfway through an import rewrite.
When one file rejects the batch
Suppose A.lean can use the smaller imports but B.lean relies on a namespace effect that does not appear as a declaration edge. With --isolate, lev verifies the restored baseline, tests smaller subsets, keeps A.lean, and restores B.lean.
$ lev inspect imports --fix --isolate --json > shake.json
lev: batch verification failed; isolating import edits
lev: verifying the restored baseline
lev: rejected the import rewrite for B.lean
lev: keep the dependency with `import Mathlib -- shake: keep`
$ echo $?
1{
"schema": "lev.cli.shake/v1",
"data": {
"mode": "isolate",
"status": "partially_applied",
"files": [
{
"path": "A.lean",
"disposition": "applied",
"removed_imports": [],
"added_imports": []
},
{
"path": "B.lean",
"disposition": "rejected",
"removed_imports": [],
"added_imports": [],
"keep_annotations": [
"import Mathlib -- shake: keep"
]
}
]
}
}Status 1 is intentional here: useful edits were kept, but the requested whole-project minimization did not fully succeed. Review the retained diff, add the suggested keep annotation when it matches project intent, and run the command again. The report names the file that still needs a policy decision.
| Flag | Effect |
|---|---|
--explain | Preview each proposed edit with the declaration that requires it. |
--check | Report drift without touching sources; nonzero exit when changes exist. |
--fix / --apply | Rewrite sources inside a validation transaction. |
--isolate | On failure, split the batch and keep independently verified subsets. |
--no-build | Do not automatically rebuild stale modules before analysis. |
--force | Ask Lake shake to analyze stale .olean files without its freshness check. |
A rewrite can compile and still expose the wrong public API. The next page covers the separate policy for public imports, implied imports, and prefix preservation.
Requirements and failure behavior
- The selected Lake toolchain must provide its native
shakecommand; older toolchains without it cannot run the analysis. - Analyzed files must use the Lean module system (a leading
modulecommand). Legacy files are reported, not silently skipped. - Stale
.oleanfiles are rebuilt automatically before analysis unless--no-builddisables that retry.--forceinstead asks Lake to analyze stale.oleanfiles without its freshness check. - Validation builds bypass Lake's artifact cache and end with a freshness check, so a cache hit cannot mask a missing
.olean. - If the whole-batch rewrite fails under
--isolate, lev restores and verifies the baseline first. A broken baseline leaves every file unchanged; a partial result exits with status 1 and reports candidate-- shake: keepannotations for the failing files.
Policy, coverage, and project-wide analysis
Once a one-file rewrite succeeds, set the repository policy, check analyzer coverage, and inspect the project-wide graph.
| Topic | Page or command | Contents |
|---|---|---|
| Repository import policy | Import policy and coverage | Public imports, keep annotations, implied boundaries, and coverage evidence. |
| Project module graph | lev inspect graph | Graph views, JSON output, and Graphviz DOT without crawling generated build trees. |
| Full-project results | FLT case study | A whole-project pass, the policy adjustments that mattered, and review-friendly artifacts. |