Skip to content

Reuse the pre-commit linter config in the CI runners #32500

Open

Description

@ogrisel

At the moment, our CI runs linting jobs using a custom script build_tools/lintin.sh with manually pip installed dependencies in the Circle CI and Azure Pipelines CI configuration files.

Those linter tool versions are not always consistent with what we configure in .pre-commit-config.yaml and that can cause problems such as the following: #32499 (comment).

I think we should refactor our CI config to reuse the pre-commit command, for instance by running:

pre-commit run --all-files

Instead of our custom build_tools/linting.sh script. However, this might not be that easy:

  • there are probably linting failures to fix or config to update to ignore specific files or folders;
  • there might be CI integration to update, in particular the .github/workflows/bot-lint-comment.yml and the ./build_tools/get_comment.py script that generate a helpful user facing comments will need to be reworked and adapted to work with the output of the pre-commit command.

Activity

  1. ogrisel commented on Oct 14, 2025

    @ogrisel
    MemberAuthor

    Before starting to work on it, I think we need to make sure that other @scikit-learn/core-devs agree that this is a worthy thing to do.

  2. lesteve commented on Oct 14, 2025

    @lesteve
    Member

    Simplest fix/work-around would be to pip install cython-lint==<manually-fixed-version> which matches the .pre-commit-config.yaml, which we are doing for ruff (and I think we did for some time with black)?

    Side-comment: some time ago I looked at using a lock-file for the linters, except that pre-commit has its own opinionated way of managing the environment and I was not able to find anything. Maybe worth another look today to see if things have changed.

  3. betatim commented on Oct 15, 2025

    @betatim
    Member

    The fact that pre-commit has its own way of managing things and that you can't run ruff from your dev environment directly but need to go via pre-commit doesn't make me happy (mostly because I don't really know how to drive pre-commit). So I am a little "against" committing ourselves to pre-commit more.

    However, I think having to keep things in sync or tracking down why you sometimes get an error and sometimes not is pretty annoying and uses up a lot of time (with little benefit to the person investing the time).

    Maybe Loïc's suggestion is a good compromise?

  4. adrinjalali commented on Oct 15, 2025

    @adrinjalali
    Member

    You can easily do pre-commit run --all-files or if you want to run a single hook, you can do pre-commit run <hook-id>. I don't think it's too hard to remember those 😜 but I personally just have the hooks installed and never have to run either ruff or pre-commit since it goes out of the way and simply works.

    I wouldn't mind us using https://github.com/pre-commit/action instead of writing our own, but my main concern would be if the bot can extract the right information as it is doing now.

  5. lesteve commented on Oct 15, 2025

    @lesteve
    Member

    There is also the question: do we want to tell contributors through the lint bot comment

    1. use pre-commit to fix all the linting issues or
    2. stick with the current status quo, "let's not require contributors to install pre-commit". The main argument for the status quo is that pre-commit behaviour can be confusing when you need to commit twice

    No strong opinion on this personally.

    By the way pre-commit run --all-files currently fails for me. I guess because we actually never have run it fully but only on changed files through pre-commit hooks, not 100% sure. This kind of drift could well happen in the future as well?

  6. adrinjalali commented on Oct 15, 2025

    @adrinjalali
    Member

    pre-commit run --all-files was passing, not sure why it's failing now.

    For me, if a contributor doesn't want to install pre-commit, I don't want to spend time telling them the alternative route. If they want the alternative, they can figure it out themselves. In practice, this has been pretty much the response on PRs. We just tell people to do pre-commit.

  7. lesteve commented on Oct 15, 2025

    @lesteve
    Member

    In practice, this has been pretty much the response on PRs. We just tell people to do pre-commit.

    Indeed I misremembered and thought the bot linting comment did NOT mention pre-commit only the tool version so that people can use the correct version. Oh well, so I guess the bot linting comment has been nudging contributors towards pre-commit for some time 1.

    pre-commit run --all-files was passing, not sure why it's failing now.

    One of this caveats you hope this seemingly simple tools never bump into, except they somehow always do (with enough time). My best guess is that we updated the version of all the hooks but did not run the pre-commit on all files so you only then update files when you touch them through pre-commit git hooks?

    Footnotes

    1. probably one of many Adrin's cunning masterplans 😜 ↩

  8. betatim commented on Oct 15, 2025

    @betatim
    Member

    You can easily do pre-commit run --all-files or if you want to run a single hook, you can do pre-commit run <hook-id>. I don't think it's too hard to remember those 😜 but I personally just have the hooks installed and never have to run either ruff or pre-commit since it goes out of the way and simply works.

    I know I can do it, but it isn't "easily done". I use it rarely when I want to format some stuff without committing it, so I don't remember how to do it, which means I need to find the docs first, look it up, etc. For example, what is the hook ID of the ruff step? How do I only "ruff" this one file here? - I am sure it is possible, maybe Cursor even knows, but it does add some friction. Especially because the pre-commit docs are, unsurprisingly, targeted at "apply all the checks to all the stuff being committed".

  9. Remi-Gau commented on Nov 27, 2025

    @Remi-Gau
    Contributor

    I wouldn't mind us using https://github.com/pre-commit/action instead of writing our own, but my main concern would be if the bot can extract the right information as it is doing now.

    I would use the pre-commit-ci in instead: https://pre-commit.ci/

    Usually finishes before any github CI even starts.

    By default it can autofix PRs by pushing commits with fixable things on it, but this can be turned off in the pre-commit config and activated via the github UI by labelling the issue with the proper label: if you don't remember the pre-commit command, just add the label to the PR and pre-commit CI will format things for you

  10. GaelVaroquaux commented on Nov 27, 2025

    @GaelVaroquaux
    Member

    @rgommers had thought's about security risks with pre-commits. Ralf, do you want to express them here, so that I don't distort what you said?

  11. rgommers commented on Nov 29, 2025

    @rgommers
    Contributor

    Sure. I consider pre-commit as used on the average OSS Python project quite risky security-wise, because:

    1. It installs/updates packages automatically in a very low-visibility way without much control
      • Contrast that to, e.g., conda which shows you a list of packages and then allows you to say Yes; pixi/uv as the currently best tools for conda-forge and PyPI respectively are also much better.
    2. Usually the type of things included in pre-commit, like sorting import order or single vs double quotes, are pointing at random projects from a single person that I don't know. Each such project needs inspecting (for me) and is a potential security risk. The risk/reward tradeoffs are generally very poor.
    3. There tend to be frequent updates of the packages pulled in by pre-commit with some bot (not for scikit-learn though it looks like), magnifying the risk.

    For SciPy we ended up adding a pre-commit hook but making it fully optional - that was a hard requirement I and a number of others had - so it's opt-in only. Linting is done with spin lint by default, and the pre-commit hook just calls the same script as spin lint does. There's a discussion starting around scipy/scipy#15489 (comment) that may be relevant.

    To each their own, so I don't mind it's in the SciPy repo or that other projects use it, but I personally don't want to touch it. If a project uses it in their CI, when I contribute I just let it fail in CI and iterate until green.

  12. Remi-Gau commented on Dec 1, 2025

    @Remi-Gau
    Contributor

    To each their own, so I don't mind it's in the SciPy repo or that other projects use it, but I personally don't want to touch it. If a project uses it in their CI, when I contribute I just let it fail in CI and iterate until green.

    so in that sense pre-commit CI would help you so that you would not have to run it locally but you could ask it to run the autofixing for you without having you to run it locally

  13. betatim commented on Dec 2, 2025

    @betatim
    Member

    I'm quite against using pre-commit CI. The main reason being that it is quite complicated to recover from having new commits in the remote branch and in your local branch. For example when pre-commit CI pushes a commit without the PR author noticing, and the PR author adding more changes locally. I don't enjoy resolving that state when I have to do it myself and I really don't want to have to explain via text how to do it to someone who is new to this.

    In addition using pre-commit's CI ties us to yet another infrastructure provider. I've not invested the time to understand how sustainable the business model of pre-commit CI is, but I suspect like all services it will eventually start charging (which is a fine strategy) or stop existing (also understandable). The more different infrastructures you rely on, the lower the combined availability (multiplying numbers smaller than one always makes an even smaller number).

    I had not considered the security aspects of the lack of transparency with pre-commit, I mostly meet it as something that makes it more tricky to replicate it to be able to run "just the formatting on just this particular file"

  14. lesteve commented on Dec 2, 2025

    @lesteve
    Member

    Yeah, my reading of the scikit-learn maintainers' room is also against https://pre-commit.ci.

    Regarding security aspects of pre-commit, I am like 🤷 for now. Sure the environment management is opaque and another layer of indirection which could obfuscate things and execute arbitrary code.

    At the same time, when you are following the setup instructions of a new repo, it's not like you are checking the implications of running every single commands that you are told to run ... for example to install the environment to build our doc pip install <this-sphinx-plugin-that-i-have-no-idea-who-maintains-it-and-why-we-need-it-but-it-looks-like-we-do> could also run arbitrary code (not to mention the dependencies of the dependencies of the dependencies of this particular sphinx plugin) ...

  15. moved this to Todo in Labson Dec 11, 2025
  16. MarcoGorelli commented on Feb 28, 2026

    @MarcoGorelli
    Contributor

    My only real opinion here is that, if you have a .pre-commit-config.yaml file, then pre-commit run --all-files should pass

    Else as a contributor i just assume that I can run it when I'm done, and then find changes unrelated to my PR which don't even pass CI

    So i'd suggest:

    • Either have a .pre-commit-config.yaml and check that pre-commit run --all-files passes in CI.
    • Or: remove it and let contributors set it up for themselves if they want to use it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions