Skip to content

[Merged by Bors] - feat: rewrite file length check in Lean#13620

Closed
grunweg wants to merge 9 commits intomasterfrom
MR-rewrite-file-length-check
Closed

[Merged by Bors] - feat: rewrite file length check in Lean#13620
grunweg wants to merge 9 commits intomasterfrom
MR-rewrite-file-length-check

Conversation

@grunweg
Copy link
Copy Markdown
Contributor

@grunweg grunweg commented Jun 7, 2024

This is the first in a series of PRs rewriting most checks from lint-style.py in Lean.
This PR sets up the basic infrastructure and ports the check for files longer than 1500 lines.
This includes parsing style-exceptions.txt.

To enable a gradual conversion of the linters, this PR adds a new executable lint_style (which runs the rewritten linters), and has lint-style.sh call this one also.


  • this currently reads the files to lint from {Archive,Counterexamples,Mathlib}.lean: should this traverse the file system (like mk_all) instead? My reasoning is that these files are ensured to be up-to-date in CI, so in practice this amounts to similar results --- and reading one file only is easier and slightly less I/O.

This changes the ordering of the output lines in style-exceptions.txt: the ones on the file length come last.
I think this is in fact more readable in practice, given that there are only about three kinds of style exceptions left.

Open in Gitpod

@grunweg grunweg added awaiting-review CI Modifies the continuous integration setup or other automation t-linter Linter tech debt Tracking cross-cutting technical debt, see e.g. the "Technical debt counters" stream on zulip labels Jun 7, 2024
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jun 7, 2024

PR summary a15b10fc37

Import changes

No significant changes to the import graph


Declarations diff

+ ErrorContext
+ StyleError
+ StyleError.errorCode
+ StyleError.errorMessage
+ StyleError.normalise
+ checkFileLength
+ formatErrors
+ instance : BEq ErrorContext
+ isImportsOnlyFile
+ lintAllFiles
+ lintFile
+ main
+ outputMessage
+ parse?_errorContext
+ parseStyleExceptions

You can run this locally as follows
## summary with just the declaration names:
./scripts/no_lost_declarations.sh short <optional_commit>

## more verbose report:
./scripts/no_lost_declarations.sh <optional_commit>

@grunweg grunweg force-pushed the MR-rewrite-file-length-check branch from 6bb829f to 4a26f39 Compare June 7, 2024 22:22
/-- Create the underlying error message for a given `StyleError`. -/
def StyleError.errorMessage (err : StyleError) : String := match err with
| StyleError.fileTooLong current_size size_limit =>
s!"{size_limit} file contains {current_size} lines, try to split it up"
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.

For reviewers: this matches the current output of lint-style.py. #13623 will add a more human-readable variant.

beq ctx ctx' :=
-- XXX: `lint-style.py` was calling `resolve()` on the path before before comparing them
-- should we also do so?
ctx.path == ctx'.path
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.

In my local testing, this works as-is.

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.

Looking through this file's history, this goes back to #352. I don't see why it would be useful now. I propose to simply remove the comment and the check.


/-- Output the formatted error message, containing its context. -/
def outputMessage (errctx : ErrorContext) : String :=
-- We are outputting for github: duplicate file path, line number and error code,
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.

#13623 will add human-readable output here - this PR preserves existing behaviour

-- Omit the line number, as we don't use it anyway.
return (err.map fun e ↦ (ErrorContext.mk e 0 path))
-- XXX: print an error on any lines which don't match the particular format.
| _ => -- IO.println s!"Invalid input file: line {line} doesn't match any particular format"
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.

Printing an error here doesn't work because the monads don't work out. Quickly trying to fix that failed - a suggestion is welcome.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not just return a fake ErrorContext here?

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.

Sure, I could to this - but I don't see the advantage over just returning none (as I'm doing now). Can you articulate why you consider that better?

Printing an error is merely for validating the style exceptions file; I'm also fine with leaving this out: the style exceptions file is usually automatically generated, after all! (And if not, we should make updating it easier, as #13623 does.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Okay. Do you want to just clean up the comments here, and explain the situation (maybe that it would be nice to report an error, but awkward, and hopefully because the file is automatically generated unnecessary)?

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.

Sounds good; I added such a comment.

python-version: 3.8

- name: lint
- name: install elan
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.

