Search code examples
huskycommitlint

Commitlint with Husky Failing Due to Incorrect Commit Message from Previous Commit


I am having an issue with Commitlint and Husky in my project. I have set up Commitlint to enforce conventional commits, but I am running into a problem where the commit message seems to be taken from a previous commit rather than the current one.

Here is my setup:

commitlint.config.js

module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'type-enum': [
      2,
      'always',
      [
        'build',
        'chore',
        'ci',
        'docs',
        'feat',
        'fix',
        'perf',
        'refactor',
        'revert',
        'style',
      ],
    ],
    'subject-empty': [2, 'never'],
    'type-empty': [2, 'never'],
  },
};

.husky/pre-commit

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

npx --no -- commitlint --edit ${1}

echo "Running linting and fix..."

npm run lint:fix

git add .

I attempted to commit with the message:

feat(app): added commitlint, edited pre-commit

I am getting the following error:

14:01:36.477: [original-soft] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- package.json .husky/pre-commit package-lock.json commitlint.config.ts
warning: in the working copy of '.husky/pre-commit', LF will be replaced by CRLF the next time Git touches it
14:01:36.618: [original-soft] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\colli\AppData\Local\Temp\git-commit-msg-.txt --
⧗   input: Added eslint, husky. Code formatted
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]
✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
husky - pre-commit hook exited with code 1 (error)

The error indicates that the commit message is coming from a previous commit ("Added eslint, husky. Code formatted") instead of the current message ("feat(app): added commitlint, edited pre-commit").


Solution

  • The only workaround for this is to rename the pre-commit file to commit-msg. This tells the husky to run the script on the current commit message.

    Also, I've reported this as a bug on Commitlint GitHub issues.