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

🚀 Feature: if loading a file as CJS and ESM both fail, display both errors instead of just ESM #5235

Open
3 tasks done
jedwards1211 opened this issue Oct 25, 2024 · 3 comments
Labels
status: waiting for author waiting on response from OP - more information needed type: feature enhancement proposal

Comments

@jedwards1211
Copy link

jedwards1211 commented Oct 25, 2024

Feature Request Checklist

Overview

Ever since chai v5 dropped support for CJS, we get errors like this when we accidentally install chai 5+:

$ mocha 

 Exception during run: TypeError: Unknown file extension ".ts" for /Users/user/src/battalion/storage-site-sim/src/__tests__/SeededRNG.test.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:141:22)
    at ModuleLoader.load (node:internal/modules/esm/loader:409:7)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
    at link (node:internal/modules/esm/module_job:76:21) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

This threw us for a loop at first, because normally our tests work fine with @babel/register handling .ts files.

What's happening is first Mocha tries to load our file as CJS, which make it as far as require('chai') which fails with ERROR_REQUIRE_ESM. Then Mocha tries to load our file as ESM instead, which triggers the above error.

Suggested Solution

If Mocha would print out the error from loading our file as CJS in addition to the above error, we would have seen what caused the problem more quickly.

Alternatives

I've thought about adding code to our toolchain to check for chai v5+ and explicitly warn...but this same issue could occur with any package we're using in tests that drops CJS support

I guess another solution would be a mocharc option to force Mocha to only try one of CJS or ESM, not both.

I can't just put type: "commonjs" in my package.json because I made my toolchain run tests in CJS mode with @babel/register, and then in ESM mode with babel-register-esm, since I'm transpiling my TypeScript source to both CJS and ESM for publishing.

Additional Info

No response

@jedwards1211 jedwards1211 added status: in triage a maintainer should (re-)triage (review) this issue type: feature enhancement proposal labels Oct 25, 2024
@JoshuaKGoldberg
Copy link
Member

This is tricky. If both CJS and ESM fail to import the file because it actually doesn't exist, then displaying both errors would be extra noise for users. If this were to be implemented it would probably need some kind of heuristic for determining whether the errors are different enough to both warrant being displayed. I don't know how straightforward that would be to implement.

Would you be able to post a minimum reproduction project we could look at to better understand this?

@JoshuaKGoldberg JoshuaKGoldberg added status: waiting for author waiting on response from OP - more information needed and removed status: in triage a maintainer should (re-)triage (review) this issue labels Oct 29, 2024
@jedwards1211
Copy link
Author

Okay I'll work on making a repro soon

@jedwards1211
Copy link
Author

I don't think the extra noise would be worse, because what it currently displays is very confusing at first in a situation like this.

Theoretically the heuristic could be whether ERR_REQUIRE_ESM came from the user's own project files or from node_modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for author waiting on response from OP - more information needed type: feature enhancement proposal
Projects
None yet
Development

No branches or pull requests

2 participants