Skip to content

type-enum does NOT work with scoped package names #2108

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
pyrocat101 opened this issue Sep 7, 2020 · 6 comments
Open
1 of 4 tasks

type-enum does NOT work with scoped package names #2108

pyrocat101 opened this issue Sep 7, 2020 · 6 comments
Labels

Comments

@pyrocat101
Copy link

pyrocat101 commented Sep 7, 2020

type-enum does not work if the enum values are scoped packages such @foo/bar.

Expected Behavior

Commit message such as fix(@foo/bar): fix some bug should be supported, in a monorepo setting where the repo hosts multiple scoped npm packages.

Current Behavior

It prints a confusing error message: scope must be one of [@foo/bar, .... The error message doesn't say that slashes are used as a delimiter in this rule.

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

Do not use slashes as the multi-scope values delimeter.

Steps to Reproduce (for bugs)

  1. First step

Setup husky to use commitlint as the commit-msg hook in the repo root's package.json:

  // in package.json
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  },
  1. Second step

Use the following commitlint config:

commitlint.config.js
module.exports = {
  rules: {
    'scope-enum': [2, 'always', '@foo/bar'],
  },
};

Try to do a commit with commit message fix(@foo/bar): fix some bug.

Context

Our repo https://github.com/formatjs/formatjs is a monorepo that hosts the source code multiple packages. Most of the packages are scoped to @formatjs/. We use the scope-enum linter rule to ensure that each commit correctly specify the affected packages. This information is critical because we use Lerna to version and publish the packages, and Lerna reads these commit messages for semantic versioning.

Your Environment

Executable Version
commitlint --version 9.1.2
git --version 2.25.0
node --version 14.9.0
@escapedcat
Copy link
Member

Thanks for the report!

Do not use slashes as the multi-scope values delimeter.

I guess this would break multi-scope functionality for other users.

commitlint offers a package called config-lerna-scopes which might suit your usecase if you use lerna, but even then it won't use i.e. @project/package as scope, but only the package name.

@jdbruijn
Copy link
Contributor

@pyrocat101 I think this issue can be resolved using my plugin commitlint-plugin-function-rules.

Using the plugin, you can write something like this as commitlint config. Hope this helps!

module.exports = {
  extends: ['@commitlint/config-conventional'],
  plugins: ['commitlint-plugin-function-rules'],
  rules: {
    'scope-enum': [0], // level: disabled
    'function-rules/scope-enum': [
      2, // level: error
      'always',
      (parsed) => {
        const allowedScopes = ['@foo/bar'];
        if (!parsed.scope || allowedScopes.includes(parsed.scope)) {
          return [true];
        }

        return [false, `scope must be one of ${allowedScopes.join(', ')}`];
      },
    ],
  },
};

@longlho
Copy link

longlho commented Jan 15, 2021

@escapedcat can we make delimiter configurable? I'm happy to PR

@escapedcat
Copy link
Member

@longlho happy for any PR!

@RS-Sautter
Copy link

RS-Sautter commented Nov 8, 2022

what is the state on this? @longlho
It would also be needed to solve #3421

@RS-Sautter
Copy link

@pyrocat101 I think this issue can be resolved using my plugin commitlint-plugin-function-rules.

Using the plugin, you can write something like this as commitlint config. Hope this helps!

module.exports = {
  extends: ['@commitlint/config-conventional'],
  plugins: ['commitlint-plugin-function-rules'],
  rules: {
    'scope-enum': [0], // level: disabled
    'function-rules/scope-enum': [
      2, // level: error
      'always',
      (parsed) => {
        const allowedScopes = ['@foo/bar'];
        if (!parsed.scope || allowedScopes.includes(parsed.scope)) {
          return [true];
        }

        return [false, `scope must be one of ${allowedScopes.join(', ')}`];
      },
    ],
  },
};

@escapedcat I think this plugin should be part of the main code. It should be allowed to define a custom rule based on a function, what do you think?

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

Successfully merging a pull request may close this issue.

5 participants