djangoproject.com icon indicating copy to clipboard operation
djangoproject.com copied to clipboard

Fix pre commit in container

Open charlesroelli opened this issue 4 months ago • 13 comments

docker compose run --rm web python -m pre_commit run --all-files does not currently work. Here are the changes needed to get it running properly.

charlesroelli avatar Oct 31 '25 10:10 charlesroelli

I'm not sure this is desired. pre-commit hooks don't require any dependency that is handled with the container setup, and I believe the standard/common practice is to keep anything git related on the host env. Can you please give more details about the usecase?

ulgens avatar Nov 02 '25 21:11 ulgens

The use case is running the pre-commit checks in a local development environment using Docker, which a few of us tried to do at a recent sprint. Maybe these tweaks belong in a separate build stage, though, if there are cases where they should not be applied.

charlesroelli avatar Nov 03 '25 08:11 charlesroelli

which a few of us tried to do at a recent sprint

Sorry, I'm missing why. Hooks can be called separately, but the expected flow to use them is to trigger the hooks during a commit, automated via git. Making a commit in a container requires access to the hosts .git config, which is not easy or feasible to provide.

ulgens avatar Nov 05 '25 10:11 ulgens

I don't recall anyone committing from the container. The use case is to check that the hooks succeed independent of committing without having to first commit, go online, push, open this page, let the CI run and open the result. The container happens to be a convenient place to run the hooks since it seems to have the required dependencies, including e.g. git which doesn't work due to the safe directory issue.

charlesroelli avatar Nov 06 '25 08:11 charlesroelli

The container happens to be a convenient place to run the hooks since it seems to have the required dependencies

I'm not opposing the case, but just wanted to clarify, the hooks don't depend on any specific dependency in the container. Only dependency, afaik, is pre-commit.

ulgens avatar Nov 21 '25 21:11 ulgens

Right, after reading about pre-commit, it seems the intention of that project is that its user should not have to install any extra dependencies to get the hooks running. Is it a bug in pre-commit, then, that we have to install libatomic1 to make the hooks work?

charlesroelli avatar Dec 05 '25 09:12 charlesroelli

Aha, it looks like recent node installs now need libatomic.

charlesroelli avatar Dec 05 '25 09:12 charlesroelli

Aha, it looks like recent node installs now need libatomic.

I'm missing where do we need to install node to run pre-commit hooks.

ulgens avatar Dec 15 '25 11:12 ulgens

Probably for mirrors-prettier.

charlesroelli avatar Dec 15 '25 12:12 charlesroelli

I removed node from my system and tried to run hooks:

Screenshot 2025-12-15 at 15 37 57 UTC@2x

I'll try to replicate the issue if you have a way to reproduce it.

ulgens avatar Dec 15 '25 12:12 ulgens

You might have libatomic1 installed for other reasons, as most of us probably do, so it's unlikely this will reproduce outside of Docker. libatomic1 is probably also installed in the Github Actions runner image, although I don't know how to check that.

charlesroelli avatar Dec 15 '25 13:12 charlesroelli

That's possible, but I can't follow you, sorry. If it's required for node, but node is not required for hooks, why would we need libatomic1?

I checked my system to be sure that libatomic is not installed and I couldn't find any trace of it being installed.

All in all, I'd still recommend keeping your dev tooling out of the container for the best experience.

ulgens avatar Dec 15 '25 13:12 ulgens

Well, that's the twist: pre-commit installs its own copy of node via nodeenv, in order to run prettier, so it should not matter whether you have node installed beforehand. It's interesting though, that it works for you without libatomic1. Maybe this was already fixed somewhere upstream.

charlesroelli avatar Dec 15 '25 14:12 charlesroelli