vitejs / vite

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

(v6) `ssrLoadModule` error doesn't show log for each import #17810

Open hi-ogawa opened 1 month ago

hi-ogawa commented 1 month ago

Describe the bug

On v5, when error occurs during ssrLoadModule, it logs error for each import from entry to where the error happened, for example:

$ node repro.mjs
1:11:30 PM [vite] Error when evaluating SSR module /src/dep3.js:
|- Error: something bad
    at eval (/home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/src/dep3.js:3:20)
    at instantiateModule (file:///home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/node_modules/.pnpm/vite@5.3.5/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:52650:11)

1:11:30 PM [vite] Error when evaluating SSR module /src/dep2.js:
|- Error: something bad
    at eval (/home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/src/dep3.js:3:20)
    at instantiateModule (file:///home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/node_modules/.pnpm/vite@5.3.5/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:52650:11)

1:11:30 PM [vite] Error when evaluating SSR module /src/dep1.js:
|- Error: something bad
    at eval (/home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/src/dep3.js:3:20)
    at instantiateModule (file:///home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/node_modules/.pnpm/vite@5.3.5/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:52650:11)

1:11:30 PM [vite] Error when evaluating SSR module /src/entry:
|- Error: something bad
    at eval (/home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/src/dep3.js:3:20)
    at instantiateModule (file:///home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/node_modules/.pnpm/vite@5.3.5/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:52650:11)

This is not the case on v6 and the error would look like:

$ node repro.mjs
1:12:18 PM [vite] (ssr) Error when evaluating SSR module /src/entry:
|- Error: something bad
    at eval (/home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/src/dep3.js:3:20)
    at ESModulesEvaluator.runInlinedModule (file:///home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/node_modules/.pnpm/vite@6.0.0-alpha.19/node_modules/vite/dist/node/module-runner.js:1065:6)
    at ModuleRunner.directRequest (file:///home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/node_modules/.pnpm/vite@6.0.0-alpha.19/node_modules/vite/dist/node/module-runner.js:1038:82)
    at ModuleRunner.cachedRequest (file:///home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/node_modules/.pnpm/vite@6.0.0-alpha.19/node_modules/vite/dist/node/module-runner.js:947:28)
    at request (file:///home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/node_modules/.pnpm/vite@6.0.0-alpha.19/node_modules/vite/dist/node/module-runner.js:984:116)
    at async eval (/home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/src/dep2.js:3:44)
    at async ESModulesEvaluator.runInlinedModule (file:///home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/node_modules/.pnpm/vite@6.0.0-alpha.19/node_modules/vite/dist/node/module-runner.js:1057:5)
    at async ModuleRunner.directRequest (file:///home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/node_modules/.pnpm/vite@6.0.0-alpha.19/node_modules/vite/dist/node/module-runner.js:1038:61)
    at async ModuleRunner.cachedRequest (file:///home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/node_modules/.pnpm/vite@6.0.0-alpha.19/node_modules/vite/dist/node/module-runner.js:948:76)
    at async eval (/home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/src/dep1.js:3:44)

There is a similar information available in stacktrace, but it's probably limited what it can show. I'm wondering whether v6 should do similar logging (though error logs on v5 can be too verbose so maybe that should be discussed as well).

Reproduction

https://github.com/hi-ogawa/reproductions/tree/main/vite-v6-ssr-error

Steps to reproduce

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: 6.0.0-alpha.19 => 6.0.0-alpha.19

Used Package Manager

pnpm

Logs

No response

Validations

sapphi-red commented 2 weeks ago

I think the new behavior is fine.

One thing that might be good to improve would be to remove ESModulesEvaluator.runInlinedModule/ModuleRunner.directRequest/ModuleRunner.cachedRequest/request from the stacktrace. Then the stacktrace will be like:

$ node repro.mjs
1:12:18 PM [vite] (ssr) Error when evaluating SSR module /src/entry:
|- Error: something bad
    at eval (/home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/src/dep3.js:3:20)
    at async eval (/home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/src/dep2.js:3:44)
    at async eval (/home/hiroshi/code/personal/reproductions/vite-v6-ssr-error/src/dep1.js:3:44)
sapphi-red commented 2 weeks ago

Hmm, it seems fixStacktrace: true is not working in this repro. The following error happens if I add , { fixStacktrace: true } to server.ssrLoadModule.

 node repro.mjs
Error: `line` must be greater than 0 (lines start at line 1)
    at async ModuleLoader.import (https://vbwimjlralgithub-3s0r.w-credentialless-staticblitz.com/builtins.ddb8d84d.js:154:2688)
    at async loadESM (https://vbwimjlralgithub-3s0r.w-credentialless-staticblitz.com/builtins.ddb8d84d.js:184:541)
    at async handleMainPromise (https://vbwimjlralgithub-3s0r.w-credentialless-staticblitz.com/builtins.ddb8d84d.js:165:296)
hi-ogawa commented 1 week ago

The broken stack might be due to stackblitz's runtime? https://github.com/stackblitz/webcontainer-core/issues/1240