-
Notifications
You must be signed in to change notification settings - Fork 56
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
Error with pre-commit: The environment variables PRE_COMMIT_FROM_REF and PRE_COMMIT_TO_REF must be defined #113
Comments
Hi @rossbar, thanks for the detailed bug report! I haven't yet used Darker too much with pre-commit, so I'm not entirely sure what the problem could be. When adding #109 (which fixed #103 reported by @KangOl), I assumed that pre-commit will always provide values for #109 documents:
Reading pre-commit's documentation, I see that the environment variables are only provided during
Is it then so that from-ref and to-ref don't actually make sense during a pre-commit hook? So if you're using Darker in a commit hook, you shouldn't specify Maybe @KangOl, @sherbie or @samoylovfp could shed some light on this? |
Perhaps this can be conditional, based on whether the environment variables have a value set or not. If set use them if not refer back to default behavior.
|
@tkolleh but is that better than the current explicit |
I believe it is more consistent. The intuition is that pre-commit (by default) runs darker as if darker was ran from the terminal by a user upon first install. If the user wants to adjust how darker runs they can pass an option/argument to darker. If using pre-commit the behavior is the same, by default run darker without options. @akaihola Thank you for creating |
I've also stumbled upon this issue. As far as I can tell, those variables are also set (besides the Perhaps the setting Another relevant issue is that it is not possible to override the Side note: I've also found that pre-commit sets an environment flag which could be used to auto detect the pre-commit mode, albeit I didn't find a relevant reference on the documentation page. Edit:
|
@rossbar, @tkolleh would @philipgian's suggestion above fix the issue for you, too, in an appropriate way? If so, @philipgian please go ahead with a pull request! |
I think I agree with what @tkolleh is saying, though I'm not familiar enough with
If I'm interpreting things correctly, I think this is consistent with @tkolleh 's intuition - i.e. run darker on the staged changes (against HEAD) unless the user explicitly asks for something different by passing in arguments or setting flags. I tested @philipgian 's suggested patch against the motivating case I set up in the OP and it does indeed behave the way that I was expecting/hoping, so +1 from me for that change if it doesn't break behavior for other users. Also thanks @philipgian for the detailed explanation, that was very informative! |
Thanks @philipgian for the PR and the details provided above. The changes address my concerns. |
Thanks for building
darker
, it seems to fill an obvious need and is a very nice tool!Apologies in advance if I'm just misunderstanding something, but I'm having trouble getting the
darker
pre-commit hooks working out-of-the-box. I've followed the instructions in the README, but when I test it locally I'm seeing the following:I'm not sure I understand this error message - my understanding was that darker compares the current staged/working directory against
HEAD
by default. I tried to explicitly setPRE_COMMIT_FROM_REF=HEAD
, but it doesn't seem possible to set theFROM
envvar without also specifyingTO
.Reproducing example
I tested things in a big, un-blackened code base, e.g. numpy (assuming a clean virtual env):
Add some lines to test the formatter, e.g. I add the following to
numpy/polynomial/chebyshev.py
:Running darker directly, the result is as expected (note that the file has many non-black-formatted lines):
Now test with pre-commit, first setting up
.pre-commit-config.yaml
as specified in the README, then:Other attempts
I've also tried adding
args: [--revision HEAD]
to the pre-commit config, as is done in #108, but am still seeing the same error.Version info
pre-commit: 2.10.1
darker: 1.2.2
The text was updated successfully, but these errors were encountered: