Skip to content

fix: can't set headerPattern from YAML configuration files #3487

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

Open
1 of 4 tasks
baby-gnu opened this issue Jan 6, 2023 · 7 comments
Open
1 of 4 tasks

fix: can't set headerPattern from YAML configuration files #3487

baby-gnu opened this issue Jan 6, 2023 · 7 comments

Comments

@baby-gnu
Copy link

baby-gnu commented Jan 6, 2023

Expected Behavior

I use YAML configuration and try to set headerPattern like this:

extends:
  - '@commitlint/config-conventional'
parserPreset:
  parserOpts:
    headerPattern: '/^([^0-9]\w*)(?:\(([^0-9].*)\))?!?: (.*)$/' 

This should avoid scope starting with a digit:

$ echo 'fix(123): foo' | commitlint --verbose
⧗   input: fix(123): foo
✖   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

Current Behavior

This load the headerPattern as a string instead of a regular expression:

$ commitlint --print-config | grep headerPattern
    parserOpts: { headerPattern: '/^([^0-9]\\w*)(?:\\(([^0-9].*)\\))?!?: (.*)$/' }

It looks like the scope starting with a digit is catch:

$ echo 'fix(123): foo' | commitlint --verbose
⧗   input: fix(123): foo
✖   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

But it catch everything since headerPattern is not a regex:

echo 'fix(bar): foo' | commitlint --verbose
⧗   input: fix(bar): foo
✖   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

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

The pattern options should be converted to regular expression.

Steps to Reproduce

1. define the following configuration `.commitlintrc.yaml`
   
   extends:
     - '@commitlint/config-conventional'
   parserPreset:
     parserOpts:
       headerPattern: '/^([^0-9]\w*)(?:\(([^0-9].*)\))?!?: (.*)$/'
   
2. execute the following command which should fail: `echo 'fix(123): foo' | commitlint --verbose`
3. execute the following command which should pass: `echo 'fix(bar): foo' | commitlint --verbose`

Context

Note that replacing the .commitlintrc.yaml with the following commitlint.config.js is working:

module.exports = {
    extends: ['@commitlint/config-conventional'],
    parserPreset: {
        parserOpts: {
            headerPattern: /^([^0-9]\w*)(?:\(([^0-9].*)\))?!?: (.*)$/,
        }
    }
};

commitlint --version

@commitlint/[email protected]

git --version

2.38.2

node --version

v19.3.0

@escapedcat
Copy link
Member

@commitlint/[email protected], I assume you get the same issue with the latest version

@baby-gnu
Copy link
Author

baby-gnu commented Jan 9, 2023

@commitlint/[email protected], I assume you get the same issue with the latest version

Yes, I tested with @commitlint/[email protected], sorry, I cut&past the wrong window.

@baby-gnu
Copy link
Author

That's strange because conventional-commit-parser seems to check for strings.

@baby-gnu
Copy link
Author

baby-gnu commented Jan 11, 2023

It's even worse that I thought, the options are not merged:

@commitlint/config-conventional: commitlint --print-config
  parserPreset: {
    name: 'conventional-changelog-conventionalcommits',
    path: './../usr/local/lib/node_modules/@commitlint/config-conventional/node_modules/conventional-changelog-conventionalcommits/index.js',
    parserOpts: {
      headerPattern: /^(\w*)(?:\((.*)\))?!?: (.*)$/,
      breakingHeaderPattern: /^(\w*)(?:\((.*)\))?!: (.*)$/,
      headerCorrespondence: [ 'type', 'scope', 'subject' ],
      noteKeywords: [ 'BREAKING CHANGE', 'BREAKING-CHANGE' ],
      revertPattern: /^(?:Revert|revert:)\s"?([\s\S]+?)"?\s*This reverts commit (\w*)\./i,
      revertCorrespondence: [ 'header', 'hash' ],
      issuePrefixes: [ '#' ]
    }
  },
@commitlint/config-conventional + config for headerPattern: commitlint --print-config
  parserPreset: {
    parserOpts: { headerPattern: '/^([^0-9]\\w*)(?:\\(([^0-9].*)\\))?!?: (.*)$/' }
  },

As you see, the other parserOpts are not preserved.

@baby-gnu
Copy link
Author

baby-gnu commented Jan 11, 2023

The non merging happens with JS configuration too, not just YAML 🤷

@baby-gnu
Copy link
Author

Adding some console.log in several js files I think the problem comes from ./../usr/local/lib/node_modules/commitlint/node_modules/@commitlint/load/lib/load.js

I don't really understand the full logic of the merging from the loaded config and the extends but it looks like the extended configuration parserPreset is simple overridden by the configuration one.

@baby-gnu
Copy link
Author

I think it may be related to #3257 and #607.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants