Skip to content

[Merged by Bors] - feat: syntax linter for #-commands#11019

Closed
adomani wants to merge 48 commits intomasterfrom
adomani/hash_command_linter
Closed

[Merged by Bors] - feat: syntax linter for #-commands#11019
adomani wants to merge 48 commits intomasterfrom
adomani/hash_command_linter

Conversation

@adomani
Copy link
Copy Markdown
Contributor

@adomani adomani commented Feb 27, 2024

A Lean-linter implementation of the #-command linter introduced in #10809.

This linter produces a warning whenever a non-allowed #-command is used. The allowed commands are

#align
#align_import
#noalign
#adaptation_note

Zulip discussion


This PR is accompanied by #11028, which places the linter in Tactic.Basic, to make sure that (most of) Mathlib passes the #-command linter.

Open in Gitpod

@adomani adomani added awaiting-review t-meta Tactics, attributes or user commands labels Feb 27, 2024
For example, `#eval, #guard, #guard_msgs` all trigger a message.
-/
register_option linter.hashCommand : Bool := {
defValue := true
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 don't think this should be enabled by default, but it should be enabled in CI (can do so in this PR). What do you think?

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.

I actually think that I prefer it to be disabled by default outside of CI: if I am using #eval in a file, I expect to want to see the output and not be bugged by the linter! I'll look up how to make sure that CI uses a different default.

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.

Yeah I agree exactly, I think its enoguh to call lean in CI with -Dlinter.hashCommand=true

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.

I am failing to make this work locally:

$ lake build Mathlib.mwe
[303/304] Building Mathlib.mwe
error: > LEAN_PATH=./.lake/packages/std/.lake/build/lib:./.lake/packages/Qq/.lake/build/lib:./.lake/packages/aesop/.lake/build/lib:./.lake/packages/proofwidgets/.lake/build/lib:./.lake/packages/Cli/.lake/build/lib:./.lake/packages/importGraph/.lake/build/lib:./.lake/build/lib LD_LIBRARY_PATH=./.lake/build/lib /home/damiano/.elan/toolchains/leanprover--lean4---v4.6.0-rc1/bin/lean -Dlinter.hashCommand=true -Dpp.unicode.fun=true -Dpp.proofs.withType=false -DautoImplicit=false -DrelaxedAutoImplicit=false ./././Mathlib/mwe.lean -R ././. -o ./.lake/build/lib/Mathlib/mwe.olean -i ./.lake/build/lib/Mathlib/mwe.ilean -c ./.lake/build/ir/Mathlib/mwe.c
error: stderr:
invalid -D parameter, unknown configuration option 'linter.hashCommand'
error: external command `/home/damiano/.elan/toolchains/leanprover--lean4---v4.6.0-rc1/bin/lean` exited with code 1

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.

I modified this

  weakLeanArgs :=
    if get_config? CI |>.isSome then
      #["-DwarningAsError=true", "-Dlinter.hashCommand=true"]
    else
      #["-Dlinter.hashCommand=true"]

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.

Alex, it seems that it is not an option to make this linter quiet locally, but chatty on CI. I propose to make the linter available with quiet default: hopefully in the future it will be possible to set different defaults globally and then the linter would reach its full potential!

Copy link
Copy Markdown
Contributor

@thorimur thorimur Mar 30, 2024

Choose a reason for hiding this comment

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

Hmm, it's a hack to work around it now, but: what if we made the defValue := true, yet also checked warningAsError in the linter (and left a very visible note, and even made the description "enables the `#`-command linter as long as `warningAsError` is `true`")? I.e.

def getLinterHash (o : Options) : Bool :=
  Linter.getLinterValue linter.hashCommand o && warningAsError.get o

Then it would run in CI (since that uses warningAsError := true) but not locally under normal circumstances.

I guess the question is: in practice, would someone want to turn on this linter locally? Or is it ok if it's only ever run in CI?

(Another option is to keep defValue := false but turn it on whenever warningAsError := true, i.e. using || instead of && above.)

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 me, the main use of this linter while working on a PR, would be when a #-command is silent. If I am using a "noisy" #-command, I am probably curious about its output and adding the linter warning would be annoying.

If there were a way of making the linter noisy exactly when the #-command is not, then I would like it always on, I think.

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.

Having it on while editing when there are no messages is a good idea! I do wonder whether it should also be on everywhere during CI for good error messages (as mentioned elsewhere). I guess the logic would be getLinterHash (<- getOptions) && ((<- get).messages.msgs.size == 0 || warningAsError.get (<- getOptions)) in that case. I won't insist on this, though! :)

@alexjbest
Copy link
Copy Markdown
Member

I like this much more than the sed version!

Copy link
Copy Markdown
Contributor

@thorimur thorimur left a comment

Choose a reason for hiding this comment

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

This review seems bigger than it is. It's basically:

  • style/documentation
  • code tweaking/taking care of edge cases
  • questions about a couple of details

This PR looks very nice! :)

