r/learnpython • • 4d ago

Help with pre-commit or alternatives to not require a dev to manually do a step before it works

As a primarily Python and Django developer for the last 1.5 decades (but also all the other things... Ruby, RoR, Go, JS, Typecript, CoffeeScript, whatever) I latched onto pre-commit several years ago as an easy way for devs to "just do the right thing" in terms of linting and formatting and easy to skip stuff because "it doesn't matter as long as thing work" or "I didn't mess something that simple up".

But with proper tooling which just does stuff correctly for you, those arguments go away.

My problem with pre-commit, which is Python based and so the Python community latched onto, has been that every dev still has to manually type "pre-commit install". It is insane how many times I have had to have my first comment on a code review be "Did you run pre-commit install?" even for commits from devs who have been working on a project for months (are they deleting and doing a new clone every few weeks? I have no idea.)

At my current employer, as of around 1 year ago, I found that they were using Lefthook on both JS/TS and Java projects. The most interesting part was that it "just worked". I did not need to run a manual command for it to do its thing, unlike with pre-commit.

I have tried to use it for a Python project, using uv for package management, and it does not "just run". I suspect using uv as the environment is the problem because of needing to use uv run lefthook, etc. There is a good chance it would work in some of my other environments where they run in a container and so the path points at the virtualenv bin.

Is there a way to make this "just work", whether with lefthook or pre-commit, where a dev does not need to initially run a command which they may not do?

3 Upvotes

15 comments sorted by

2

u/Financial_Morning386 4d ago

I ran into the same thing on Python teams — the annoying part is that pre-commit install only writes into .git/hooks, which isn't tracked, so a fresh clone always starts without hooks until someone runs it. What actually worked for me was to stop treating the install as a separate step and fold it into the setup devs already run after cloning, usually a small setup script or a make setup target that runs uv sync and then pre-commit install, so it just happens alongside creating the venv. If your team already runs something on first checkout, that's the place to hide it.

For Lefthook with uv the snag I hit was similar, it needs its binary on PATH and the hooks need to call tools through uv, so I ended up installing Lefthook once via uv tool install and having the hooks run things like uv run ruff check rather than assuming a venv binary exists. And honestly I still keep the same checks in CI as the backstop, because no local hook setup is ever foolproof — the local hook is there to catch things early, CI is what actually enforces it.

1

u/jmichalicek 3d ago

Well, glad it's not just me. The good news is that it sounds like lefthook will actually work as intended in many of my projects, just not the one I first tested it with. A lot of my work is full Django sites rather than re-usable packages, and I do all of my dev in those in a dev container to match the production deployment as closely as possible and those are all set up so that the the virtualenv's bin dir is on the path.

