-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Rethrow error if not ESM import error #36
base: master
Are you sure you want to change the base?
Conversation
Ah, I think |
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #36 +/- ##
==========================================
+ Coverage 48.82% 49.54% +0.71%
==========================================
Files 2 2
Lines 213 220 +7
==========================================
+ Hits 104 109 +5
- Misses 109 111 +2 ☔ View full report in Codecov by Sentry. |
Ok, was a bit more complicated than matching a single error code, but it seems to be working now, and not too much code: // Rethrow error if it is not any of:
// - not found error
// - old Node.js compatibility error
if (e && e.message && !e.code === 'ERR_MODULE_NOT_FOUND' &&
!['Not supported', 'require(...).pathToFileURL is not a function'].includes(e.message)
) throw e; |
@lukeed friendly ping, have any thoughts on this one? |
@lukeed Ah just found a logic error in my implementation, here's the fixed version from the last commits: // Rethrow error if it is not any of:
// - not found error
// - old Node.js compatibility error
if (e && e.message &&
- !e.code === 'ERR_MODULE_NOT_FOUND' &&
+ e.code !== 'ERR_MODULE_NOT_FOUND' &&
!['Not supported', 'require(...).pathToFileURL is not a function'].includes(e.message)
) throw e; |
Edit: outdated - see my comment below: I worked around this with an additional check for a Show outdated comment content❗️(large) Caveat: this disables support for TypeScript migration files when using $ pnpm migrate up
> next-js-example-winter-2024-atvie@0.1.0 migrate /Users/k/projects/next-js-example-winter-2024-atvie
> ley --require tsx/cjs "up"
[ley] Loading configuration
[ley] An error occurred:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/k/projects/next-js-example-winter-2024-atvie/migrations/00000-createTableAnimals.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:143:22)
at async ModuleLoader.load (node:internal/modules/esm/loader:409:7)
at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
at async link (node:internal/modules/esm/module_job:76:21)
code: ERR_UNKNOWN_FILE_EXTENSION
ELIFECYCLE Command failed with exit code 1. WorkaroundInstead of |
Since Windows does not support npm scripts setting environment variables |
@lukeed we revisited this today because a student was having problems with the condition still swallowing the error on the following import: import { setEnvironmentVariables } from 'util/config'; This led to the following confusing ESM error (we use
But the real error was actually the missing import { setEnvironmentVariables } from 'util/config.js'; Without this error swallowing behavior of Ley, the error message would have been the following, which is much more helpful:
For this reason, I simplified the rethrow condition (and fixed the test) in c218b21 If you would have an opportunity to review this PR and offer some feedback, it would be great! |
Hey @lukeed, hope you're good!
Today when running
ley new
we received an error withERR_REQUIRE_ESM
:ley.config.mjs
which had a JS error in ittry/catch
inload()
fromutils.js
require()
ran on theley.config.mjs
file, which results inERR_REQUIRE_ESM
instead, which was confusingThis PR instead rethrows the error if it's not an error related to importing the ESM file (inspired by similar code in
tsm
)In the meantime, while this PR is waiting on a review, we've published our fork here:
@upleveled/ley
on npm