adomani

This comment was marked as resolved.

@adomani adomani force-pushed the adomani/hash_command_linter branch from ac6194d to 9d9f40d Compare March 3, 2024 09:48
@jcommelin jcommelin requested a review from thorimur March 26, 2024 08:58
Copy link
Copy Markdown
Contributor

@thorimur thorimur left a comment

Choose a reason for hiding this comment

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

Apologies for the delay—very close to looking good to me! :) (Also see comment about withSetOptionIn' which is not organized as part of this review.)

I would like to see if we can run this during CI if possible by hijacking the warningAsError option (only until the underlying issue is fixed, of course)—see comment.

For example, `#eval, #guard, #guard_msgs` all trigger a message.
-/
register_option linter.hashCommand : Bool := {
defValue := true
Copy link
Copy Markdown
Contributor

@thorimur thorimur Mar 30, 2024

Choose a reason for hiding this comment

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

Hmm, it's a hack to work around it now, but: what if we made the defValue := true, yet also checked warningAsError in the linter (and left a very visible note, and even made the description "enables the `#`-command linter as long as `warningAsError` is `true`")? I.e.

def getLinterHash (o : Options) : Bool :=
  Linter.getLinterValue linter.hashCommand o && warningAsError.get o

Then it would run in CI (since that uses warningAsError := true) but not locally under normal circumstances.

I guess the question is: in practice, would someone want to turn on this linter locally? Or is it ok if it's only ever run in CI?

(Another option is to keep defValue := false but turn it on whenever warningAsError := true, i.e. using || instead of && above.)

@adomani
Copy link
Copy Markdown
Contributor Author

adomani commented Mar 30, 2024

Thomas, thank you very much for your comments! The new withSetOptionIn' command works great, thanks! I added it and also put some tests in to ensure that it does what it should!

Copy link
Copy Markdown
Contributor Author

@adomani adomani left a comment

Choose a reason for hiding this comment

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

Oh, I thought that I had replied to those previous messages, but they got lost in review!

Anyway, I ended up allowing more #guards!

@adomani
Copy link
Copy Markdown
Contributor Author

adomani commented May 23, 2024

Ok, after yet another failed test, I decided to make the #guards not allowed, except in test other than test.HashCommandLinter. I hope that this time the tests pass!

@grunweg
Copy link
Copy Markdown
Contributor

grunweg commented May 23, 2024

Tests pass; I took one last look and pushed a few tiny tweaks. (@adomani Feel free to revert those if you prefer!)
I think this lint is ready to go: performance is good, mathlib passes, no bad test changes or anything that looks questionable. Thank you for your work on this, this one took a while - but I'm glad it lands!
maintainer merge

(A follow-up PR could remove the sed-based linter this replaces. Imho, this doesn't have to happen in this PR.)

@github-actions
Copy link
Copy Markdown

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

@github-actions github-actions bot added the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label May 23, 2024
@grunweg grunweg added the awaiting-CI This PR does not pass CI yet. This label is automatically removed once it does. label May 23, 2024
grunweg added a commit that referenced this pull request May 23, 2024
The linter was rewritten in Lean in #11019.
@grunweg
Copy link
Copy Markdown
Contributor

grunweg commented May 23, 2024

Filed #13142 for the follow-up.

@github-actions github-actions bot removed the awaiting-CI This PR does not pass CI yet. This label is automatically removed once it does. label May 23, 2024
@grunweg grunweg added t-linter Linter and removed t-meta Tactics, attributes or user commands labels May 23, 2024
@jcommelin
Copy link
Copy Markdown
Member

Thanks 🎉

bors merge

@github-actions github-actions bot added ready-to-merge This PR has been sent to bors. and removed awaiting-review labels May 24, 2024
mathlib-bors bot pushed a commit that referenced this pull request May 24, 2024
A Lean-linter implementation of the `#`-command linter introduced in #10809.

This linter produces a warning whenever a non-allowed `#`-command is used.  The allowed commands are
```lean
#align
#align_import
#noalign
#adaptation_note
```

[Zulip discussion](https://leanprover.zulipchat.com/#narrow/stream/144837-PR-reviews/topic/.2311019.3A.20a.20syntax.20linter.20for.20.23-commands)



Co-authored-by: Michael Rothgang <rothgami@math.hu-berlin.de>
@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented May 24, 2024

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title feat: syntax linter for #-commands [Merged by Bors] - feat: syntax linter for #-commands May 24, 2024
@mathlib-bors mathlib-bors bot closed this May 24, 2024
@mathlib-bors mathlib-bors bot deleted the adomani/hash_command_linter branch May 24, 2024 04:42
grunweg added a commit that referenced this pull request May 24, 2024
The linter was rewritten in Lean in #11019.
grunweg added a commit that referenced this pull request May 24, 2024
A Lean-linter implementation of the `#`-command linter introduced in #10809.

This linter produces a warning whenever a non-allowed `#`-command is used.  The allowed commands are
```lean
#align
#align_import
#noalign
#adaptation_note
```

[Zulip discussion](https://leanprover.zulipchat.com/#narrow/stream/144837-PR-reviews/topic/.2311019.3A.20a.20syntax.20linter.20for.20.23-commands)



Co-authored-by: Michael Rothgang <rothgami@math.hu-berlin.de>
mathlib-bors bot pushed a commit that referenced this pull request May 27, 2024
The linter was rewritten in Lean in #11019. This removes one separate workflow step.
callesonne pushed a commit that referenced this pull request Jun 4, 2024
A Lean-linter implementation of the `#`-command linter introduced in #10809.

This linter produces a warning whenever a non-allowed `#`-command is used.  The allowed commands are
```lean
#align
#align_import
#noalign
#adaptation_note
```

[Zulip discussion](https://leanprover.zulipchat.com/#narrow/stream/144837-PR-reviews/topic/.2311019.3A.20a.20syntax.20linter.20for.20.23-commands)



Co-authored-by: Michael Rothgang <rothgami@math.hu-berlin.de>
callesonne pushed a commit that referenced this pull request Jun 4, 2024
The linter was rewritten in Lean in #11019. This removes one separate workflow step.
js2357 pushed a commit that referenced this pull request Jun 18, 2024
A Lean-linter implementation of the `#`-command linter introduced in #10809.

This linter produces a warning whenever a non-allowed `#`-command is used.  The allowed commands are
```lean
#align
#align_import
#noalign
#adaptation_note
```

[Zulip discussion](https://leanprover.zulipchat.com/#narrow/stream/144837-PR-reviews/topic/.2311019.3A.20a.20syntax.20linter.20for.20.23-commands)



Co-authored-by: Michael Rothgang <rothgami@math.hu-berlin.de>
js2357 pushed a commit that referenced this pull request Jun 18, 2024
The linter was rewritten in Lean in #11019. This removes one separate workflow step.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. ready-to-merge This PR has been sent to bors. t-linter Linter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants