Skip to content

Commit

Permalink
fix: stack track point to incorrect file. (vitest-dev#3004)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenKS12138 committed Apr 1, 2023
1 parent 326b242 commit 5229a86
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/vitest/src/node/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { existsSync, readFileSync } from 'fs'
import { normalize, relative } from 'pathe'
import c from 'picocolors'
import cliTruncate from 'cli-truncate'
import { stringify } from '@vitest/utils'
import { deepClone, stringify } from '@vitest/utils'
import type { ErrorWithDiff, ParsedStack } from '../types'
import { lineSplitRE, parseErrorStacktrace, positionToOffset } from '../utils/source-map'
import { F_POINTER } from '../utils/figures'
Expand Down Expand Up @@ -44,9 +44,20 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro

const stacks = parseErrorStacktrace(e, fullStack ? [] : undefined)

/**
* file in ParsedStack is not a resolveId, use `moduleGraph.resovleUrl` to preprocess stackTrace
*/
const stackWithResolveUrl = await Promise.all(stacks.map(async (stack) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_url, resolveId] = await ctx.server.moduleGraph.resolveUrl(stack.file)
const clonedStack = deepClone(stack)
clonedStack.file = resolveId
return clonedStack
}))

const nearest = error instanceof TypeCheckError
? error.stacks[0]
: stacks.find(stack =>
: stackWithResolveUrl.find(stack =>
ctx.server.moduleGraph.getModuleById(stack.file)
&& existsSync(stack.file),
)
Expand All @@ -62,7 +73,7 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
ctx.logger.error(c.yellow(e.frame))
}
else {
printStack(ctx, stacks, nearest, errorProperties, (s) => {
printStack(ctx, stackWithResolveUrl, nearest, errorProperties, (s) => {
if (showCodeFrame && s === nearest && nearest) {
const sourceCode = readFileSync(nearest.file, 'utf-8')
ctx.logger.error(c.yellow(generateCodeFrame(sourceCode, 4, s.line, s.column)))
Expand Down

0 comments on commit 5229a86

Please sign in to comment.