-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Support arbitrary shells for hook scripts #971
Comments
It would be perfect with https://github.com/google/zx ! |
I don't know if this works generally for other shells, but I found that we can add a section at the top of the #!/usr/bin/env bash
# Bash sets the BASH environment variable, so if it is not set, then we
# are running in a different shell, so manually run ourselves in BASH.
if [ -z "${BASH:-}" ]; then
exec bash "$0" "$@"
fi
source "$(dirname "$0")/_/husky.sh"
set -euo pipefail
yarn lint-staged This seems to work alright on Ubuntu (which uses dash as /bin/sh). Maybe for now it's enough to document this as a way to achieve this, for shells that are supersets of sh (i.e. as long as the shell can run normal sh scripts, then this should work). I think what happens is this:
I think we can remove some of the nested execs if we use the |
Also related: since #!/bin/sh
. "$(dirname "$0")/_/husky.sh"
# Avoid applying hook if `git rebase` is in progress
git rev-parse -q --verify REBASE_HEAD
if [ $? -eq 0 ]; then
# ... |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Please remove the |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Don't close, bot |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
need fix |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Here's my best attempt to work around this issue:
diff --git a/node_modules/husky/husky.sh b/node_modules/husky/husky.sh
index 6809ccc..4a467b3 100644
--- a/node_modules/husky/husky.sh
+++ b/node_modules/husky/husky.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/usr/bin/env bash
if [ -z "$husky_skip_init" ]; then
debug () {
if [ "$HUSKY_DEBUG" = "1" ]; then
@@ -20,7 +20,7 @@ if [ -z "$husky_skip_init" ]; then
fi
export readonly husky_skip_init=1
- sh -e "$0" "$@"
+ bash -e "$0" "$@"
exitCode="$?"
if [ $exitCode != 0 ]; then
This setup works on Linux, Windows (via git bash) and even Mac with an updated bash installed via brew, which was the challenging part. I'd love to have a non monkey patched way to achieve the same result. |
+1 for some option to change this behavior. Our pre-commit started as portable script that worked on bash and dash for one Ubuntu user but now started to use Bash specific code so this would be nice without patching or committing the husky code. |
Just to give my two cents on this discussión. Bash is not a POSIX compliant shell. Only the |
Found this out the hard way! I ended up monkey-patching: {
"prepare": "husky install && npx replace-in-file '#!/bin/sh' '#!/usr/bin/env bash' '.husky/_/husky.sh' --quiet && npx replace-in-file 'sh -e' 'bash -e' '.husky/_/husky.sh' --quiet",
} |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
no stale |
was this ever fixed? I'm on husky@^8.0.0, the issue remains |
For those who want to use arbitrary shells for hooks, I would recommend lefthook |
Correct fix for latest version (v8.0.0+):
|
To run hooks with another shell simply use this:
Be aware that not everyone has bash (in particular Windows user) so your hook may fail for them. |
Husky seems to run all hook scripts using
sh
, regardless of the shebang present in the hook. For example, with a pre-commit hook containing a line like:we will get an error, because
source
is a Bash-specific command:This occurs regardless of whether the shebang line is set as
#!/bin/sh
or#!/usr/bin/env bash
, and seems to be because Husky invokes the currently-running hook script using sh in order to capture the exit value:husky/packages/husky/src/husky.sh
Lines 20 to 26 in a66271d
A workaround for this is to use a Bourne-shell compatible
pre-commit
script, which then invokes bash explicitly with the target hook.However, it would be nice if Husky respected the shebang of the hook script directly when executing it. This seems to be possible by changing husky.sh to use env to execute the command instead, for example:
However, this only works if the hook is also marked executable. Maybe we can use $SHELL instead to detect the currently-running shell, and exec the script again using that?
The text was updated successfully, but these errors were encountered: