conventional-changelog / commitlint

πŸ““ Lint commit messages
https://commitlint.js.org
MIT License
16.64k stars 892 forks source link

Cannot get commitlint to run when using lint-staged and cz-customizable #766

Open brandondurham opened 5 years ago

brandondurham commented 5 years ago

Expected Behavior

When using yarn commit the expected result is to run lint-staged to lint all js and css files; then git-cz to generate a commit message; and finally commitlint to ensure that the scope and subject fields are formatted correctly.

Current Behavior

Both lint-staged and git-cz run correctly and in the correct order, but commitlint is never triggered.

Affected packages

Steps to Reproduce (for bugs)

  1. Add "commit": "lint-staged && git-cz --no-verify", as a script in package.json.
  2. In your .lintstagedrc file, add the following config:

    {
      "*.js": [
        "pretty-quick --staged",
        "eslint src/ --fix",
        "git add",
        "eslint --fix"
      ],
      "*.css": [
        "stylelint src/ --fix",
        "git add"
      ]
    }
  3. In your .huskyrc file, add the following config:

    {
      "hooks": {
        "pre-commit": "lint-staged",
        "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
      }
    }
  4. Finally, in your .commitlintrc.js file, add the following config items:

    module.exports = {
      extends: ['@commitlint/config-conventional'],
      rules: {
        'header-case': [2, 'always', 'lower-case'],
        'scope-case': [2, 'always', 'lower-case'],
        'subject-case': [2, 'always', 'lower-case'],
      }
    };
commitlint.config.js ```js module.exports = { extends: ['@commitlint/config-conventional'], rules: { 'header-case': [2, 'always', 'lower-case'], 'scope-case': [2, 'always', 'lower-case'], 'subject-case': [2, 'always', 'lower-case'], } } ```

Context

My absolute goal is to run lint-staged, then git-cz, then commitlint any time you run yarn commit.

Your Environment

My project was started with create-react-app and has not been ejected. I’ve not even added any components yet β€” I’m just trying to get a solid, baseline codebase to start building on. Getting all of these tools to work well together has been tricky.

Executable Version
@commitlint/cli 8.1.0
@commitlint/config-conventional 8.1.0
git --version 2.20.1 (Apple Git-117)
node --version 12.6.0
commitizen 4.0.3
cz-conventional-changelog 3.0.2
cz-customizable 6.2.0
husky 3.0.1
byCedric commented 5 years ago

Hi @brandondurham! Thanks for the detailed issue report, that's helpful πŸ˜„ So I got your setup working I think, you can see it here.

If you remove --no-verify from your commit script, it works like expected. I'm not sure what commitizen or husky does with --no-verify, but it seems that the hook commit-msg is skipped with that.

Try this commit script instead "commit": "lint-staged && git-cz", that gives me this output:

❯ yarn commit
yarn run v1.17.3
$ lint-staged && git-cz
No staged files match any of provided globs.
husky > pre-commit (node v12.7.0)
No staged files match any of provided globs.
husky > commit-msg (node v12.7.0)
β§—   input: another faulty commit
βœ–   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 > commit-msg hook failed (add --no-verify to bypass)
Done in 8.89s.

Hope it helps!

byCedric commented 5 years ago

Im quite sure husky is picking up the --no-verify flag, which skips the commit-msg hook πŸ˜„

brandondurham commented 5 years ago

Ah yes. The --no-verify is in place because otherwise instead of things running in this order:

lint-staged
commitizen
commitlint

… you get this:

lint-staged
commitizen
lint-staged
commitlint

Example:

demo

byCedric commented 5 years ago

Ah ok, so basically --no-verify ignores the hook you are binding commitlint to. Is there another way to ease up lint-staged?

dzintars commented 4 years ago

Looks like i have similar issue