[jest-resolve] Prevent default resolver failure when potential resolution directory does not exist #4483
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
A recent change to take precedence of NODE_PATH for module resolution (#4453) made this issue more likely to occur since NODE_PATH could contain non-existent directories. While this could have occurred prior to the change, it was less likely since it is more common that modules are in local
node_modules
.Either way, non-existent directories should be treated the same way as non-existent files during module resolution. The equivalent case for
statSync
returningENOENT
for files isENOTDIR
for directories. We may want to addENOTDIR
toisFile
as well.Test plan
yarn test
Cannot find module
error. Instead, it now resolves to a potential path further down the path list.Additional Context
One other potential fix would be to propagate these exceptions up to the
Resolver.findNodeModule
callsites. Currently, this method absorbs all exceptions though, and since it is used externally by jest-config, I figured keeping the existing non-throwing API was preferable.