From 204d15b217c2238d950bbf396bd6815f20f82007 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 20 Mar 2024 10:47:47 +0100 Subject: [PATCH] fix: correctly resolve folder id --- src/api.ts | 24 ++++++++++++++---------- src/runner/runner.ts | 8 ++++---- src/worker/rpc.ts | 2 +- src/worker/types.ts | 13 +++++++++++++ src/worker/worker.ts | 41 ++++++++++++++--------------------------- 5 files changed, 46 insertions(+), 42 deletions(-) create mode 100644 src/worker/types.ts diff --git a/src/api.ts b/src/api.ts index 667df9b9..29822e0d 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,5 +1,6 @@ import type { ChildProcess } from 'node:child_process' import { fork } from 'node:child_process' +import { pathToFileURL } from 'node:url' import { gte } from 'semver' import { basename, dirname, normalize } from 'pathe' import * as vscode from 'vscode' @@ -10,12 +11,13 @@ import type { BirpcEvents, VitestEvents, VitestRPC } from './api/rpc' import { createVitestRpc } from './api/rpc' import { resolveVitestPackage } from './api/resolve' import type { TestTree } from './testTree' +import type { WorkerRunnerOptions } from './worker/types' const _require = require export class VitestReporter { constructor( - protected folderFsPath: string, + protected id: string, protected handlers: ResolvedMeta['handlers'], ) {} @@ -35,8 +37,8 @@ export class VitestReporter { private createHandler>(name: K) { return (callback: VitestEvents[K]) => { - this.handlers[name]((folder, ...args) => { - if (folder === this.folderFsPath) + this.handlers[name]((id, ...args) => { + if (id === this.id) (callback as any)(...args) }) } @@ -79,11 +81,12 @@ export class VitestFolderAPI extends VitestReporter { constructor( folder: vscode.WorkspaceFolder, private meta: ResolvedMeta, - public readonly id: string, + id: string, ) { - super(normalize(folder.uri.fsPath), meta.handlers) + const normalizedId = normalize(id) + super(normalizedId, meta.handlers) WEAKMAP_API_FOLDER.set(this, folder) - this.id = normalize(id) + this.id = normalizedId // TODO: make it prettier, but still unique this.tag = new vscode.TestTag(this.id) } @@ -271,7 +274,7 @@ function createChildVitestProcess(tree: TestTree, meta: VitestMeta[]) { '--require', pnp, '--experimental-loader', - pnpLoaders[0], + pathToFileURL(pnpLoaders[0]).toString(), ] : undefined const vitest = fork( @@ -325,18 +328,19 @@ function createChildVitestProcess(tree: TestTree, meta: VitestMeta[]) { } }) vitest.on('spawn', () => { - vitest.send({ + const runnerOptions: WorkerRunnerOptions = { type: 'init', meta: meta.map(m => ({ vitestNodePath: m.vitestNodePath, - folder: normalize(m.folder.uri.fsPath), env: getConfig(m.folder).env || undefined, configFile: m.configFile, workspaceFile: m.workspaceFile, id: m.id, })), loader: pnpLoaders[0] && gte(process.version, '18.19.0') ? pnpLoaders[0] : undefined, - }) + } + + vitest.send(runnerOptions) }) }) } diff --git a/src/runner/runner.ts b/src/runner/runner.ts index 8666a4af..a21db120 100644 --- a/src/runner/runner.ts +++ b/src/runner/runner.ts @@ -26,7 +26,7 @@ export class TestRunner extends vscode.Disposable { this.endTestRuns() }) - api.onWatcherRerun(() => this.startTestRun()) + api.onWatcherRerun(files => this.startTestRun(files)) api.onTaskUpdate((packs) => { packs.forEach(([testId, result]) => { @@ -108,9 +108,9 @@ export class TestRunner extends vscode.Disposable { const testNamePatern = formatTestPattern(tests) const files = getTestFiles(tests) if (testNamePatern) - log.info(`Running ${files.length} files with name pattern: ${testNamePatern}`) + log.info(`Running ${files.length} file(s) with name pattern: ${testNamePatern}`) else - log.info(`Running ${files.length} files`) + log.info(`Running ${files.length} file(s):`, files) await this.api.runFiles(files, testNamePatern) } @@ -131,7 +131,7 @@ export class TestRunner extends vscode.Disposable { } } - private startTestRun() { + private startTestRun(_files: string[]) { // TODO: refactor to use different requests, otherwise test run doesn't mark the result value! const currentRequest = this.testRunRequests.values().next().value as vscode.TestRunRequest | undefined if (currentRequest) { diff --git a/src/worker/rpc.ts b/src/worker/rpc.ts index cc0d6c7b..7c732722 100644 --- a/src/worker/rpc.ts +++ b/src/worker/rpc.ts @@ -40,7 +40,7 @@ export function createWorkerRPC(vitest: Vitest[], channel: ChannelOptions) { async function globTestFiles(vitest: Vitest, filters?: string[]) { const cwd = process.cwd() - process.chdir(vitest.config.root) + process.chdir(dirname(getId(vitest))) const files = await vitest.globTestFiles(filters) process.chdir(cwd) return files diff --git a/src/worker/types.ts b/src/worker/types.ts new file mode 100644 index 00000000..1cdd06a9 --- /dev/null +++ b/src/worker/types.ts @@ -0,0 +1,13 @@ +export interface WorkerMeta { + vitestNodePath: string + id: string + configFile?: string + workspaceFile?: string + env: Record | undefined +} + +export interface WorkerRunnerOptions { + type: 'init' + meta: WorkerMeta[] + loader?: string +} diff --git a/src/worker/worker.ts b/src/worker/worker.ts index a051c592..f74ed07a 100644 --- a/src/worker/worker.ts +++ b/src/worker/worker.ts @@ -6,30 +6,16 @@ import type { BirpcReturn } from 'birpc' import type { File, Reporter, TaskResultPack, UserConsoleLog, Vitest } from 'vitest' import type { BirpcEvents, BirpcMethods } from '../api/rpc' import { createWorkerRPC } from './rpc' - -interface VitestMeta { - folder: string - vitestNodePath: string - id: string - configFile?: string - workspaceFile?: string - env: Record | undefined -} - -interface RunnerOptions { - type: 'init' - meta: VitestMeta[] - loader?: string -} +import type { WorkerMeta, WorkerRunnerOptions } from './types' class VSCodeReporter implements Reporter { private rpc!: BirpcReturn private ctx!: Vitest - private folder!: string + private id!: string - initVitest(ctx: Vitest) { + initVitest(ctx: Vitest, id: string) { this.ctx = ctx - this.folder = ctx.config.root + this.id = id } initRpc(rpc: BirpcReturn) { @@ -37,7 +23,7 @@ class VSCodeReporter implements Reporter { } onUserConsoleLog(log: UserConsoleLog) { - this.rpc.onConsoleLog(this.folder, log) + this.rpc.onConsoleLog(this.id, log) } onTaskUpdate(packs: TaskResultPack[]) { @@ -53,29 +39,30 @@ class VSCodeReporter implements Reporter { }) }) - this.rpc.onTaskUpdate(this.folder, packs) + this.rpc.onTaskUpdate(this.id, packs) } onFinished(files?: File[], errors?: unknown[]) { - this.rpc.onFinished(this.folder, files, errors) + this.rpc.onFinished(this.id, files, errors) } onCollected(files?: File[]) { - this.rpc.onCollected(this.folder, files) + this.rpc.onCollected(this.id, files) } onWatcherStart(files?: File[], errors?: unknown[]) { - this.rpc.onWatcherStart(this.folder, files, errors) + this.rpc.onWatcherStart(this.id, files, errors) } onWatcherRerun(files: string[], trigger?: string) { - this.rpc.onWatcherRerun(this.folder, files, trigger) + this.rpc.onWatcherRerun(this.id, files, trigger) } } -async function initVitest(meta: VitestMeta) { +async function initVitest(meta: WorkerMeta) { const vitestMode = await import(meta.vitestNodePath) as typeof import('vitest/node') const reporter = new VSCodeReporter() + _debug('root', dirname(meta.id)) const vitest = await vitestMode.createVitest( 'test', { @@ -95,7 +82,7 @@ async function initVitest(meta: VitestMeta) { }, }, ) - reporter.initVitest(vitest) + reporter.initVitest(vitest, meta.id) return { vitest, reporter, @@ -107,7 +94,7 @@ const cwd = process.cwd() process.on('message', async function init(message: any) { if (message.type === 'init') { process.off('message', init) - const data = message as RunnerOptions + const data = message as WorkerRunnerOptions try { if (data.loader)