Skip to content
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: add parent node_modules directories #136

Closed
wants to merge 2 commits into from

Conversation

antfu
Copy link
Collaborator

@antfu antfu commented Apr 21, 2022

πŸ”— Linked issue

Partially fix nuxt/nuxt#13700

It will be fixed by the combination of this PR and nuxt/framework#4488

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

options.nodeModulesDirs.push(resolve(options.rootDir, 'node_modules'))
// Scan and add parent node_modules directory
let currentDir = resolve(options.rootDir)
while (currentDir && currentDir !== dirname(currentDir)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the Node.js search algorithm, this should normally happen when all parent node_modules are scanned. (https://nodejs.org/api/modules.html#modules_all_together see NODE_MODULES_PATHS section). Might this be an issue with rollup plugin?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this PR is trying to mimic the behavior of Node. The bug of https://github.com/nuxt/framework/issues/4217 is that the dependencies are hoisted to the upper level so the rollup failed to find it. To me, it sounds reasonable for rollup to only scan the node_modules when you provide them explicitly. I think this is quite an important bug, maybe we could have it for now and raise the discussion to rollup later?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not confident to add this and masking issue without a minimal reproduction. We use plugin node-resolve that internally uses resolve package. They are supposed to do exactly this!

https://github.com/browserify/resolve/blob/dc8ad1945c663ba47821a269aefb5549f6177cf7/lib/node-modules-paths.js#L40

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to reproduce. Can you please check this if can find a way to reproduce? https://github.com/pi0/nitro-reproduction

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@pi0 pi0 Apr 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for repro but my a minimal reproduction for nitro, I meant a nitro project. I've updated mine with yarn 3 monorepo however this issue doesn't seems to exist. It is something with nuxt integration or vue alias.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated your repro. By cd app && yarn build

Copy link
Member

@pi0 pi0 Apr 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Now i can reproduce it. (More precisely with externals: { inline: ['lodash-es'] })

@pi0
Copy link
Member

pi0 commented Apr 25, 2022

Investigating more, currently, resolve package (used by rollup plugin) processes a strange search path:

image

Adding current workaround on top of it, fixes the issue, but also produces even more duplicates:

image

Another finding: Not providing moduleDirectories option at all to rollup plugin: (also fixes the issue but looses custom dirs)

image

Concat node_modules to search path:

image

@pi0
Copy link
Member

pi0 commented Apr 25, 2022

I came up with a simpler solution by adding ['node_modules'] to the rollup options, we can create same effect for same path. But definitely need to improve resolver and also introduce workspaceDir

@pi0 pi0 closed this in 458ff57 Apr 25, 2022
@pi0 pi0 deleted the fix/resolve-deps-in-monorepo branch April 25, 2022 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failed to resolve dependencies when starting Nuxt under workspaces
2 participants