Skip to content

feat: #min_imports in the module system - #168

Merged
joneugster merged 61 commits into
leanprover-community:mainfrom
thorimur:min-imports-module
Oct 23, 2026
Merged

joneugster merged 61 commits into
leanprover-community:mainfrom
thorimur:min-imports-module

Conversation

@thorimur

@thorimur thorimur commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

This PR upgrades #min_imports to function in the module system.

It also includes an ergonomic upgrade: #min_imports can now be run from any point in the file, and will take into account the declarations in the entire file, including those which come after it. However, there is currently no indication that the command is still running until we reach the end of the file. This will be addressed in future PRs.

This also performs some misc. related changes now that the trio of #norm_imports, #min_imports and #find_home are updated

  • Update the README
  • Remove long-deprecated #minimize_imports

As well as some miscellaneous refactoring. This includes a nearby bugfix to WorkspaceModel which indirectly has to do with the Init management needed at some point by #min_imports in this PR.

@thorimur thorimur mentioned this pull request Oct 2, 2026
@thorimur
thorimur marked this pull request as ready for review October 2, 2026 23:33
@thorimur

Copy link
Copy Markdown
Collaborator Author

Okay, I've responded to Kim's helpful bot review from this gist. :) (Claude summarized the items and organized this doc, I wrote the responses (and the code!) manually.)

Fixed

168-2 · Extra module-use records do not reach import needs

Review: extraModUses records (from attribute implementations and metaprogramming hooks) never make it into the computed import needs.
Response: Fixed by taking extraModUses out of DeclNeeds (it doesn't quite belong there anyway) and, for now, simply adding these into the ImportNeeds separately.

168-3 · Wrapped invocations count the command's own syntax and duplicate imports

Review: Inside a wrapper such as #guard_msgs in, the command's own syntax is counted and ImportGraph.Tools.MinImports appears twice in the suggestion.
Response: Fixed by checking if min imports will be provided first. We don't bother to support #guard_msgs in #min_imports or set_option ... in #min_imports, since this is very rare and not worth the extra plumbing at this time.

168-4 · Unsupported non-module files continue into the rewrite

Review: Non-module files get a warning but still receive a public import suggestion, which is invalid without module.
Response: Actually, I think it's worth offering a partial modulization to promote the module system! With a warning that the file will need the user to write module and public section and so on to fix it. I now provide the partial modulization suggestion if all necessary upstream files are modules together with instructions/disclaimers, or else report the non-module necessary imports blocking modulization. Also added a TODO to allow adding the module token through ImportGraph.Imports.Pretty

168-5 · New tests are outside the test root and bypass EOF scheduling

Review: The new test files aren't imported from ImportGraphTest.lean, and they call fulfill by hand instead of exercising the real end-of-file path.
Response: Imported in ImportGraphTest. (Compiling a fixture and diffing the output to actually test that the module linter is active is really not worth it.)

168-7 · Verify tool-provider preservation across import phases

Review: Asks for a test that the suggested imports keep #min_imports available, including via transitive and meta/ordinary import combinations.
Response: Also about importing #min_imports itself. Fixed as part of 168-3.

168-8 · Diagnostic classification uses a different needs set from the replacement

Review: The "imports can be reduced" check compares against reducedNeeds alone, ignoring the preserved tool imports that the replacement includes.
Response: Revealed that Init needed to be handled in the subsumption check. Fixed by unioning with the appropriate Needs.

168-9 · Compatibility and documentation follow-ups

Review: Asks whether removing the deprecated #minimize_imports alias is intentional, and to update the docs for the new behavior and its limitations.
Response: Done.

Deliberately not addressed

168-1 · Dependencies used only by commands disappear

Review: #min_imports drops imports used only by commands such as #check, because syntax needs are attached only to declarations.
Response: This is out of scope for the current PR as it would require some hacks or extra infrastructure, and is already documented as a TODO.

168-6 · EOF messages escape #guard_msgs

Review: With end-of-file reporting, #guard_msgs in #min_imports no longer captures the diagnostic; asks for documentation and a testable or "so far" mode.
Response: Not necessary for testing, and out of scope for this PR. #min_imports so far would require gathering all of the syntax up until the current command, which is complex. I've documented the testing approach in the module docstring, but we don't need #guard_msgs to be the way to test.

@joneugster

Copy link
Copy Markdown
Member

Thanks @thorimur !

@joneugster
joneugster merged commit dccdc42 into leanprover-community:main Oct 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants