Repository navigation
Conversation
PR summary d9a435d835
|
| File | Base Count | Head Count | Change |
|---|---|---|---|
| Mathlib.Tactic.Common | 376 | 377 | +1 (+0.27%) |
Import changes for all files
| Files | Import difference |
|---|---|
| ../mathlib-ci/scripts/pr_summary/import_trans_difference.sh all | |
| There are 8607 files with changed transitive imports taking up over 375923 characters: this is too many to display! | |
You can run this locally from your mathlib4 directory: |
git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci
Declarations diff (regex)
+ formatImports
- ImportRef
- ImportRef.getIdent
- headerToImportRefs
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
d9a435d).
- +1 new declarations
- −0 removed declarations
+Mathlib.Linter.linter.style.header.importsNo changes to strong technical debt.
No changes to weak technical debt.
Current commit d9a435d835
Reference commit 499b0ec11b
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
relativevalue is the weighted sum of the differences with weight given by the inverse of the current value of the statistic. - The
absolutevalue is therelativevalue divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).
| let some (lastImportRef, _) := imports.back? | return | ||
| let impsRef := mkNullNode <| imports.map (·.1.stx) | ||
| let some (msg, errs) ← liftCoreM <| | ||
| Import.mkImportSuggestionMessage impsRef (imports.map (·.1.toImport)) imports |
There was a problem hiding this comment.
Could we validate the proposed replacement before offering the code action? A multiline block comment starting after an import can be split when whitespace is reassigned, even when errs.isEmpty is true.
For example, formatting this header:
module
import B /- trailing
continued -/
public import Acurrently produces:
module
continued -/
public import A
import B /- trailingI reproduced this against ImportGraph's merged main at d058217, using the same headerToImportRefsWithWhitespace / prettyWithSourceWhitespace path used by mkImportSuggestionMessage. The formatter reports FormatErrors.isEmpty: true: the opening /- trailing stays with import B, while continued -/ moves with public import A. Applying the suggestion therefore corrupts a valid file. Returning early only when errs is nonempty would not catch it.
A defensive check would apply the candidate edit to the source, reparse the header, and require that it contains exactly the expected imports in the intended order, with all modifiers preserved. Checking only the parser's error log is insufficient: in this example parseHeader returns no errors but an empty import list because continued ends the header. If validation fails, we can still warn about formatting, but omit the automatic edit. Ideally the comment handling itself is fixed in ImportGraph too.
Could we add this as a regression test? #44503 already checks the reparsed import list before applying automatic fixes.
There was a problem hiding this comment.
Nice catch! I'll address this when I'm back from PTO, but I wanted to mention that I suspect this also uncovers an issue in core's Lean.Syntax.updateLeading(Aux), on which this formatting/whitespace-splitting relies! 👀
(Or maybe it's just an API gap. It's possible that updateLeading is not even attempting to have well-formed whitespace, and is indeed just looking for that trailing newline.)
There was a problem hiding this comment.
My feeling is that while this is important, it is also the case that as far as this PR goes, it is more important to keep Mathlib from drifting from the import style convention than to wait for the necessary upstream (ImportGraph) changes to fix this, especially given that it's likely quite a rare occurrence. (Nor is it worth the code complexity that would be necessary to address it here.)
I do think this should be fixed in import graph, though; I now have some not-yet-PR'd code that performs the defensive reparsing. Parsing the comment itself is a bit finicky but there are options I'm considering :) In the meantime if you'd like to make an issue on import graph, you could assign me to it! :)
But, all in all, I think the benefit is worth leaving this edge case is unhandled in this PR.
This PR formats imports throughout Mathlib by grouping imports into visibility blocks separated by a newline (`public import`/`import`/`import all`), then sorting within each block by `meta`/(non-meta), and then sorting alphabetically. This uses `scripts/runSkimmer.sh` to apply the suggestions generated in #44507. (Files outside of `Mathlib/` are not touched.) This was discussed among reviewers in the mathlib reviewers channel on Zulip in the topic "linting imports", as well as incidentally in the latter part of [this topic](https://leanprover.zulipchat.com/#narrow/channel/144837-PR-reviews/topic/chore.3A.20demote.20public.20imports.20.2343918/with/625621866). (Note: this does not format Mathlib.Tactic.Common yet, since that file makes deliberate choices about import ordering.) (Note for regeneration purposes: this technically used a transient skimmer lakefile dependency and `lake build Mathlib:applyCurrentTryThis` instead of the script per se, due to the header linter's sensitivity to the package directory and implementation details of the `runSkimmer` script. This may be fixed in the future.) Co-authored-by: Kim Morrison <[email protected]>
|
This pull request has conflicts, please merge |
5e215d8 to
a7a63b1
Compare
|
!bench |
|
Benchmark results for 60af689 against fad3406 are in. There are significant results. @thorimur
Small changes (43✅, 104🟥)
|
|
There's one more drifting file. I'll take a first review look (hopefully) tomorrow. Thanks for automating away the bikeshedding! |
|
Okay, I've split off all the new reformatting into the dependent PR #45071. |
|
This PR/issue depends on: |
|
bors d+ |
|
✌️ thorimur can now approve this pull request until 2026-11-03 16:39 UTC (in 2 weeks). To approve and merge, reply with
|
|
bors r+ |
…he style convention (#44507) This PR enables the header linter to reformat imports to match the style guide. It logs an interactive suggestion in the event that imports are not formatted correctly. (The try-this suggestion preserves comments above and inline with each import.) This is only active in the module system, and currently only for files in `Mathlib` (and test files). This linter is on by default whenever `linter.style.header` is on, but may be turned off separately from the rest of the header linter by writing `set_option linter.style.header.imports false` just after the import block. (Note: while we're here, we also allow `linter.style.header.license` to be set just after the import block for consistency.) The suggestion does *not* "normalize" imports as `#norm_imports` does, i.e. eliminate redundancy among imports. It only makes sure that the imports are ordered and grouped correctly. This also fixes some unformatted imports that crept in since #44513.
|
Pull request successfully merged into master. Build succeeded: |
This PR modifies the way the header linter finds the library root from the current module. Instead of assuming that the current working directory contains the root, we peel off components from the current filename and match them against the current module name until we reach the root. We also adjust the directory dependency linter to take in a `srcDir` when locating `Mathlib.Init` and `scripts/forbiddenDirs.json`, which we provide as the parent of this filepath. This is both more principled and enables us to apply header linter suggestions from skimmer, which is started from `scripts/`. It also has the effect of (correctly) running these linters when building mathlib as an upstream dependency, just as we run mathlib's other linters in the same circumstance. Note that the `scripts/` folder is distributed along with the source files, so we are still able to locate `scripts/forbiddenDirs.json` successfully. (This was tested locally with a minimal downstream project.) This was used in conjunction with #44507 to create #45071. (This PR also adjusts the style of some nearby but unrelated `MessageData` handling.)
This PR enables the header linter to reformat imports to match the style guide. It logs an interactive suggestion in the event that imports are not formatted correctly. (The try-this suggestion preserves comments above and inline with each import.)
This is only active in the module system, and currently only for files in
Mathlib(and test files).This linter is on by default whenever
linter.style.headeris on, but may be turned off separately from the rest of the header linter by writingset_option linter.style.header.imports falsejust after the import block. (Note: while we're here, we also allowlinter.style.header.licenseto be set just after the import block for consistency.)The suggestion does not "normalize" imports as
#norm_importsdoes, i.e. eliminate redundancy among imports. It only makes sure that the imports are ordered and grouped correctly.This also fixes some unformatted imports that crept in since #44513.
Imports.Prettyimport-graph#171