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
Defer environment installation if hook would be skipped #2481
Comments
it used to do this but led to situations where the configuration was incorrect and passed a commit anyway so it was changed to be eager. I'm having a hard time finding this but the very first version used to defer installation to being on-demand. the |
Could this mean that the current logic about skipping running the hook is also flawed and it would pass commits in those situations? |
yes, but SKIP is an intentional decision rather than an implicit behaviour -- therefore "our compromise there" |
unfortunately the context was lost but here was the change: #137 |
Oh, that's surprising. I would expect that |
|
Rationale
When running
pre-commit run
or duringgit commit
, all the virtual environments are initialized (fast) and dependencies for them are installed. The latter can be slow depending on many factors (number of dependencies, whether they are cached already, etc.).pre-commit has logic to decide if certain hooks should be invoked at all and if there are no matching files the hook would be run on, it is "(no files to check)Skipped". However, even when this is the case, the hook's environment still would be installed.
Possible implementation
This could be improved by leveraging the
Classifier
to check if there would be modified files at all just like in_run_single_hook
, and if not, don't just skip running the hook but also skip installing its environment.Our use case
Click for details....
Many monorepos, where hooks are possibly defined to run on
files
in certain^subdirectory/.*$
s would greatly benefit from this. In our case, many C++ developers only work in C++-related subdirectories; however, although they would never touch python files, if another, python-related hook's dependencies change, the other hook's python virtualenv is recreated.Currently, even with cached packages, this recreation (just installing packages with pip) takes 2-3 minutes on modern hardware, multiplied by the number of hooks whose
additional_dependencies
changed. In our repository, one of these virtualenvs takes around 8+ minute to install (from already cached wheels). This is acceptable for the python developers (after all they are changing code which is expected to be checked), however not for the rest of the team.Example
A PoC quick&dirty implementation is available here.
Workarounds
pre-commit run <hook>
, only given hooks' environments are installedSKIP
Searched:
Related:
Good solution is to manually invoke
pre-commit run hook-id
, however duringgit commit
it would still install unused environmentsConfining pre-commit hooks to separate subdirectories can be done by either
cd
ing in a system hook, or simply running (aliased) hooks with afiles
include pattern ofsubdirectory/.*
cd
ing out-of-the-box by pre-commit has already been discussed, and I also don't think it's a good ideaThe text was updated successfully, but these errors were encountered: