allow resolution to work when the source file does not exist #16851
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.
Fixes #16705
URL decoding issues
Vite handles percent encoding special characters like / and : in urls, but expects pathToFileURL to encode
%2F
as%252F
(encoding the %). This change has been made in WebKit since i believe all places we create file URLs out of paths should have this, for exampleimport.meta.url
in a file named%2F.js
(which also matches node)Resolver changes
Nuxt/Vite uses
createRequire("file://...")
to facilitaterequire
in files. Some virtual files get a filepath@vue/server-renderer
which gets normalized to an absolute path relative to the current working directory (file:///Users/clo/scratch/my-nuxt-app/@vue/server-renderer
). This path, given to createRequire, produces a function that cant resolve from node_modules in Bun, but can in Node.js. The bug in Bun is when we walk up for node_modules, we use a directory cache; but the cache is not populated for non-existing directories. The change is to walk up until a cache entry exists, then to use it.This can be reproduced in the node and bun repls, by replacing the path to your project dir.
Here is the code path in Node.js that causes this to successfully resolve
createRequire
callscreateRequireFromPath
and assignsm.paths
to the list of all parent paths that contain a node_modules linkrequire
calls_resolveFilename
with the parent module (above: m) and the requestpaths = Module._resolveLookupPaths(request, parent);
m.paths
above (in code parent.paths) and the list of NODE_PATH entries. [link](https://github.com/nodejs/node/blob/e346323109b49fa6b9a4705f4e3816fc3a30c151/lib/internal/modules/cjs/loader.js#L930. this is hit since the require string "vue" is not a relative file pathtrySelf
does some native code and finds zero results. this is the directory traversal i mentioned before, which would find the nearest package.json, the import is not a self referential import._findPath
loops through paths from point 4