Skip to content

Commit

Permalink
fix: use source-mapped line when showing result.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Crowl authored and zxch3n committed Aug 6, 2022
1 parent a6019a4 commit 9db4fec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 38 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@
"@types/glob": "^7.2.0",
"@types/node": "14.x",
"@types/semver": "^7.3.9",
"@types/stack-utils": "^2.0.1",
"@types/vscode": "^1.63.0",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.12.1",
Expand All @@ -175,7 +174,6 @@
"mocha": "^9.2.1",
"prettier": "^2.5.1",
"semver": "^7.3.5",
"stack-utils": "^2.0.5",
"tsup": "^5.12.7",
"typescript": "^4.5.5",
"vite": "^2.8.6",
Expand Down
37 changes: 18 additions & 19 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import getPort from 'get-port'
import { getTasks } from '@vitest/ws-client'
import { effect, ref } from '@vue/reactivity'
import Fuse from 'fuse.js'
import StackUtils from 'stack-utils'
import type { ErrorWithDiff, File, Task } from 'vitest'
import type { ErrorWithDiff, File, ParsedStack, Task } from 'vitest'
import type { TestController, TestItem, TestRun, WorkspaceFolder } from 'vscode'
import { Disposable, Location, Position, TestMessage, TestRunRequest, Uri } from 'vscode'
import { Lock } from 'mighty-promise'
Expand All @@ -18,9 +17,6 @@ import type { TestFile } from './TestData'
import { TestCase, TestDescribe, WEAKMAP_TEST_DATA } from './TestData'
import { log } from './log'

const stackUtils = new StackUtils({
cwd: '/ensure_absolute_paths',
})
export interface DebuggerLocation { path: string; line: number; column: number }
export class TestWatcher extends Disposable {
static cache: Record<number, TestWatcher> = {}
Expand Down Expand Up @@ -281,20 +277,23 @@ export class TestWatcher extends Disposable {
}
}

function parseLocationFromStack(testItem: TestItem, stack: string | undefined): DebuggerLocation | undefined {
const lines = stack?.split('\n') || []
const target = testItem.uri!.fsPath
for (const line of lines) {
const frame = stackUtils.parseLine(line)
if (!frame || !frame.file || !frame.line || !frame.column)
function parseLocationFromStacks(testItem: TestItem, stacks: ParsedStack[]): DebuggerLocation | undefined {
if (stacks.length === 0)
return undefined

const targetFilepath = testItem.uri!.fsPath
for (const { sourcePos } of stacks) {
if (!sourcePos)
continue
frame.file = frame.file.replace(/\//g, path.sep)
if (target === frame.file) {
return {
path: frame.file,
line: frame.line,
column: frame.column,
}

const sourceFilepath = sourcePos.source?.replace(/\//g, path.sep)
if (sourceFilepath !== targetFilepath || isNaN(sourcePos.column) || isNaN(sourcePos.line))
continue

return {
path: sourceFilepath,
line: sourcePos.line,
column: sourcePos.column,
}
}
}
Expand All @@ -306,7 +305,7 @@ function testMessageForTestError(testItem: TestItem, error: ErrorWithDiff | unde
else
testMessage = new TestMessage(error?.message ?? '')

const location = parseLocationFromStack(testItem, error?.stack)
const location = parseLocationFromStacks(testItem, error?.stacks ?? [])
if (location) {
const position = new Position(location.line - 1, location.column - 1)
testMessage.location = new Location(Uri.file(location.path), position)
Expand Down
17 changes: 0 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,6 @@
resolved "https://registry.npmmirror.com/@types/semver/-/semver-7.3.9.tgz#152c6c20a7688c30b967ec1841d31ace569863fc"
integrity sha512-L/TMpyURfBkf+o/526Zb6kd/tchUP3iBDEPjqjb+U2MAJhVRxxrmr2fwpe08E7QsV7YLcpq0tUaQ9O9x97ZIxQ==

"@types/stack-utils@^2.0.1":
version "2.0.1"
resolved "https://registry.npmmirror.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c"
integrity sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==

"@types/unist@*", "@types/unist@^2.0.2":
version "2.0.6"
resolved "https://registry.npmmirror.com/@types/unist/-/unist-2.0.6.tgz#250a7b16c3b91f672a24552ec64678eeb1d3a08d"
Expand Down Expand Up @@ -1085,11 +1080,6 @@ escape-string-regexp@^1.0.5:
resolved "https://registry.npmmirror.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
integrity sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg==

escape-string-regexp@^2.0.0:
version "2.0.0"
resolved "https://registry.npmmirror.com/escape-string-regexp/-/escape-string-regexp-2.0.0.tgz#a30304e99daa32e23b2fd20f51babd07cffca344"
integrity sha512-UpzcLCXolUWcNu5HtVMHYdXJjArjsF9C0aNnquZYY4uW/Vu0miy5YoWvbV345HauVvcAUnpRuhMMcqTcGOY2+w==

eslint-import-resolver-node@^0.3.6:
version "0.3.6"
resolved "https://registry.npmmirror.com/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.6.tgz#4048b958395da89668252001dbd9eca6b83bacbd"
Expand Down Expand Up @@ -2830,13 +2820,6 @@ spdx-license-ids@^3.0.0:
resolved "https://registry.npmmirror.com/spdx-license-ids/-/spdx-license-ids-3.0.11.tgz#50c0d8c40a14ec1bf449bae69a0ea4685a9d9f95"
integrity sha512-Ctl2BrFiM0X3MANYgj3CkygxhRmr9mi6xhejbdO960nF6EDJApTYpn0BQnDKlnNBULKiCN1n3w9EBkHK8ZWg+g==

stack-utils@^2.0.5:
version "2.0.5"
resolved "https://registry.npmmirror.com/stack-utils/-/stack-utils-2.0.5.tgz#d25265fca995154659dbbfba3b49254778d2fdd5"
integrity sha512-xrQcmYhOsn/1kX+Vraq+7j4oE2j/6BFscZ0etmYg81xuM8Gq0022Pxb8+IqgOFUIaxHs0KaSb7T1+OegiNrNFA==
dependencies:
escape-string-regexp "^2.0.0"

string-width@^4.1.0, string-width@^4.2.0:
version "4.2.3"
resolved "https://registry.npmmirror.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
Expand Down

0 comments on commit 9db4fec

Please sign in to comment.