I do also run the checks in CI, but I'd prefer to catch them before there's ever a commit and push to github. Some of the pre-commit stuff we run does some basic security checks to do stuff like ensure aws creds, private keys, passwords, etc. are not in the committed code. You can rebase to clean it up, but there are some situations (which I don't remember the specifics of) where github will still retain an artifact with the secret visible. It also saves time, github actions credits, etc.

I'm also an annoying stickler for a clean git history, commit comments, etc. and inevitably when catching the problem after a commit, you end up with trash commits with messages like "fixed formatting" rather than the dev just doing a fixup rebase to get the change where it should have been, although these days I tend to use github's squash and merge so that I can clean that stuff up at that point.

7

u/Bratmon 4d ago

I think I'm missing something here. I was under the impression that pre-commit install and lefthook install both had to be ran the first time you set up the repository, but never again

2

u/Hettie_Henseleit 3d ago

thats my understanding too, unless the package manager is somehow triggering the install script automatically in the background

1

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 3d ago

That's exactly it, Lefthook doesn't just magically install itself into your Git configuration.

1

u/ohnoitssobig 4d ago

I think you are doing it wrong. You do not have to ask "Did you run pre-commit install?". You just have to set up a CI step that runs pre-commit and let it fail if they committed without checks.

On top of it, if they frequently forget to pre-commit install (though you have to do it only once?) you probably can add a global git hook to run pre-commit install. Well, they will still have to do a manual git config so not sure what problem is solved here. If there is a unit test you can add a unit test checking for pre-commit install (lol).

1

u/jmichalicek 3d ago

I do also run the checks in CI, but the point of pre-commit or similar is to run them locally before the commit and push to github. I use hooks which check for secrets which ideally will never get committed and pushed to github, even if they get rebased out before merging because mistakes happen and they can still be exposed (in some cases there will be permanent artifacts in github PR history), it saves time and github actions credits, and generally results in a cleaner commit history.

The idea of forcing the `pre-commit install` did give me an idea. 99% of my projects, especially at work, are full Django projects which run in a dev container to maintain consistency between each developers' environment and between dev and the production deployment as much as possible. Those are all set up so that the virtual env's bin is on the path, so lefthook with uv will probably "just work" (I just happened to try it first on a non-containerized reusable package project) or I may be able to rig something in the container so that `pre-commit install` definitely runs. I've got a couple ideas to test out with that.

3

u/mrswats 4d ago

There's a way, but I do not remember exactly how.

However, pre-commit should be ran in CI so you don't have to discuss formatting or linting during code review.

1

u/JanEric1 3d ago

You use pre-commit locally and then ALSO run the same checks (+ usually additional slower ones) in CI.

1

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 3d ago

At dayjob, we used Lefthook until two days ago when I went and replaced our last instance of it with custom Git hooks. Prior to that, I considered using pre-commit to reduce (or eliminate) our JS/NPM dependencies as Python dependencies are easier to manage security-wise.

However, I realised that we weren't actually making any use of Lefthook's features. We delegated all the linting and formatting to Taskfile tasks (basically Makefiles but way better), so the Git hooks were basically just running task lint / task format, and by extension it honestly made no sense to even have Lefthook as a dependency at that point.

However, even with switching to custom Git hooks, we still needed to tell Git to use them. In our case that was easy, as all I needed to do was plug git config core.hooksPath .githooks into the Taskfile task that previously handled installing Lefthook (npm lefthook install). And hey presto, it just works.

At dayjob we basically have a separate project that sets up both local and CI dev environments for us, and it handles things like installing Taskfile, uv, Git, and setting up authentication with the internal company repositories, so we don't need to worry about installing those tools ourselves. However, this would be cumbersome in personal projects (especially if hoping to get other people to contribute), so for those I was somewhat stuck until I found out about Mise.

Mise is this handy metatool that handles installing and managing project-specific tooling, meaning every project can define its own set of tools to use (including version pinning and lockfiles), meaning I could set things like Taskfile, cspell, Markdownlint, and uv to be explicitly required in every project without needing to worry about other people having to manually install those themselves. For example, in my Python-Rust template project I only need to run mise install && task install once when first installing the project, and from thereon everything just works.

I know this isn't a solution fit for everyone or every situation, but it's been working wonders for me lately. I'd probably already use it at work, if I knew how to wire Mise into our private repositories somehow and handle the auth stuff.

Here's an example install script: https://github.com/Diapolo10/python-rust-template/blob/main/tasks/install.yml

1

u/white_nerdy 3d ago

"just do the right thing" in terms of linting and formatting

I have had to have my first comment on a code review be "Did you run pre-commit install?"

If you want to enforce mandatory linting and formatting policy (or any other machine checkable policy) on a project with multiple devs, you need to set it up in CI. Forcing everybody to install a pre-commit hook is the wrong tool for the job. As you've discovered, inevitably someone somewhere will create a clone that doesn't enforce the policy.

Enforcing the policy centrally and automatically makes the problem go away. You'll waste no more time telling people "Please run the pre-commit hook" because CI will do everything the pre-commit hook does.

1

u/Baner-Fournadet 11h ago

if you already have a makefile or a bootstrap script that sets up the local database and env vars, just tack the hook installation onto that. devs will always run the thing that makes the app actually boot on their machine