vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
65.97k stars 5.88k forks source link

Potential unhandled rejection during deps optimization phase #14706

Open ohana54 opened 8 months ago

ohana54 commented 8 months ago

Describe the bug

When there is an error from esbuild during deps optimization, vite can crash with an unhandled rejection. The problem can be a missing transitive dependency, or a syntax error in a transitive dependency. From my debugging, I think it happens due to this promise's rejection not being handled: https://github.com/vitejs/vite/blob/4cedcdc99ca8ad38a67f46cf94dd3be65a567955/packages/vite/src/node/optimizer/optimizer.ts#L627

In the reproduction case, I'm importing a dependency that imports another dependency which doesn't exist in node_modules. This causes an unhandled rejection due to the unhandled promise above.

Reproduction

https://stackblitz.com/edit/vitejs-vite-s2mweu?file=index.html

Steps to reproduce

  1. Open the reproduction link
  2. Make sure vite dev is run in the terminal, and index.html is opened in the preview frame
  3. See vite dev crash in the terminal

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.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.9.2 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^4.4.8 => 4.5.0

Used Package Manager

npm

Logs

No response

Validations

stackblitz[bot] commented 8 months ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

RedJue commented 8 months ago

image

image The package functions-have-names is used as a development dependency, but it is used in the source code, which is not downloaded, so a not found error is reported.

ohana54 commented 8 months ago

@RedJue I know, that's the reproduction case - I'm claiming that in this case (or a syntax error in a transitive dependency) vite crashes. Instead, I expect it to remain open and only show the error shown in your screenshot.