-
Notifications
You must be signed in to change notification settings - Fork 42
build(lint): consolidate linters under pre-commit #903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
build(lint): consolidate linters under pre-commit #903
Conversation
0223c49 to
332cd1e
Compare
|
FWIW, I'm not a fan of pre-commit. I prefer to have linters and tests in the same automation tool, so I can run all checks in parallel from the same tool with a single command. I'm still maintaining my own |
@tiran This is what pre-commit does... You don't have to install the hooks, you can run |
|
Pre-commit is a significant change in developer workflow, we can skip the pre-commit failures in the local workflow incase we want to focus on the code chnages initially i.e. |
|
Just to make sure it's clear - you are not required to install pre-commit as a git hook. It's opt-in and you have to install the hook manually locally by running Another way to use it (which is what I use, and is written in the README) is just by running it manually with |
|
We need a compelling reason why we should introduce yet another automation tool to run linters. Also I'm worried about
|
|
@tiran We already have pre-commit used here on Fromager. @dhellmann added it to the repo (Link #1, Link #2). I'm just consolidating the linters under the same tool, and making it easier to run the linters we only have on the CI, locally. I don't think line counts should be a metric. I've used a lot of comments and updated the What training would developers need? It's literally just a single command to run. |
Migrate all linting tools to pre-commit for unified local and CI execution. - Add .pre-commit-config.yaml with hooks for file checks, Python version consistency, EditorConfig, markdownlint-cli2, Ruff, mypy, and Mergify - Add conditional_hook.py wrapper for a graceful skip (with a warning) when requirements for linters (like Node.js for markdownlint-cli2) are not installed locally - Update Hatch lint environment with pre-commit integration - Rename .markdownlint-config.yaml to .markdownlint.yaml for markdownlint-cli2 compatibility - Update AGENTS.md and docs/develop.md with pre-commit documentation
332cd1e to
c258689
Compare
dhellmann
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of this change as a way to standardize the tool configuration. When we introduced pre-commit we only brought some of the tests in, and this makes it easier to ensure they are all run. I find that very useful when working with agents that generate code, since you can't always get them to run the linter or unit tests but you can always get them to run the pre-commit hooks when committing changes.
The markdownlint stuff is a little confusing, mostly because I don't remember how that tool is installed. Is there a way to run it via an image or to configure npm to install the right thing when it's used locally?
The only other comment is that I might have staged the changes in multiple commits to make it easier to understand what each update is doing. It's not worth splitting this up into multiple commits, now, though.
.pre-commit-config.yaml
Outdated
| - id: hatch-lint | ||
| name: hatch lint check | ||
| entry: hatch run lint:check | ||
| - id: markdownlint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does this hook get the linter?
|
@dhellmann Yeah this allows a full sync between linters running locally and on our CI. To answer your question about markdownlint: It requires Node.js to run (we install Node in our CI). I thought of using containers, but than we would just require Docker / Podman instead, and it would only work with one of them (e.g., if write it as a Podman command, it won't work for Docker users). Thought it's probably too much maintenance and overhead, with many possible issues and edge cases. The solution I came up with (which also answers your review note) is to use a script - scripts/conditional_hook.py. This script will always run on pre-commit and is required. Then inside the script, if Node.js isn't installed, there are two cases:
However... I just found mdformat, which is a Python-based Markdown linter with native pre-commit hooks. If we're OK with switching to another Markdown linter (which also might change some Markdown files to slightly different formatting rules this linter follows), I think this could be a much simpler and cleaner solution. |
Eh, we could just pick podman.
That's definitely worth looking at. Is the project maintained? How different is the output? |
|
@dhellmann For public open-source projects, I prefer to make the contribution process as simple as possible, without requiring installations of tools like Podman from people who just want to contribute something quickly :) About Initially it did make some changes I'm not sure we wanted (For example: all numbered list items started with There are still a few changes, but it's mostly converting This removed a lot of overhead and complex code I previously head to deal with because of Node.js, and it's much cleaner now so I'd love to move forward with it. I've ran some tests (changing Markdown files to invalid formats) and it appears to work well. If you'd like to test the linters, check out the branch locally, make some linting errors, and then run |
| - repo: https://github.com/astral-sh/ruff-pre-commit | ||
| rev: v0.11.11 | ||
| hooks: | ||
| # Linter runs before formatter so fixes (like removing imports) get formatted | ||
| - id: ruff-check | ||
| args: [--fix] | ||
| types_or: [python, pyi] | ||
| - id: ruff-format | ||
| types_or: [python, pyi] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block seens to fix Ruff formating and linting issues. How does pre-commit detect linting errors and fail when there is a problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there are any modifications made, it returns a non-zero error code (even if all the linting issues were fixed following these changes).
And the same happens if there are issues that couldn't be auto-fixed.
| - repo: https://github.com/hukkin/mdformat | ||
| rev: 1.0.0 | ||
| hooks: | ||
| - id: mdformat | ||
| additional_dependencies: | ||
| - mdformat-gfm # GitHub Flavored Markdown support (tables, autolinks, etc.) | ||
| - mdformat-simple-breaks # Use --- for thematic breaks instead of 70 underscores |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This linter and formatter seems to replace all * bullet points with -. It's a lot of noise that interferes with git history and git annotate. The asterisk is a valid character for bullet points. I actually prefer it over dash, because it sticks out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This linter and formatter seems to replace all * bullet points with -. It's a lot of noise that interferes with git history and git annotate
It modified only 4 doc files, and out of those only 2 doc files (docs/files.md docs/using.md) had their lists updated to use - instead of *. I wouldn't call it "a lot of noise".
I will also add that we use both * and - for different lists across our docs inconsistently, so any commit that would line these up to a specific convention would require similar changes, since we didn't enforce consistency until now and we use both symbols. See the list items here for example, that weren't modified since they already use -.
If you're concerned about git blame noise, I can move those lint changes to a separate commit (might require a separate PR since we squash merge?), and then add this commit to a .git-blame-ignore-revs and then it'll be ignored on git blame.
The asterisk is a valid character for bullet points. I actually prefer it over dash, because it sticks out.
I tried to check if there's a config option / plugin to override that, but couldn't find one.
I don't really mind either option, but if I have to choose between a Markdown linter that requires Node.js to run, and adds complexity to run locally, and a linter that just uses - for bullet points instead of * (completely valid Markdown), but runs locally and auto-fixes linting issues easily, I'll pick the other one that helps me work fast and more efficiently. I don't think this minor personal preference for docs that end up rendering the same way anyways, is worth blocking this PR.
If it's really a big concern, we could write a small plugin to override it (plugins seem pretty simple - example) but I'd rather not because we might break stuff unintentionally and it'll require maintenance if the linter updates and changes the API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't call it "a lot of noise".
I'm consider about 100 changed lines a lot of noise.
If you're concerned about git blame noise, I can move those lint changes to a separate commit (might require a separate PR since we squash merge?), and then add this commit to a .git-blame-ignore-revs and then it'll be ignored on git blame.
That's a good idea. Let's move all auto-formatting changes to a separate commit.
This PR migrates all linting tools to pre-commit for unified local and CI execution.
This allows developers to run
hatch run lint:precommit(orpre-commit run) to check for lint issues locally and auto-fix them locally, or even install git pre-commit hooks that will auto-fix them before making a commit.Notes:
Super-Linterwhich had Markdown linting (already covered bymarkdownlint) and editorconfig validation (now covered byeditorconfig-checker) was removed..markdownlint-config.yamlconfig file was renamed to.markdownlint.yamlto follow the official naming convention, which is auto-used by the CLI tool used (reference). Beforehand the name was a "made up" name that was referenced by using--config .markdownlint-config.yaml.markdownlintrequired Node.js to work (we install it in the CI beforehand). The pre-commit implementation here makes so that it's required on the CI, but for local runs themarkdownlintcheck is skipped (with a warning) if Node.js isn't installed.