You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to get lefthook to use hooks from a custom external directory defined via an environment variable
(e.g. extends: $HOOKDIR/hooks.yml).
So far I haven't been able to get it working, but maybe I'm missing something obvious.
Steps to reproduce (tested using bash):
# 1. A basic hook for testing purposes.
$ cat hooks.yml
pre-commit:
commands:
greet:
run: echo'Extended hooks!'# 2. Export a HOOKDIR environment variable and copy our hook file there.
$ export HOOKDIR=/tmp
$ cp hooks.yml $HOOKDIR# 3. Tell lefthook about our external hooks file and test running the hook. It fails :( <--THIS IS THE ISSUE
$ echo'extends: $HOOKDIR/hooks.yml'> lefthook.yml
$ lefthook run pre-commit
runtime error: slice bounds out of range [:-1]
# 4. Repeat steps 2 & 3 using $HOME instead of $HOOKDIR. It works! :)
$ cp hooks.yml $HOME
$ echo'extends: $HOME/hooks.yml'> lefthook.yml
$ lefthook run pre-commit
Lefthook v0.7.2
RUNNING HOOKS GROUP: pre-commit
EXECUTE > greet
Extended hooks!
SUMMARY: (done in 0.02 seconds)
✔️ greet
For context:
Remote config as in #118 would be a better solution for my use case (sharing a standard set of common hooks across repos for a team of developers), but unless / until that PR gets merged, I'll be looking at extends: plus an env-defined local path as the next best thing.
The text was updated successfully, but these errors were encountered:
Looking into this, the root cause of this issue is viper's absPathify function (as called by the AddConfigPath method). It currently fails when provided with a path starting with an environment variable and containing a single pre-expansion path separator (excluding $HOME, which is handled differently than other env vars).
So:
$HOOKDIR/hooks.yml will fail (one pre-expansion path separator), but
$HOOKDIR/shared/hooks.yml (2 pre-expansion path separators) will succeed
The good news is, there's spf13/viper#495 which fixes the issue and adds a solid unit test (outside of this PR, viper does not currently test absPathify). The bad news is, the PR's been pending, with no comments, for over 2 years...
So, until there's an upstream fix in viper, a workaround for lefthook users could be to always use at least 2 pre-expansion path separators when using non-$HOME environment variables with extends, as in:
I'm trying to get lefthook to use hooks from a custom external directory defined via an environment variable
(e.g.
extends: $HOOKDIR/hooks.yml
).So far I haven't been able to get it working, but maybe I'm missing something obvious.
Steps to reproduce (tested using bash):
For context:
Remote config as in #118 would be a better solution for my use case (sharing a standard set of common hooks across repos for a team of developers), but unless / until that PR gets merged, I'll be looking at
extends:
plus an env-defined local path as the next best thing.The text was updated successfully, but these errors were encountered: