openedx / .github

Centralized openedx repository workflows, community health files, etc.
1 stars 24 forks source link

fix: commitlint fails if run in a repo that uses ES6 modules #109

Closed bradenmacdonald closed 6 months ago

bradenmacdonald commented 7 months ago

On a recent PR of mine, the commitlint check was failing because of an error in the workflow:

error running commitlint
require() of ES Module /github/workspace/commitlint.config.js from /node_modules/@commitlint/load/node_modules/cosmiconfig/dist/loaders.js not supported.
commitlint.config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename commitlint.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /github/workspace/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

The reason is that I am changing the repo to use ES6 modules, by putting "type": "module" in package.json. This is the newer, modern format for JavaScript source files. However, commitlint requires that its config file use the older CommonJS format.

As you can read in this commitlint issue, the simple workaround for now is to rename the config file to use the .cjs extension. This is a painless fix that is perfectly backwards compatible, and now the commitlint check will work regardless of the current repo's module setting.

See an example showing a successful commitlint check in the same repo after I temporarily incorporated this fix.

(Another way to fix this would be to rm package.json before running commitlint, so that its runtime environment is not influenced by the checked-out repo's package.json settings, but that seems unnecessary.)

openedx-webhooks commented 7 months ago

Thanks for the pull request, @bradenmacdonald! Please note that it may take us up to several weeks or months to complete a review and merge your PR.

Feel free to add as much of the following information to the ticket as you can:

All technical communication about the code itself will be done via the GitHub pull request interface. As a reminder, our process documentation is here.

Please let us know once your PR is ready for our review and all tests are green.

feanil commented 7 months ago

@nedbat this seems reasonable to me, let me know if you have any concerns as the one who put this workflow in here in the first place.

nedbat commented 6 months ago

I'm unfamiliar with the factors here, but @bradenmacdonald seems to have found a simple recommended fix. :+1:

openedx-webhooks commented 6 months ago

@bradenmacdonald 🎉 Your pull request was merged! Please take a moment to answer a two question survey so we can improve your experience in the future.