This step is necessary as the "lint style" workflow now needs lake to run lake exe lint_style.

grunweg added 6 commits June 15, 2024 11:46
As all module names do not contain path separators, splitting on file paths
is actually fine :-)
thus, we *could* error on invalid lines, in principle.
@grunweg grunweg force-pushed the MR-rewrite-file-length-check branch from 39ba36c to 3540df4 Compare June 15, 2024 09:59
@ghost ghost added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jun 19, 2024
@grunweg grunweg removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jun 19, 2024
@kim-em
Copy link
Copy Markdown
Contributor

kim-em commented Jun 20, 2024

bors d+

@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented Jun 20, 2024

✌️ grunweg can now approve this pull request. To approve and merge a pull request, simply reply with bors r+. More detailed instructions are available here.

@github-actions github-actions bot added delegated This pull request has been delegated to the PR author (or occasionally another non-maintainer). and removed awaiting-review labels Jun 20, 2024
@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Jun 21, 2024

Thank you for the review!
bors r+

mathlib-bors bot pushed a commit that referenced this pull request Jun 21, 2024
This is the first in a series of PRs rewriting most checks from `lint-style.py` in Lean.
This PR sets up the basic infrastructure and ports the check for files longer than 1500 lines.
This includes parsing `style-exceptions.txt`.

To enable a gradual conversion of the linters, this PR adds a new executable `lint_style` (which runs the rewritten linters), and has `lint-style.sh` call this one also.
@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented Jun 21, 2024

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title feat: rewrite file length check in Lean [Merged by Bors] - feat: rewrite file length check in Lean Jun 21, 2024
@mathlib-bors mathlib-bors bot closed this Jun 21, 2024
@mathlib-bors mathlib-bors bot deleted the MR-rewrite-file-length-check branch June 21, 2024 07:58
mathlib-bors bot pushed a commit that referenced this pull request Jun 23, 2024
…dable output by default (#14012)

- Fix the output of `update-style-exceptions.py` by making `lake exe lint_style` optionally produce output in the right format: this regressed in #13620
- The current error messages are tailored for github annotations, which are not very readable for running the linter locally. Produce a human-readable and clickable error by default, but add a flag (which CI sets) for producing github-style output.

This entails adding a small CLI for the lint-style executable.
kbuzzard pushed a commit that referenced this pull request Jun 26, 2024
This is the first in a series of PRs rewriting most checks from `lint-style.py` in Lean.
This PR sets up the basic infrastructure and ports the check for files longer than 1500 lines.
This includes parsing `style-exceptions.txt`.

To enable a gradual conversion of the linters, this PR adds a new executable `lint_style` (which runs the rewritten linters), and has `lint-style.sh` call this one also.
kbuzzard pushed a commit that referenced this pull request Jun 26, 2024
…dable output by default (#14012)

- Fix the output of `update-style-exceptions.py` by making `lake exe lint_style` optionally produce output in the right format: this regressed in #13620
- The current error messages are tailored for github annotations, which are not very readable for running the linter locally. Produce a human-readable and clickable error by default, but add a flag (which CI sets) for producing github-style output.

This entails adding a small CLI for the lint-style executable.
dagurtomas pushed a commit that referenced this pull request Jul 2, 2024
This is the first in a series of PRs rewriting most checks from `lint-style.py` in Lean.
This PR sets up the basic infrastructure and ports the check for files longer than 1500 lines.
This includes parsing `style-exceptions.txt`.

To enable a gradual conversion of the linters, this PR adds a new executable `lint_style` (which runs the rewritten linters), and has `lint-style.sh` call this one also.
dagurtomas pushed a commit that referenced this pull request Jul 2, 2024
…dable output by default (#14012)

- Fix the output of `update-style-exceptions.py` by making `lake exe lint_style` optionally produce output in the right format: this regressed in #13620
- The current error messages are tailored for github annotations, which are not very readable for running the linter locally. Produce a human-readable and clickable error by default, but add a flag (which CI sets) for producing github-style output.

This entails adding a small CLI for the lint-style executable.
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 delegated This pull request has been delegated to the PR author (or occasionally another non-maintainer). t-linter Linter tech debt Tracking cross-cutting technical debt, see e.g. the "Technical debt counters" stream on zulip

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants