Skip to content

fix(resolve): normalize node_modules and bare imports, fix #2503#2848

Merged
patak-cat merged 4 commits intovitejs:mainfrom
patak-cat:fix/2503
May 4, 2021
Merged

fix(resolve): normalize node_modules and bare imports, fix #2503#2848
patak-cat merged 4 commits intovitejs:mainfrom
patak-cat:fix/2503

Conversation

@patak-cat
Copy link
Member

Description

Fix for #2503, the issue is explained in detail there.
Direct link to the reproduction https://github.com/meteorlxy/repro-vite-2503

Additional context

It is not that easy to add this to the tests. I think it would be great if something similar is added later to the repro (maybe also using cpx2)

@meteorlxy suggested that a possible solution is to inject version hash according to the node_modules path prefix, instead of only injecting for bare imports.

Given the current structure of the Resolve plugin, and that hashes are not the only special logic (we also have dedupe, we need to deal with pre-bundled deps). This PR normalizes direct node_modules imports to bare imports, so the same logic is applied in both cases. There may be a better way to do this, but I think it would involve a big refactoring. This PR not only fixes #2503, but probably other issues with direct node_modules import, for example its interaction with dedupe


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

Comment on lines +127 to +128
const pathFromBasedir = normalizePath(fsPath).slice(basedir.length)
if (pathFromBasedir.startsWith('/node_modules/')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if this will work in monorepo case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you have a failing repro after this PR?
I think one possible improvement is to trigger the node resolution at this point, but only use it if the resolved path is the same one as the one being imported

Copy link
Member Author

Choose a reason for hiding this comment

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

I pushed this change, it should avoid direct imports to be interpreted as packages from other node_modules folders.

@patak-cat patak-cat requested a review from antfu April 4, 2021 07:52
antfu
antfu previously approved these changes May 4, 2021
antfu
antfu previously approved these changes May 4, 2021
@patak-cat patak-cat requested review from Shinigami92 and antfu May 4, 2021 06:56
@Shinigami92 Shinigami92 added the p4-important Violate documented behavior or significantly improves performance (priority) label May 4, 2021
@patak-cat patak-cat changed the title fix(resolve): normalize node_modules imports as bare imports, fix #2503 fix(resolve): normalize node_modules and bare imports, fix #2503 May 4, 2021
@patak-cat patak-cat merged commit 0c97412 into vitejs:main May 4, 2021
@patak-cat patak-cat deleted the fix/2503 branch May 4, 2021 10:26
fi3ework pushed a commit to fi3ework/vite that referenced this pull request May 22, 2021
vitejs#2848)

Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p4-important Violate documented behavior or significantly improves performance (priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug report] importing dependencies by fs paths results in two different copies of the same module

4 participants