-
-
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
WSL2 (or Linux?) cannot use regular expressions in "[[ ]]" syntax #1326
Comments
I just ran into this as well. In my case the hook was triggered in a Github action. I figured out that in Github actions the default shell is I +1 an option to tell husky how to invoke the hook script |
Thank you for reply!😊 This method seems to solve the problem of not being able to recognize the "[[" syntax, but entering the RegExp will still report a syntax error. This error will only occur when running husky, and will report this error if executed directly in the terminal, such as the following code: #!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
branchName="feature/1.1.2"
if [[ $branchName =~ ([a-z]+/([0-9]+\.){2}[0-9]+.*$) ]]; then
echo ${BASH_REMATCH[1]}
echo "branch ✅"
else
echo "branch 🛑"
fi output in terminal:
output in husky:
I think this is a problem with husky. How can I solve it to support the use of RegExp in "[["? |
On Ubuntu, the default for In We now use something like this: #!/usr/bin/env dash
set -e
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
if echo "$BRANCH" | grep -q -E '([a-z]+/([0-9]+\.){2}[0-9]+.*$)'; then
echo "branch ✅"
else
echo "branch 🛑"
exit 1
fi |
Git hooks are now all run via |
@typicode I believe that, if the script has both execute permission and has a shebang (i.e. starts with This would give users maximum flexibility if they know what they are doing, including having a Python or Node.js script. as opposed to a shell script. |
@barthy-koeln Thank you for the idea, the problem is in ubuntu, but I still want to be able to use [[...]] syntax and regular expressions, so I tried to change the default dash to bash, and by asking gpt, I got the solution :
Then it works for me. Thanks all ! 🥰 |
Sure that works, but be aware that this will have global performance impacts as It's most certainly going to be negligible for your git hooks, but might not be for system boot times and other applications. Please read the first link I sent. Here's another direct source from the Ubuntu Wiki. The first paragraph explains crucial things that ChatGPT did not warn you about, at least not in the text you pasted here. |
Is this a git thing or a husky thing? |
Husky thing. |
Is there no workaround for windows users to specify a shell that works for them? I'm flustered that the shebang lines in my scripts are ignored. It seems like I should be able to specify the shell in which my commands are run. to be clear: I want the same scripts to work in macos/ubuntu/ubuntu-wsl Edit. again: I love the idea behind husky -- but this is not intuitive and cost several hours of development time today. I'd love to contribute what I can to address the issue, but I feel like there's a philosophic issue here that I need to understand before proceeding. I'd settle for an update to the docs saying that husky expects scripts to be written in a 100% POSIX compliant shell, that would have saved a lot of time, and I'll be happy to PR that update for you. However, I'd like the choice of shell to be left to the developers, ideally. |
Hi, Sorry about that. I've updated docs, in particular: |
@typicode - That workaround is not a universal solution due to shell escaping issues among other things. Can you elaborate more on:
Windows doesn't have |
I recently started migrating my workflow to WSL2 and I was stumped on this issue for 3 hours today. 😥 |
@tsears I don't know the inner details, but Git on Windows ships with what's necessary to run However, they don't ship WSL2 is not mandatory. |
@typicode How do we use other runtime if shebang are now ignored? |
Terminal used
linux zsh
OS
Ubuntu 22.04.3 in wsl2
➜ uname -a Linux GOD-BOOK 5.15.133.1-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux ➜ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 22.04.3 LTS Release: 22.04 Codename: jammy
Problem
pre-commit
as followsgit commit -m 'chore: cache'
then throw an exception
#!/usr/bin/env bash
,but it still didn't work and the error was the same.So
Is this the official reason? How can I solve this problem? Can a certain configuration be modified to support the [[ syntax?
Thanks!
The text was updated successfully, but these errors were encountered: