alessandrojcm / commitlint-pre-commit-hook

A pre-commit hook for commitlint
MIT License
151 stars 17 forks source link

Installation of the hook fails with Error [ERR_REQUIRE_ESM] #152

Closed nils-van-zuijlen closed 7 months ago

nils-van-zuijlen commented 7 months ago

Installing with the following .pre-commit-config.yaml fails with an error.

default_language_version:
  node: "20.11.1"
repos:
  - repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
    rev: v9.12.0
    hooks:
      - id: commitlint
        stages: [commit-msg]
        additional_dependencies: ["@commitlint/config-angular"]

The error, with home path anonymized with $HOME:

[INFO] Installing environment for https://github.com/alessandrojcm/commitlint-pre-commit-hook.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
commitlint...............................................................Failed
- hook id: commitlint
- exit code: 1

$HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/cli/lib/cli.js:123
        throw err;
        ^

Error [ERR_REQUIRE_ESM]: require() of ES Module $HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/@commitlint/config-conventional/lib/index.js from $HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js not supported.
Instead change the require of $HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/@commitlint/config-conventional/lib/index.js in $HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js to a dynamic import() which is available in all CommonJS modules.
    at $HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js:48:19
    at Array.reduce (<anonymous>)
    at loadExtends ($HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js:45:16)
    at resolveExtends ($HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js:25:22)
    at load ($HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/load/lib/load.js:42:52) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.11.1

Our commitlint.config.js:

const scope = [
  "admin",
  "deps",
  "i18n",
  "settings",
  // and other strings
];

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

module.exports = {
  extends: ["@commitlint/config-angular"],
  rules: {
    "scope-enum": [2, "always", scope],
    "type-enum": [2, "always", types],
  },
};
alessandrojcm commented 7 months ago

Hi there, this a weird once since this module is not ESM nor it does any import or require. Do you have a package.json in your project by any chance? If so can you please share it?

nils-van-zuijlen commented 7 months ago

I do not have a project.json as the project is python.

nils-van-zuijlen commented 7 months ago

I found out that @commitlint/config-angular and other configs backed by their repo recently (in v19) switched to ESM, so it may be related.

https://github.com/conventional-changelog/commitlint/releases/tag/v19.0.0

alessandrojcm commented 7 months ago

I found out that @commitlint/config-angular and other configs backed by their repo recently (in v19) switched to ESM, so it may be related.

https://github.com/conventional-changelog/commitlint/releases/tag/v19.0.0

Yeah that's probably it, ESM interop is always fun. I guess I could modify this package to be ESM by adding type: module but that might have unintended consequences.

mecampbellsoup commented 7 months ago

Any workaround for the time being? Experiencing same thing, here's our config:

const Configuration = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    "header-max-length": [2, "always", 72], // To show full header in github
    "subject-case": [2, "always", "sentence-case"], // To look consistent in auto-generated changelog
    "scope-enum": [2, "always"],
  },
  /*
   * Array of functions that return true if commitlint should ignore the given message.
   * Given array is merged with predefined functions, which consist of matchers like:
   *
   * - 'Merge pull request', 'Merge X into Y' or 'Merge branch X'
   * - 'Revert X'
   * - 'v1.2.3' (ie semver matcher)
   * - 'Automatic merge X' or 'Auto-merged X into Y'
   *
   * To see full list, check https://github.com/conventional-changelog/commitlint/blob/master/%40commitlint/is-ignored/src/defaults.ts.
   * To disable those ignores and run rules always, set `defaultIgnores: false` as shown below.
   */
  ignores: [(commit) => commit === ''],
  /*
   * Whether commitlint uses the default ignore rules, see the description above.
   */
  defaultIgnores: true,
  /*
   * Custom URL to show upon failure
   */
  helpUrl:
    'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',
  /*
   * Custom prompt configs
   */
  prompt: {
    messages: {},
    questions: {
      type: {
        description: 'please input type:',
      },
    },
  },
};

module.exports = Configuration;
commitlint...............................................................Failed
- hook id: commitlint
- exit code: 1

/home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/cli/lib/cli.js:123
        throw err;
        ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/@commitlint/config-conventional/lib/index.js from /home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js not supported.
Instead change the require of /home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/@commitlint/config-conventional/lib/index.js in /home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js to a dynamic import() which is available in all CommonJS modules.
    at /home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js:48:19
    at Array.reduce (<anonymous>)
    at loadExtends (/home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js:45:16)
    at resolveExtends (/home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js:25:22)
    at load (/home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/load/lib/load.js:42:52) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v21.6.2
alessandrojcm commented 7 months ago

Hey, could you both please try with this tag and see if that sorts the issue?

strieflin commented 7 months ago

Works for me with:

  - repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
    rev: esm # This is a workaround for https://github.com/alessandrojcm/commitlint-pre-commit-hook/issues/152
    hooks:
      - id: commitlint
        stages:
          - commit-msg
        additional_dependencies:
          - '@commitlint/config-conventional'
mecampbellsoup commented 7 months ago

Works for me too.

(k8s-billing-py3.11) (⎈|default:k8s-billing)mcampbell-1➜  ~/github/coreweave/k8s-billing : mc/fix-org-target ✘ :✚ ᐅ  git commit -m "refactor: Increase default log level to INFO"
[WARNING] The 'rev' field of repo 'https://github.com/alessandrojcm/commitlint-pre-commit-hook' appears to be a mutable reference (moving tag / branch).  Mutable references are never updated after first install and are not supported.  See https://pre-commit.com/#using-the-latest-version-for-a-repository for more details.  Hint: `pre-commit autoupdate` often fixes this.
[INFO] Initializing environment for https://github.com/alessandrojcm/commitlint-pre-commit-hook.
[INFO] Initializing environment for https://github.com/alessandrojcm/commitlint-pre-commit-hook:@commitlint/config-conventional.
check for added large files..............................................Passed
check python ast.........................................................Passed
check for case conflicts.................................................Passed
check for merge conflicts................................................Passed
check json...........................................(no files to check)Skipped
check yaml...............................................................Passed
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check for case conflicts.................................................Passed
black....................................................................Passed
isort....................................................................Passed
pycln....................................................................Passed
prettier.................................................................Passed
pyupgrade................................................................Passed
markdownlint-fix-docker..............................(no files to check)Skipped
[WARNING] The 'rev' field of repo 'https://github.com/alessandrojcm/commitlint-pre-commit-hook' appears to be a mutable reference (moving tag / branch).  Mutable references are never updated after first install and are not supported.  See https://pre-commit.com/#using-the-latest-version-for-a-repository for more details.  Hint: `pre-commit autoupdate` often fixes this.
[INFO] Installing environment for https://github.com/alessandrojcm/commitlint-pre-commit-hook.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
commitlint...............................................................Passed
[mc/fix-org-target 4c48f3a] refactor: Increase default log level to INFO
 2 files changed, 2 insertions(+), 2 deletions(-)
steeltomato commented 7 months ago

Wow, I was going to pop in here to let you know version pinning the config dependency, such as additional_dependencies: ['@commitlint/config-conventional@18.6.2'] is a viable temporary workaround... but Alessandro is already on the proper fix!

alessandrojcm commented 7 months ago

Great I'll see about mergin it into master, it should be fine since ESM is supposed to be able to import CJS but weird things tend to happen.

morremeyer commented 7 months ago

Just verified against 46ad8d10517771202384ef846ec4a5aa17e6ad26, works without any problems. Looking forward to the release.

Thanks for your work @alessandrojcm!

alessandrojcm commented 7 months ago

Released on v9.13.0