0

I have husky and lint-staged setup in a repository I am a main contributor to, and these guards consist of ensuring correct commitlint messages, proper branch naming patterns, and checking for linting errors on all commits.

This all works great when creating/committing/pushing branches from the command line, but when using any IDE's integrated Git UI, the checks are completely ignored, and anything can make it to the repo (incorrect commit messages, incorrect branch names, linting errors, etc.). Not sure why, nor do I know how to avoid this.

Here are some of the relevant pieces of code that is within this web of guards (obviously with irrelevant code omitted for clarity):

package.json:

"scripts": {
  "lint": "eslint app/"
},
"devDependencies": {
  "@commitlint/cli": "^16.2.3",
  "@commitlint/config-conventional": "^16.2.1",
  "enforce-branch-name": "^1.0.1"
},

...

"husky": {
  "hooks": {
    "commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
    "pre-push": "enforce-branch-name '^(branch-name-regex-rules)$'"
  }
},
"lint-staged": {
  "*.js": "npm run lint"
}

And then I have my .husky folder at the project root, with my commit-msg, pre-commit, and pre-push hook scripts:

commit-msg:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no -- commitlint --edit $1

pre-commit:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged

pre-push:

#!/bin/sh
LC_ALL=C

local_branch="$(git rev-parse --abbrev-ref HEAD)"
valid_branch_regex="^(branch-name-regex-rules)$"
message="Error message for incorrect branch naming"

if [[ ! $local_branch =~ $valid_branch_regex ]]
then
  echo "$message"
  exit 1
fi

exit 0

I think that should cover all the relevant code pieces that I have. Like I said, when using the command line to make commits and push branches to the remote, these rules come into play and work, informing developers of incorrect patterns.

But any developer that uses like, VSCode's integrated Git feature to commit and/or push (or even just stage the files), none of the guards work, and it's as if someone just added --no-verify to a command line Git declaration to force the commit/push despite failing hooks/guards.

Not sure what I need to do to get husky and lint-staged to work appropriately for any method of staging files, making commits, branch naming, etc. etc.

Does anyone know what I'm doing wrong, or can point me somewhere that might explain what I'm doing wrong? Thanks in advance!

torek
  • 448,244
  • 59
  • 642
  • 775
Lushmoney
  • 412
  • 11
  • 26
  • for vscode, you can see the git commands executed by the IDE : in the terminal view (`View > Terminal`) select the `Output` tab, then `Log (Git)` in the dropdown in the top right corner. – LeGEC Nov 04 '22 at 04:55
  • Yep, I'm aware of that. This doesn't really pertain to my question tho. – Lushmoney Nov 04 '22 at 05:05
  • When you commit : do you see `git commit -n` or `git commit --no-verify` in the Log output ? – LeGEC Nov 04 '22 at 05:12
  • still focusing on the "commit" action: from my PC using VSCode : if I exit with non-zero code from pre-commit hook, VSCode does block the commit and shows me the stdout/stderr in a dialog. I'm trying to see what is different wrt your setup. – LeGEC Nov 04 '22 at 05:18
  • your pre-commit hook also involves loading a whole lot of nodejs code, which may be affected by the environment in which your commands are executed. To debug your script's traces when executed through vscode, you may redirect stdout and stderr to files on disk : `exec 2>/tmp/pre-commit-log; exec 1>/tmp/pre-commit-log`, and inspect things like `env`, `which npx`, `set -x`, turn on verbose logging in husky ... – LeGEC Nov 04 '22 at 05:26
  • Interesting. Thanks for the tips! Let me do some more digging around this information.... – Lushmoney Nov 04 '22 at 20:30

0 Answers0