vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.8k stars 6.22k forks source link

fix: catch error in full reload handler #18713

Closed sapphi-red closed 4 days ago

sapphi-red commented 6 days ago

Description

deps reload test in environment-react-ssr playground was flaky on my machine. This PR fixes that.

7020160c95b711a21ef6221f48d3d8a779415995

I improved the stacktrace for invoke error (this was the error happening).

The stacktrace before was: ``` Error: There is a new version of the pre-bundle for "C:/dummy/vite/playground-temp/environment-react-ssr/node_modules/.vite/deps_ssr/react-fake-server.js?v=d75a9ccb", a page reload is going to ask for it. at throwOutdatedRequest (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:16951:15) at LoadPluginContext.load (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:16925:15) at EnvironmentPluginContainer.load (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:44606:22) at loadAndTransform (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:38314:22) at fetchModule (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:48813:16) at handleInvoke (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:41489:22) at EventEmitter.listenerForInvokeHandler (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:41562:19) { code: 'ERR_OUTDATED_OPTIMIZED_DEP' } ``` This only includes the stacktrace on the Vite server side. I changed the `reviveInvokeError` function to include the stacktrace on the module runner side first. ``` Error: There is a new version of the pre-bundle for "C:/dummy/vite/playground-temp/environment-react-ssr/node_modules/.vite/deps_ssr/react-fake-server.js?v=46c7e018", a page reload is going to ask for it. at reviveInvokeError (file:///C:/dummy/vite/packages/vite/dist/node/module-runner.js:543:24) at EventEmitter.onMessage (file:///C:/dummy/vite/packages/vite/dist/node/module-runner.js:585:34) at EventEmitter.emit (node:events:518:28) at Object.send (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:42140:22) at Object.send (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:41585:23) at Object.send (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:28057:23) at EventEmitter.listenerForInvokeHandler (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:41556:16) { code: 'ERR_OUTDATED_OPTIMIZED_DEP', [cause]: Error: There is a new version of the pre-bundle for "C:/dummy/vite/playground-temp/environment-react-ssr/node_modules/.vite/deps_ssr/react-fake-server.js?v=46c7e018", a page reload is going to ask for it. at throwOutdatedRequest (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:16951:15) at LoadPluginContext.load (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:16925:15) at EnvironmentPluginContainer.load (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:44606:22) at loadAndTransform (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:38314:22) at fetchModule (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:48813:16) at handleInvoke (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:41489:22) at EventEmitter.listenerForInvokeHandler (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:41562:19) } ``` Now the stacktrace on the module runner side is included. But the stacktrace does not point where the `invoke` call happened. I put the propeties on the root error so that the consumer can do things like `if (e.code === 'something')`. So I moved the `reviveInvokeError(e)` call to `await promise` from `promise.reject`. ``` Error: There is a new version of the pre-bundle for "C:/dummy/vite/playground-temp/environment-react-ssr/node_modules/.vite/deps_ssr/react-fake-server.js?v=de90e1b1", a page reload is going to ask for it. at reviveInvokeError (file:///C:/dummy/vite/packages/vite/dist/node/module-runner.js:543:24) at Object.invoke (file:///C:/dummy/vite/packages/vite/dist/node/module-runner.js:627:15) at ModuleRunner.getModuleInformation (file:///C:/dummy/vite/packages/vite/dist/node/module-runner.js:1170:73) at request (file:///C:/dummy/vite/packages/vite/dist/node/module-runner.js:1192:88) at eval (C:/dummy/vite/playground-temp/environment-react-ssr/src/entry-server.tsx:4:31) at ESModulesEvaluator.runInlinedModule (file:///C:/dummy/vite/packages/vite/dist/node/module-runner.js:1269:5) at ModuleRunner.directRequest (file:///C:/dummy/vite/packages/vite/dist/node/module-runner.js:1249:61) { code: 'ERR_OUTDATED_OPTIMIZED_DEP', [cause]: Error: There is a new version of the pre-bundle for "C:/dummy/vite/playground-temp/environment-react-ssr/node_modules/.vite/deps_ssr/react-fake-server.js?v=de90e1b1", a page reload is going to ask for it. at throwOutdatedRequest (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:16951:15) at LoadPluginContext.load (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:16925:15) at EnvironmentPluginContainer.load (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:44606:22) at loadAndTransform (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:38314:22) at fetchModule (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:48813:16) at handleInvoke (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:41489:22) at EventEmitter.listenerForInvokeHandler (file:///C:/dummy/vite/packages/vite/dist/node/chunks/dep-BOBR3ooh.js:41562:19) } ``` Now the stacktrace shows where the `invoke` was called.

7be26b723455e91ee42ddc1e6e448d6bed555b42

Unhandled rejection error was happening because the rejection of this await import was not handled. Maybe this error should be sent to the Vite server side so that frameworks can handle it? For now, I think we can go with this as this at least shows the error and avoids the unhandled rejection. This handler is called by https://github.com/vitejs/vite/blob/fd1283fe27cc1a19b5c7d9d72664832e4daa1bbf/packages/vite/src/node/optimizer/optimizer.ts#L514-L517

28789435d47120f48ed3cc2f731406addc72cc58

This commit and the previous commit fixes the flaky fail in the environment-react-ssr playground. ERR_OUTDATED_OPTIMIZED_DEP error was thrown sometimes and the test failed in that case. I made it to retry in that case. Currently, it seems the framework needs to call runner.clearCache() but maybe that should be called automatically.

e43490034a001b5d09dbc0104d3f7922b9cb4448

Simply replaced .replace(/\x1B\[\d+m/g, '') with util.stripVTControlCharacters.