Skip to content
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

ESM swallowing MODULE_NOT_FOUND errors in case of type:module #4687

Merged
merged 2 commits into from Jul 13, 2021

Conversation

giltayar
Copy link
Contributor

Description of the Change

The move to "import-first" in Mocha generates a subtle problem in case of test files that are .js, yet are ESM due to the package.json include a "type": "module" property: if the test file includes a bad import, then Node.js throws an ERR_MODULE_NOT_FOUND, which we catch and try to require the module. Because the extension is .js and we have "type": "module", Node.js tries to be helpful and throws an ERR_REQUIRE_ESM, which is what we throw back to the user.

As pointed out by @GerHobbelt, this is totally confusing, as the original error with the correct information to resolve the problem is thrown away, and replaced by an error that has no bearing on the original problem.

What we do in the PR is simple: we figure out that this is the situation, and if it is, we throw the original error (ERR_MODULE_NOT_FOUND) instead of the new one (ERR_REQUIRE_ESM).

Alternate Designs

We thought about throwing a new error with a message that includes both error call stacks. But that's ugly and will also confuse the user, because they need to understand which of both errors is really relevant, and will be confused by an error who's message includes call stacks.

Benefits

Less confusion!

Possible Drawbacks

We're still "swallowing" errors in other situations. The alternate design would have taken care of them. We still chose the above design because it is better for the user, and currently appears to be the only problematic case.

Applicable issues

#4675, and than you to @GerHobbelt for the great bug explanation and repo that reproduces the problem. They really helped!

  • Mocha follows semantic versioning: http://semver.org

  • Is this a breaking change (major release)? No

  • Is it an enhancement (minor release)? No

  • Is it a bug fix, or does it not impact production code (patch release)? Yes

@coveralls
Copy link

coveralls commented Jul 10, 2021

Coverage Status

Coverage increased (+0.006%) to 94.375% when pulling 1d6a72b on giltayar:esm-swallowing-module-not-found into 5c59da0 on mochajs:master.

@GerHobbelt
Copy link

👍

@giltayar giltayar requested a review from juergba July 11, 2021 04:03
@juergba juergba added type: bug a defect, confirmed by a maintainer area: node.js command-line-or-Node.js-specific semver-patch implementation requires increase of "patch" version number; "bug fixes" labels Jul 12, 2021
@juergba juergba added this to the next milestone Jul 12, 2021
test/integration/esm.spec.js Outdated Show resolved Hide resolved
test/integration/fixtures/esm/type-module/package.json Outdated Show resolved Hide resolved
@juergba juergba added the run-browser-test run browser tests on forked PR if code is safe label Jul 13, 2021
@github-actions github-actions bot removed the run-browser-test run browser tests on forked PR if code is safe label Jul 13, 2021
@juergba juergba merged commit 6830821 into mochajs:master Jul 13, 2021
@juergba juergba changed the title fixes #4675: ESM swallowing MODULE_NOT_FOUND errors in case of type:module ESM swallowing MODULE_NOT_FOUND errors in case of type:module Jul 13, 2021
@giltayar giltayar deleted the esm-swallowing-module-not-found branch July 14, 2021 05:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: node.js command-line-or-Node.js-specific semver-patch implementation requires increase of "patch" version number; "bug fixes" type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

improve ERR_REQUIRE_ESM error reports by including root cause exception report.
4 participants