aider icon indicating copy to clipboard operation
aider copied to clipboard

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

Open rakataprime opened this issue 1 year ago • 1 comments

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?$

rakataprime avatar Jul 06 '24 00:07 rakataprime

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 avatar Jul 07 '24 10:07 paul-gauthier

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.

paul-gauthier avatar Jul 12 '24 09:07 paul-gauthier

Hi, ditto that this is a fantastic project! I find I'm using it more and more. I also use pre-commit, but only for formatting. Requiring that tests pass for every single small commit doesn't really work for me. When used this way, I think pre-commit fits into a different class of tooling than test/lint because it actually fixes the errors it finds. Here's some sample output of running pre-commit after Aider committed changes that skipped it

$ pre-commit run --all-files
Trim Trailing Whitespace.................................................Passed
Fix End of Files.........................................................Passed
Check Yaml...............................................................Passed
Check for added large files..............................................Passed
clang-format.............................................................Failed
- hook id: clang-format
- files were modified by this hook
ruff-format..............................................................Passed

As you can see clang-format modifies the files that have errors. Could it be an idea to add an option --format-cmd or --fix-cmd that runs for instance clang-format on the files Aider changed before committing them? pre-commit synergizes nicely with Aider since it runs only on the files that will be committed by default. I see you are using pre-commit as well in this project so maybe there already is a solution for this? I browsed the docs, but couldn't find anything.

maje91 avatar Feb 26 '25 07:02 maje91