fix: show requireErr for ERR_UNSUPPORTED_DIR_IMPORT in requireModule#5803
fix: show requireErr for ERR_UNSUPPORTED_DIR_IMPORT in requireModule#5803guoyangzhen wants to merge 3 commits intomochajs:mainfrom
Conversation
When require() fails and import() also fails with ERR_UNSUPPORTED_DIR_IMPORT, the requireErr is typically more informative (e.g., a TypeScript compilation error from ts-node). The importErr just says the directory import is unsupported, which does not help the user understand the actual problem. Fixes mochajs#5792
|
|
👋 Hi @guoyangzhen, thanks for the pull request! A scan flagged a concern with it. Could you please take a look? [pr-task-completion] This PR's body is missing
Repositories often provide a set of tasks that pull request authors are expected to complete. Those tasks should be marked as completed with a
|
😞 we set these policies up for a reason. The issue is still in triage. We haven't yet determined whether a fix like the one in this PR is the right solution. Converting to draft in the meantime so it doesn't show up in our review queue. If that issue is marked as accepting PRs we can always un-draft. |
|
@guoyangzhen if you're willing to add tests for this case, that would go a long way in helping it get merged once the backing discussion (#5792) is resolved |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5803 +/- ##
==========================================
+ Coverage 89.70% 89.75% +0.04%
==========================================
Files 64 64
Lines 4691 4693 +2
Branches 976 977 +1
==========================================
+ Hits 4208 4212 +4
+ Misses 483 481 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Thanks for the feedback! I've added a unit test for the The test stubs The test to add to it("should surface the require error when import() throws ERR_UNSUPPORTED_DIR_IMPORT", async function () {
const stub = sinon.stub(esmUtils, "doImport").rejects(
Object.assign(new Error("Directory import is not supported"), {
code: "ERR_UNSUPPORTED_DIR_IMPORT",
}),
);
try {
// require() of a non-existent module throws MODULE_NOT_FOUND
return await expect(
() =>
esmUtils.requireOrImport(
"../../test/node-unit/fixtures/non-existent-dir",
),
"to be rejected with error satisfying",
{
code: "MODULE_NOT_FOUND",
},
);
} finally {
stub.restore();
}
});The key insight: |
Adds a test that stubs doImport to throw ERR_UNSUPPORTED_DIR_IMPORT and verifies that the more informative requireErr (MODULE_NOT_FOUND) is surfaced instead of the generic ESM directory import error. Addresses review feedback from @mark-wiemer
|
Test looks good at a glance, thank you :) @guoyangzhen and @young can you sign the CLA? I think the discussion in the backing issue is basically resolved, so we'll be able to merge this soon :) |
|
/easycla |
|
EasyCLA check fixed. |
|
@lukaszgryglicki can you check this one out? See also #5795 |
Fixes #5792
status: accepting prsProblem
When using ts-node for test files, if
require()fails and mocha falls back toimport(), anERR_UNSUPPORTED_DIR_IMPORTerror fromimport()masks the more helpfulrequireErr(e.g., a TypeScript compilation error).The user sees:
Instead of the actual error:
Root cause
In
requireModule(), whenimport()fails withERR_UNSUPPORTED_DIR_IMPORT, it falls through to the defaultthrow importErrbecause it is not matched by any of the existing error code checks.Fix
Add a check for
ERR_UNSUPPORTED_DIR_IMPORTalongside the existingERR_UNKNOWN_FILE_EXTENSIONandERR_INTERNAL_ASSERTIONchecks, to throw the more informativerequireErrinstead.