Erfan
Erfan

Reputation: 42916

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?

Upvotes: 1

Views: 3606

Answers (2)

Erfan
Erfan

Reputation: 42916

I spent quite some time figuring out what caused this and how to solve this. It's not documented well, eventually I fixed it by trial and error. We have to use pass_filenames: false in our hook:

-   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
        pass_filenames: false
        args: ["--compare"]

Upvotes: 2

anthony sottile
anthony sottile

Reputation: 69964

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

Upvotes: 3

Related Questions