Skip to content

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

@meteorlxy

Description

@meteorlxy

⚠️ IMPORTANT ⚠️ Please do not ignore this template. If you do, your issue will be closed immediately.

Describe the bug

Importing dependencies in node_modules by fs paths will cause unexpected behavior, because we only do version hash injection for bare imports:

const versionHash = server._optimizeDepsMetadata?.browserHash
if (versionHash && isJsType) {
resolved = injectQuery(resolved, `v=${versionHash}`)
}

Reproduction

Reproduce repo: https://github.com/meteorlxy/repro-vite-2503

  1. Having a foo module:
// node_modules/foo/index.js
export default Symbol()
  1. Having a bar module that depends on foo module:
// node_modules/bar/index.js
import fooSymbol from 'foo'

export default fooSymbol
  1. Importing foo and bar via fs paths:
// index.js
import fooSymbol from './node_modules/foo'
import barSymbol from './node_modules/bar'

console.log(fooSymbol === barSymbol) // false

Explanation

  • No version hash will be injected in index.js because there is no bare import.
  • Version hash will be injected in node_modules/bar/index.js, because it uses bare import for foo module.

System Info

  • vite version: 2.0.5
  • Operating System: Linux
  • Node version: 12.16.1
  • Package manager (npm/yarn/pnpm) and version: npm 7.6.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    p4-importantViolate documented behavior or significantly improves performance (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions