Search code examples
continuous-integrationpre-commit-hookpre-commitpre-commit.com

pre-commit for local hook gives error: "unrecognized arguments: .pre-commit-config.yaml"


I have the following repo in pre-commit file .pre-commit-config.yaml

-   repo: local
    hooks:
    -   id: check_pip
        name: Check pip file
        description: This hook checks if requirements-dev.txt is up to date.
        language: system
        entry: python -m scripts.check_pip_requirements
        args: ["--compare"]

But it keeps giving me the error:

error: unrecognized arguments: .pre-commit-config.yaml

As it passes the filename as an argument to my python script. How can I prevent this?


Solution

  • to clean up your example a little bit -- and use files to only run when the necessary files change:

    -   repo: local
        hooks:
        -   id: check_pip
            name: Check pip file
            description: This hook checks if requirements-dev.txt is up to date.
            language: system
            entry: python -m scripts.check_pip_requirements --compare
            files: ^requirements-dev.txt$
            pass_filenames: false
    

    note that I did a couple things:

    • args doesn't really make sense for local hooks, you can just put that in entry
    • pass_filenames (as you did) -- pre-commit is a framework based on passing filenames to executables, but you can turn that off
    • files: this will make it so the hook only gets triggered if requirements-dev.txt changes

    alternatively (if you expect changes outside requirements-dev.txt to need to run this hook) you can drop files and use always_run: true


    disclaimer: I'm the author of pre-commit