Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"picomatch": "catalog:latest",
"vitest": "catalog:v3",
"vitest-vscode-shared": "workspace:*"
},
"devDependencies": {
"flatted": "^3.4.2"
}
}
10 changes: 8 additions & 2 deletions packages/extension/src/spawn/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ExtensionWorkerEvents, ExtensionWorkerTransport } from 'vitest-vscode-shared'
import v8 from 'node:v8'
import { createBirpc } from 'birpc'
import { log } from '../log'

export type {
ExtensionWorkerEvents,
Expand Down Expand Up @@ -64,6 +65,8 @@ export function createRpcOptions() {
export function createVitestRpc(options: {
on: (listener: (message: any) => void) => void
send: (message: any) => void
serialize?: (v: any) => any
deserialize?: (v: any) => any
}) {
const { events, handlers } = createRpcOptions()

Expand All @@ -76,8 +79,11 @@ export function createVitestRpc(options: {
post(message) {
options.send(message)
},
serialize: v8.serialize,
deserialize: (v) => v8.deserialize(Buffer.from(v) as any),
serialize: options.serialize ?? v8.serialize,
deserialize: options.deserialize ?? ((v) => v8.deserialize(Buffer.from(v) as any)),
onGeneralError(error) {
log.error('RPC Error', error)
},
})

return {
Expand Down
17 changes: 17 additions & 0 deletions packages/extension/src/spawn/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { log } from '../log'
import { createVitestRpc } from './rpc'
import { resolve } from 'pathe'
import { parse, stringify } from 'flatted'

export type WsConnectionMetadata = Omit<ResolvedMeta, 'process'> & {
ws: WebSocket
Expand Down Expand Up @@ -83,6 +84,21 @@ export function onWsConnection(
const { api, handlers } = createVitestRpc({
on: (listener) => ws.on('message', listener),
send: (message) => ws.send(message),
serialize:
pkg.runtime !== 'node'
? (e) =>
stringify(e, (_, v) => {
if (v instanceof Error) {
return {
name: v.name,
message: v.message,
stack: v.stack,
}
}
return v
})
: undefined,
deserialize: pkg.runtime !== 'node' ? parse : undefined,
})
ws.once('close', () => {
log.verbose?.('[API]', 'Vitest WebSocket connection closed, cannot call RPC anymore.')
Expand Down Expand Up @@ -153,6 +169,7 @@ export function onWsConnection(
env: getConfig(pkg.folder).env || undefined,
configFile: pkg.configFile,
cwd: pkg.cwd,
runtime: pkg.runtime,
arguments: pkg.arguments,
workspaceFile: pkg.workspaceFile,
id: pkg.id,
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function formatPkg(pkg: VitestPackage) {
}

function _showVitestError(message: string, error?: any) {
if (error) log.error(error)
if (error) log.error(error.stack || error)

vscode.window
.showErrorMessage(`${message}. Check the output for more details.`, 'See error')
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { VitestProcessAPI } from './apiProcess'
import type { TransformSchemaProvider } from './schemaProvider'
import type { TestTree } from './testTree'
import { relative } from 'node:path'
import { normalize, resolve } from 'pathe'
import { normalize } from 'pathe'
import * as vscode from 'vscode'
import { getConfig } from './config'
import { log } from './log'
Expand Down
19 changes: 17 additions & 2 deletions packages/extension/src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { pathToFileURL } from 'node:url'
import v8 from 'node:v8'
import { createWorkerRPC, normalizeDriveLetter, WorkerWSEventEmitter } from 'vitest-vscode-shared'
import { WebSocket } from 'ws'
import { parse, stringify } from 'flatted'

// this is the file that will be executed with "node <path>"

Expand Down Expand Up @@ -53,8 +54,22 @@ emitter.on('message', async function onMessage(message: any) {
post(message) {
emitter.send(message)
},
serialize: v8.serialize,
deserialize: (v) => v8.deserialize(Buffer.from(v) as any),
serialize:
data.meta.runtime !== 'node'
? (e) =>
stringify(e, (_, v) => {
if (v instanceof Error) {
return {
name: v.name,
message: v.message,
stack: v.stack,
}
}
return v
})
: v8.serialize,
deserialize:
data.meta.runtime !== 'node' ? parse : (v) => v8.deserialize(Buffer.from(v) as any),
})
worker.initRpc(rpc)
reporter.initRpc(rpc)
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export interface WorkerInitMetadata {
id: string
cwd: string
arguments?: string
runtime: 'node' | 'deno'
configFile?: string
workspaceFile?: string
env: Record<string, any> | undefined
Expand Down
4 changes: 2 additions & 2 deletions packages/worker-legacy/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ export class ExtensionWorker implements ExtensionWorkerTransport {

private async globTestSpecifications(filters?: string[]): Promise<TestSpecification[]> {
if ('globTestSpecifications' in this.vitest) {
return this.vitest.globTestSpecifications(filters)
return this.vitest.globTestSpecifications(filters || [])
}
return await (this.vitest as any).globTestFiles(filters)
return await (this.vitest as any).globTestFiles(filters || [])
}

private invalidateTree(mod: any, seen = new Set()) {
Expand Down
4 changes: 3 additions & 1 deletion packages/worker/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export class ExtensionWorkerRunner {
}

if (!filesOrDirectories || this.isOnlyDirectories(filesOrDirectories)) {
const specifications = await this.vitest.getRelevantTestSpecifications(filesOrDirectories)
const specifications = await this.vitest.getRelevantTestSpecifications(
filesOrDirectories || [],
)
await this.vitest.rerunTestSpecifications(specifications, true)
} else {
const specifications = await this.resolveTestSpecifications(filesOrDirectories)
Expand Down
Loading
Loading