Skip to content

Dependency pre-bundling cache created with outdated files after branch change #17336

@govizlora

Description

@govizlora

Describe the bug

After switching to a branch with different dependencies than the current branch, if running vite without running npm install first, the cache will be created with outdated dependency files, with the cache meta's lockfileHash pointing to the latest lockfile.

So, even after running npm install, since the hash of the lockfile doesn't change, subsequent runs of vite will not trigger any re-bundling. This can result in runtime errors that prevent the app from running, requiring a forced re-bundling to fix.

I'm not sure if this bug falls under vite's scope, as the root issue is the mismatch between the lockfile and the dependency files. Running npm install upon checking out branches or before starting the dev server would fix it, but it requires extra configuration efforts.

On vite's side, this could be solved by hashing all the dependency files under node_modules (maybe just the package.json of each dependency), but this might be an overkill.

Maybe we could add some docs under Optimized Dependencies, as this issue can be confusing without understanding what's happening. I'm happy to work on this if an agreement is made.

Reproduction

https://stackblitz.com/~/github.com/govizlora/vite-dep-example

Steps to reproduce

  1. Go to https://stackblitz.com/~/github.com/govizlora/vite-dep-example or checkout https://github.com/govizlora/vite-dep-example locally. Make sure you are at the default branch v8.
  2. (skip if using stackblitz) Run npm run dev. The app will render a UUID with no issue.
  3. Stop the dev server and checkout to branch v3.
  4. Run npm run dev (don't run npm install). Notice that the app fails to run, with error like SyntaxError: The requested module 'http://localhost:5173/node_modules/.vite/deps/uuid.js?v=628fb369' doesn't provide an export named: 'default'
  5. Even after running npm install, the app still fails with the same error.

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^5.2.0 => 5.2.12

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: deps optimizerEsbuild Dependencies Optimizationp4-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