Skip to content

Commit

Permalink
fix(dev): only rewrite SSR stacktrace when possible (#4248)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrygrFlzr committed Jul 14, 2021
1 parent 772b2f7 commit 887c247
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
8 changes: 6 additions & 2 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -48,7 +48,10 @@ import { TransformOptions as EsbuildTransformOptions } from 'esbuild'
import { DepOptimizationMetadata, optimizeDeps } from '../optimizer'
import { ssrLoadModule } from '../ssr/ssrModuleLoader'
import { resolveSSRExternal } from '../ssr/ssrExternal'
import { ssrRewriteStacktrace } from '../ssr/ssrStacktrace'
import {
rebindErrorStacktrace,
ssrRewriteStacktrace
} from '../ssr/ssrStacktrace'
import { createMissingImporterRegisterFn } from '../optimizer/registerMissing'
import { printServerUrls } from '../logger'
import { resolveHostname } from '../utils'
Expand Down Expand Up @@ -366,7 +369,8 @@ export async function createServer(
},
ssrFixStacktrace(e) {
if (e.stack) {
e.stack = ssrRewriteStacktrace(e.stack, moduleGraph)
const stacktrace = ssrRewriteStacktrace(e.stack, moduleGraph)
rebindErrorStacktrace(e, stacktrace)
}
},
listen(port?: number, isRestart?: boolean) {
Expand Down
7 changes: 4 additions & 3 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs'
import path from 'path'
import { ViteDevServer } from '..'
import { cleanUrl, resolveFrom, unwrapId } from '../utils'
import { ssrRewriteStacktrace } from './ssrStacktrace'
import { rebindErrorStacktrace, ssrRewriteStacktrace } from './ssrStacktrace'
import {
ssrExportAllKey,
ssrModuleExportsKey,
Expand Down Expand Up @@ -141,9 +141,10 @@ async function instantiateModule(
ssrExportAll
)
} catch (e) {
e.stack = ssrRewriteStacktrace(e.stack, moduleGraph)
const stacktrace = ssrRewriteStacktrace(e.stack, moduleGraph)
rebindErrorStacktrace(e, stacktrace)
server.config.logger.error(
`Error when evaluating SSR module ${url}:\n${e.stack}`,
`Error when evaluating SSR module ${url}:\n${stacktrace}`,
{
timestamp: true,
clear: server.config.clearScreen
Expand Down
17 changes: 17 additions & 0 deletions packages/vite/src/node/ssr/ssrStacktrace.ts
Expand Up @@ -56,3 +56,20 @@ export function ssrRewriteStacktrace(
})
.join('\n')
}

export function rebindErrorStacktrace(e: Error, stacktrace: string): void {
const { configurable, writable } = Object.getOwnPropertyDescriptor(
e,
'stack'
)!
if (configurable) {
Object.defineProperty(e, 'stack', {
value: stacktrace,
enumerable: true,
configurable: true,
writable: true
})
} else if (writable) {
e.stack = stacktrace
}
}

0 comments on commit 887c247

Please sign in to comment.