paul-gauthier / aider

aider is AI pair programming in your terminal
https://aider.chat/
Apache License 2.0
13.74k stars 1.3k forks source link

pre-commit hooks are ignored by Aider when used on projects #802

Closed rakataprime closed 3 days ago

rakataprime commented 1 week ago

Issue

Hi, this is a really fantastic project !!!

Recently, I noticed Aider isn't respecting pre-commit hooks for projects. I have tried both the .git/hooks/precommit and the python pre-commit package and in all cases failing pre-commit hook tests will not prevent Aider from committing to a repo.

I'm not sure if this could be due to the default Aider auto-commit mode using --no-verify somewhere ( I don't see any places in the repo where this is happening) or if instead this might be a side effect of the gitpython client library. I have found a function in the gitpython docs run_commit_hook, but my limited understanding is that gitpython doesn't respect hooks and only gives you the run_commit_hook to manually override specific commits you pass through. I think it would be easy to add a feature to look at the .git/hooks/repo/precommit or .precommit yaml to pass through precommit calls to the gitpython function. Alternatively, would it be possible to set the autocommit to use shell command git so the hooks get picked up?

1) Is there a way to make Aider to respect pre-commit hooks with commits or autocommits? 2) Is there a better workflow for forcing Aider to iterate on a prompt until tests are passing?

I have been using Aider for web development tasks that included end-to-end and integration tests for nodejs api service with jest testing and I have noticed that Aider can sometimes break a number of tests without realizing it. I would like to use pre-commit test hooks to make Aider stop and correct broken tests before moving forward. In an ideal setting the pre-commit test hook would run show Aider what broke and direct Aider to go back or revert to the previous commit before trying more fixes.

I have not tested the pre-commit hook behavior with other testing frameworks other than Jest.

Version and model info

Aider v0.42.0 Models: claude-3-5-sonnet-20240620 with diff edit format, weak model claude-3-haiku-20240307 Git repo: .git with 78 files Repo-map: using 1024 tokens

.pre-commit-config.yaml

repos:
  - repo: local
    hooks:
      - id: run-tests
        name: Run Jest tests
        entry: yarn jest --silent
        language: system
        pass_filenames: false
        files: .*\.(test|spec)\.[jt]sx?$
paul-gauthier commented 1 week ago

Thanks for trying aider and filing this issue.

Yes, aider skips pre-commit hooks by default. You can probably restore them using something like:

aider --test-cmd pre-commit

The --test-cmd is intended to run a command which will do repo-wide checks, like tests. Aider will attempt to fix any issues identified if the command returns a non-zero exit status.

See these docs for more info:

https://aider.chat/docs/usage/lint-test.html

paul-gauthier commented 3 days ago

I'm going to close this issue for now, but feel free to add a comment here and I will re-open or file a new issue any time.