-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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(resolve): make tryNodeResolve more robust #9170
Conversation
488e9ee
to
02ddba9
Compare
if (!pkg) { | ||
return | ||
} | ||
const nodeModules = lookupNodeModules(basedir) |
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.
lookupNodeModules
is an alias for the built-in Module._nodeModulePaths
function
This is the same way Node.js gets the node_modules
directories to check for dependencies in.
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.
One concern I have is that this function produces node_modules
directories outside of the importer's root directory (which I would define as the directory with .git
, although we might also want to consider SVN or Mercurial support too). It feels wrong for a bundler to allow node_modules
access outside the root directory, as this isn't reproducible by default outside the current machine. Any opinions on this, @patak-dev?
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.
Maybe we should let the user access the folder they whitelisted for fs access? Even if they are out of the .git root?
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.
Continued here: #9170 (comment)
@@ -506,8 +512,10 @@ function tryResolveFile( | |||
} | |||
} | |||
} | |||
const index = tryFsResolve(file + '/index', options) | |||
if (index) return index + postfix | |||
const indexFile = tryIndexFile(file, targetWeb, options) |
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.
This change avoids checking for {file}/index/index
or {file}/index/package.json
, both of which would be incorrect if found.
|
||
let basedir: string | ||
if (dedupe?.some((id) => possiblePkgIds.includes(id))) { |
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.
No need for possiblePkgIds
anymore with the new logic, as long we assume the resolve.dedupe
array only ever references package IDs and not module IDs. This assumption makes sense to me!
905d9cc
to
2b7b452
Compare
Closes vitejs#4439 More context: vitejs#10504
Bug introduced in vitejs#10683 Some packages use "require" instead of "default" for CJS entry
…in the `getInlineConditions` function.
…and avoid breaking certain `tryFsResolve` calls too (like with `es5-ext`)
The "{id}/package.json" lookup done by `resolvePackageData` is insufficient for certain edge cases, like when "node_modules/{dep}" is linked to a directory without a package.json in it. With this PR, you can now import any file from node_modules even if it has no package.json file associated with it. This mirrors the same capability in Node's resolution algorithm. In addition to supporting more edge cases, this new implementation might also be faster in some cases, since we are doing less lookups than compared to the previous behavior of calling `resolvePackageData` for every path in the `possiblePkgIds` array.
85d90b1
to
f62843d
Compare
Just rebased onto |
…instead of only the resolvedPkgId variable
e303e99
to
fff33dc
Compare
|
||
const workspaceRootFiles = ['lerna.json', 'pnpm-workspace.yaml', '.git'] | ||
|
||
export function isWorkspaceRoot( |
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.
Instead of using searchForWorkspaceRoot
, I added this function, which takes advantage of the packageCache
. It also only checks the given directory, instead of an upward traversal.
We've recently reworked I think it'll be easier to open a new PR for this if there's interest in it. Closing for now. |
Description
The
"{id}/package.json"
lookup done byresolvePackageData
is insufficient for certain edge cases, like when"node_modules/{dep}"
is linked to a directory without a package.json in it. With this PR, you can now import any file from node_modules even if it has no package.json file associated with it. This mirrors the same capability in Node's resolution algorithm.Probably fixes #6061 as reported by @sodatea here, since the
getPossibleModuleIds
function now includes the full module ID (even if it has an extension) in the returned array.Additional context
Help wanted with testing...Here's a (non-exhaustive?) list of cases that should be supported now:
foo/bar
whennode_modules/foo/bar.js
exists without package.jsonxyz
whennode_modules/xyz.js
existsxyz
whennode_modules/xyz/index.js
exists without package.jsonedit: Tests added!
What is the purpose of this pull request?