Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Allow specifying environment variables for hook installation? #758
Comments
|
You can already do: additional_dependencies: [--index-url, https://pypi.mycompany.com/simple]then you don't need the environment variable -- does this work? |
|
Yeah, I guess we can do this. People have been telling me it's too hacky to merge, but now that you've documented it, I guess it's not a hack anymore :) I was sort of hoping for diff --git a/app/templates/.pre-commit-config.yaml b/app/templates/.pre-commit-config.yaml
index 69647de..be8de27 100644
--- a/app/templates/.pre-commit-config.yaml
+++ b/app/templates/.pre-commit-config.yaml
@@ -3,35 +3,37 @@ repos:
sha: v1.1.1
hooks:
- id: trailing-whitespace
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]
- id: end-of-file-fixer
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]
- id: autopep8-wrapper
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]
- id: check-json
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]
- id: check-yaml
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]
- id: debug-statements
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]
- id: name-tests-test
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]
- id: flake8
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]
- id: requirements-txt-fixer
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]
- id: check-added-large-files
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]
- id: check-byte-order-marker
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]
- id: fix-encoding-pragma
args: [--remove]
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]
- id: check-executables-have-shebangs
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]
- id: detect-aws-credentials
args: ['--allow-missing-credentials']
+ additional_dependencies: [-i, https://pypi.yelpcorp.com/simple]...which seems like a lot to copy-paste around everywhere and sort of distracts from everything else in the manifest. |
|
The hard part about If you truly want all requests to go to internal pypi, maybe |
|
What's the hard part about correctly invalidating caches? Can't you hash the provided install environment and include that as part of the cache key? I don't think we want every pip invocation on the box to default to internal PyPI. At least for us right now, I think per-project is the right level to opt-in to that, and the pre-commit config seems like a pretty natural place to do it. I guess we will probably take the |
|
I guess it's not difficult, could just do the same thing that pre-commit/pre_commit/store.py Lines 105 to 106 in cf5f840 And then thread through the environ settings. Maybe that isn't the worst? But leave unspecified env variables alone? |
|
I've got another use-case for env vars: |
|
|
|
@asottile @webknjaz Apparently this bug is likely to become a showstopper for some users and I now I do have a valid use case for it, with no known workaround. Apparently ansible-lint may need user to set ANSIBLE_LIBRARY variable to point to the local ansible modules before being called, or linting will fail as soon it will encounter the custom ansible module. Tox has a great solution for this kind of problems which is the passenv and setenv, so to make the linter work correctly under tox, I only need to add something like:
This would fix execution from inside tox, even if my command would use pre-commit hook. The bad part is that this will not fix the git hook execution, which runs without any additional environment variables. For the moment I don't really care if we add this feature at global level or on each hook, what matters is to get it sorted. I am sure that in the future we will encounter other cases where we need to pass some extra environment variables to the hooks. |
|
This issue is a ~little different than runtime environment variables -- though runtime and install time environment variables are likely to be strongly related. (at least to me) it seems like ansible-lint should take a commandline argument to allow that instead of needing to set it in the environment. That said, let's get cheeky / clever (this'll probably work, similar caveats as usual with unix tools and cross platform support): - id: ansible-lint
entry: env ANSIBLE_LIBRARY=./library ansible-lintre: |
|
@asottile I faced another issue which is very close to your last comment. The issue is that in my case the needed ansible module is intalled by a python package (zuul), which I added to the list of dependencies. Still I need to be able to reffer to the pre-commit virtualenv in order to define some vars like:
So, I clearly need a variable that would be expanded by pre-commit at runtime to make it work. As you can see that piece above is taken from tox. So far I was not able to find any example of expending the venv path. |
|
Again, you have free rein if there's a shell involved: repos:
- repo: local
hooks:
- id: test
name: test
language: python
entry: bash -c 'exec env ANSIBLE_LIBRARY="$(python -c "import distutils.sysconfig; print(distutils.sysconfig.get_python_lib())")/astpretty.py" bash -c "env | grep ANSIBLE"'
verbose: true
always_run: true
additional_dependencies: [astpretty]$ pre-commit run test --verbose
[test] test..............................................................Passed
hookid: test
ANSIBLE_LIBRARY=/home/asottile/.cache/pre-commit/repo_0456vqj/py_env-python3/lib/python3.6/site-packages/astpretty.py |
|
@asottile Remind me to buy you a |
|
I think that this approach would work as long you do not use any excludes. But total I faced on case where I am forced to have some excludes. Unless pre-commit has a placeholder string that can be used to expand the filename I think we are blocked. That is because appending the filename at the end of the command would not work, it needs to be done inside:
To have this working the filename being linter needs to be before the last |
entry: bash -c '... "$@"' -- |
|
Thanks! Another case sorted in 2 minutes. I really need to write a blog post about this issue as using ansible-lint seems to involve a lots of weird workarounds, hopefully it will save time for many other users. In case someone wonders, for normal lintes you can also read from |
|
sounds good -- I'd be happy to help edit it if you need :) |
|
@asottile I'm stuck on this one. I'm trying to use https://gitlab.com/smop/pre-commit-hooks/blob/v1.0.0/.pre-commit-hooks.yaml - id: check-gitlab-ci
name: GitLab CI/CD configuration check
description: Validates .gitlab-ci.yml file
entry: pre_commit_hooks/check-gitlab-ci
language: script
pass_filenames: false
files: .gitlab-ci.ymlwith a non-standard
And if I use the absolute path,
Using |
|
yeah |
|
Hi @asottile, I am trying to set an environment variable for a hook sourced from a repo, but it's not working. With env / no bash:
Same thing with bash:
|
|
the answer is the same as my answer from #758 (comment) from
so when you have |
|
Ahhh, didn't catch that the hook was using script. Thanks! |
|
@asottile Since For something like |
|
@deiga While I did not need that yet for a hook, I can easily see a wide range of env vars that could be needed for using some tools, good examples would be |
|
@asottile I saw that, but in the case of Terraform you can have any number of providers which might take configuration via ENV and thus having some args solution is not really feasible. |
surely if that's a security issue then doing it in pre-commit would also be the same security issue? please don't try and push for changes with fear. (additionally, I don't believe it is a security issue) fwiw, |
The idea is basically something like this:
This would provide a reliable way to force pre-commit to use an internal registry to install a given hook.
What's wrong with the current approach (set an environment variable before calling pre-commit)? The main issue is that people will often do a
git pull(pulling in a hook bump somebody else did), make some changes of their own locally, then rungit commitwhich invokespre-committo install the updated hooks.There's no easy way to make sure this
git commitcommand runs withPIP_INDEX_URLset like it needs to in order to succeed, leading to some confusing behavior:Together these have started creating a culture of people wiping out their pre-commit caches any time they hit any pre-commit behavior they don't understand (currently people are proposing puppeting a cronjob to do this), and I'd like to see if there's a better approach...
I realize I'm sort of filing a ticket about the Y instead of the X here -- I'm not tied to this solution, but I think this approach is nice in that it's generic (no need to implement an option like
registryand support it for each language) and should work for most languages.