fix: do not mask runtime errors with require() fallback#200
fix: do not mask runtime errors with require() fallback#200mahmoodhamdi wants to merge 3 commits intopinojs:mainfrom
Conversation
When realImport() fails with a runtime error (ReferenceError, TypeError, etc.) from the loaded module, the error.code is undefined. Previously this caused the code to fall through to the realRequire() retry path, which masked the original error with an unrelated ERR_REQUIRE_ESM or similar error. Now runtime errors (where error.constructor !== Error) are rethrown immediately, while plain Error instances from pkg bundling still fall through to the require() fallback as before. Closes pinojs#156
lib/worker.js
Outdated
| // These have no error code but indicate real bugs in the loaded module. | ||
| // Only fall back to require for plain Error instances from module loading. | ||
| // More info at: https://github.com/pinojs/thread-stream/issues/156 | ||
| if (error.code === undefined && error.constructor !== Error) { |
There was a problem hiding this comment.
I think this can be a bit more tight and just check the actual error code being thrown when real require is triggered.
There was a problem hiding this comment.
Good point! I'll update this to check the actual error code from realRequire instead. If realRequire fails with something other than MODULE_NOT_FOUND, it means the module was found but had a runtime error — so we should surface that.
Will push an update shortly.
Check the actual error code from realRequire to distinguish module resolution errors (MODULE_NOT_FOUND) from runtime errors in the loaded module (ReferenceError, TypeError, etc.).
Use instanceof checks for ReferenceError, TypeError, and RangeError instead of checking requireError.code. This correctly handles the case where require fails on ESM files (non-MODULE_NOT_FOUND errors that are not runtime errors).
|
Pushed another update — the previous approach ( Now using
All 51 tests pass (3 skipped for yarn). |
Summary
realRequire()fallback in the worker module loaderpkgbundler fallback continues to work as before for plainErrorinstancesContext
Closes #156
When a worker module has a runtime error (e.g.,
ReferenceErrorfrom an undeclared variable), the error hascode === undefined. This caused it to match thepkgbundler fallback condition, which then triedrealRequire(). If that also failed (e.g., withERR_REQUIRE_ESM), the original error was rethrown — but ifrealRequire()happened to succeed for a different reason, the runtime error was silently swallowed.The fix checks
error.constructor !== Errorto distinguish runtime errors (which are subclasses likeReferenceError,TypeError) from plainErrorinstances thrown by thepkgbundler.Test plan
ReferenceErrorin a worker module is properly surfacedpkgbundler errors (plainErrorwithcode === undefined) still fall through torealRequire()