-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(vm): improve error when module is not found #5053
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
fix(vm): improve error when module is not found #5053
Conversation
✅ Deploy Preview for fastidious-cascaron-4ded94 canceled.
|
I think this is a popular way to import package and fallback when it's not installed. Something like this: try {
import('name')
} catch(err) {
if(err.code === 'ERR_MODULE_NOT_FOUND') {
// fallback
}
throw err
} |
| // check file since latest NodeJS's import.meta.resolve doesn't throw on non-existing path | ||
| if (type === 'module' || type === 'commonjs') { | ||
| if (!existsSync(path)) | ||
| throw new Error(`[vitest:vm] Cannot find module '${path}'`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer throwing the same error that Node does. Is there any reason not to do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me. I made a different error so that the test shows the difference, but there's no reason to not align with Node.
Actually this pattern seems not affected. For the bare package name, |
|
I was checking with a different version of node and it looks there was another change around v20.2.0 -> v20.3.0. It doesn't look like this change is documented in https://nodejs.org/api/esm.html#importmetaresolvespecifier, but probably this is a related PR on Node: Is it fine to keep this error for old node version? |
We don't use the default ESM loader (we don't even support loaders in VM), so I'd say it is fine to have the error we have today. |
Description
Follow up of #5045 (review)
It turned out the "non throwing" behavior of
import.meta.resolveis only for path based import such as./bad-path.jsbut not forbad-package. For the first case, Vitest throws an error duringreadFileSyncwhile NodeJS still throwsERR_MODULE_NOT_FOUND, so this is a potential inconsistency, but I suppose it's very unlikely that this difference would affect Vitest user in any critical way.Nonetheless, I'll see if there is a way to align the behavior closer to NodeJS instead of surfacing "readFileSync" exception.
references
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Documentation
pnpm run docscommand.Changesets
feat:,fix:,perf:,docs:, orchore:.