-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pre-commit Can't Find Black #2299
Comments
Hi! Yeah, as discussed this is a problem for me too. But I don't think using a remote Black version is the solution. You said it wouldn't be a problem except in very limited scenarios, but as Black is still in beta and the style is changing, I would consider not being able to apply your own changes a huge downside. I wouldn't mind using |
I agree. 1 sounds like it would make it harder to contribute to Black which is not what we are trying to do here :) and 2 would be a problem because the entire Black codebase is formatted as part of the test suite (not sure why but don't fix what's not broken I guess?) so it must use the current style and not stable's style. |
This should be a relatively easy change, but one problem we'll run into are the actions. The lint action installs everything using pip, then runs the pre-commit, so pipenv wouldn't be available in that case. Should I amend the action? |
Fixed in #3114 |
Describe the bug
When committing files, pre-commit tries to call the system-wide
black
instead of the one installed in the virtual env. This can either mean the committed files are formatted with an incorrect version of black, or if black is not installed on a system-wide level, the commit will fail. I successfully reproduced this on windows and Linux.(This is for the local development hooks, not another project using black as a hook.)
Note that this behavior is unique to committing, and can't be replicated using
pre-commit run
.To Reproduce
pre-commit install
black
isn't installed system-wide.Expected behavior
The pre-commit will find the correct black version, and run it on the staged files.
Environment (please complete the following information):
This info is mostly irrelevant, and this bug can be reproduced under any environment.
Does this bug also happen on main?
Yes
Additional context
This has been discussed on discord here. It's a pretty common problem with local pre-commit hooks.
I've run into this before, and there are a few ways to approach this problem. First, a little background about the problem:
pre-commit doesn't activate your venv when running the hooks. The reason is not really important, but the library maintainers have explicitly stated they aren't interested in doing that. That leaves end users to deal with these problems themselves.
Onto the fixes:
pipenv run black
instead of justblack
. The downside to this is the pre-commit hook doesn't work if you're not using pipenv (admittedly that seems like a pretty niche scenario).Finally, a few things that I tried and didn't work: changing language to
python
, and trying to run black as a module usingpython -m src/black
as an entrypoint.The text was updated successfully, but these errors were encountered: