Skip to content

plugins not loaded when using @commitlint/load function #3332

Open
@flemminglundahl

Description

@flemminglundahl

I am configuring a commit linting setup and for this also plugins with custom rules.
I have no issue creating the plugins and running them when the cli, something like this echo "feat(t1-1234): hello there" | npx commitlint. My plugins are executed correctly and everything.

I now want to utilize your @commitlint/load function with the @commitlint/lint function for CI purposes. But the load function does not seem to provide the plugins.

I therefor get the error message UnhandledPromiseRejectionWarning: RangeError: Found invalid rule names: scope-custom. Supported rule names are:...

If I print what is loaded, plugins will have the following content: "plugins":{"local":{"rules":{}}}

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Steps to Reproduce (for bugs)

Should be easy to reproduce. I don't think I have any special configurations or anything.

I have this test.cjs for linting a string.
Commitlint.config.cjs has my configuration setup. I've also tried to create this as a npm package and just using extends on that package.

test.cjs

const load = require("@commitlint/load").default;
const lint = require("@commitlint/lint").default;

load()
  .then((opts) => {
    console.log("finished", JSON.stringify(opts));
    lint(
      "fix: bar",
      opts.rules,
      opts.parserPreset ? { parserOpts: opts.parserPreset.parserOpts } : {}
    );
  })
  .then((report) => console.log(report));

commitlint.config.cjs

const typeEnum = [
  "build",
  "chore",
  "ci",
  "docs",
  "feat",
  "fix",
  "perf",
  "refactor",
  "revert",
  "sdk",
  "style",
  "test",
];

const configuration = {
  extends: ['@commitlint/config-conventional'],
  parserOpts: {
    headerPattern: /^(\w+)\((t1)|(t3)|(t4)|(t4)|(t5)-[0-9]+\):[ ]{1}(\w{1}.+\w{1})$/,
    headerCorrespondence: ['type', 'scope', 'subject'],
  },
  rules: {
    "subject-case": [2, "always", "lower-case"],
    "subject-full-stop": [2, "never", "."],
    "subject-max-length": [2, "always", 50],
    "scope-max-length": [2, "always", 10],
    "scope-min-length": [2, "always", 6],
    "type-enum": [2, "always", typeEnum],
    "scope-custom": [2, "always"],
  },
  plugins: [
    {
      rules: {
        "scope-custom": ({ scope }) => {
          try {
            const regex = /^((t1)|(t3)|(t4)|(t4)|(t5))-[0-9]+$/g;
            const found = scope.match(regex);

            if (found) {
              return [true, ''];
            } else {
              return [false, `Your scope should match regex: ${regex}`];
            }
          } catch (_) {
            return [false, "Your commit message should contain a scope"];
          }
        },
      },
    },
  ],
};

module.exports = configuration;

Context

Your Environment

Executable Version
commitlint --version 17.0.3
git --version 2.34.1.windows
node --version 14.18.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions