Skip to content

Commit

Permalink
fix: fully support yarn pnp (#475)
Browse files Browse the repository at this point in the history
Closes #426
  • Loading branch information
sheremet-va authored Sep 26, 2024
1 parent ba0a061 commit 9344570
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ vite.config.ts
tsconfig.json
tsconfig.base.json
.github/**
.eslintcache
.vscode-test.mjs
eslint.config.mjs
scripts/**
test-results/**
9 changes: 5 additions & 4 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ async function createChildVitestProcess(pkg: VitestPackage) {
log.error('[API]', errorMsg)
throw new Error(errorMsg)
}
log.info('[API]', `Running ${formapPkg(pkg)} with Node.js: ${execPath}`)
log.info('[API]', `Running ${formapPkg(pkg)} with Node.js: ${execPath} ${execArgv ? execArgv.join(' ') : ''}`)
const logLevel = getConfig(pkg.folder).logLevel
const vitest = fork(
// to support pnp, we need to spawn `yarn node` instead of `node`
Expand Down Expand Up @@ -363,10 +363,11 @@ async function createChildVitestProcess(pkg: VitestPackage) {
arguments: pkg.arguments,
workspaceFile: pkg.workspaceFile,
id: pkg.id,
pnpApi: pnp,
pnpLoader: pnpLoader && gte(process.version, '18.19.0')
? pathToFileURL(pnpLoader).toString()
: undefined,
},
loader: pnpLoader && gte(process.version, '18.19.0')
? pathToFileURL(pnpLoader).toString()
: undefined,
}

vitest.send(runnerOptions)
Expand Down
9 changes: 8 additions & 1 deletion src/api/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type * as vscode from 'vscode'
import { dirname, resolve } from 'pathe'
import { getConfig } from '../config'

const _require = require

export interface VitestResolution {
vitestPackageJsonPath: string
vitestNodePath: string
Expand All @@ -24,8 +26,13 @@ export function resolveVitestPackage(cwd: string, folder: vscode.WorkspaceFolder
const pnp = resolveVitestPnpPackagePath(folder?.uri.fsPath || cwd)
if (!pnp)
return null
const pnpApi = _require(pnp.pnpPath)
const vitestNodePath = pnpApi.resolveRequest('vitest/node', cwd)
if (!vitestNodePath) {
return null
}
return {
vitestNodePath: 'vitest/node',
vitestNodePath,
vitestPackageJsonPath: 'vitest/package.json',
pnp: {
loaderPath: pnp.pnpLoader,
Expand Down
9 changes: 9 additions & 0 deletions src/debug/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createServer } from 'node:http'
import { pathToFileURL } from 'node:url'
import * as vscode from 'vscode'
import WebSocket, { WebSocketServer } from 'ws'
import getPort from 'get-port'
import { gte } from 'semver'
import type { ResolvedMeta } from '../api'
import { VitestFolderAPI } from '../api'
import type { VitestPackage } from '../api/pkg'
Expand Down Expand Up @@ -185,6 +187,9 @@ function startWebsocketServer(wss: WebSocketServer, pkg: VitestPackage) {
ws.on('message', onMessage)
ws.on('close', onExit)

const pnpLoader = pkg.loader
const pnp = pkg.pnp

const runnerOptions: WorkerRunnerOptions = {
type: 'init',
meta: {
Expand All @@ -195,6 +200,10 @@ function startWebsocketServer(wss: WebSocketServer, pkg: VitestPackage) {
arguments: pkg.arguments,
workspaceFile: pkg.workspaceFile,
id: pkg.id,
pnpApi: pnp,
pnpLoader: pnpLoader && gte(process.version, '18.19.0')
? pathToFileURL(pnpLoader).toString()
: undefined,
},
}

Expand Down
2 changes: 1 addition & 1 deletion src/testTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class TestTree extends vscode.Disposable {
testItem.range = new vscode.Range(position, position)
}
else {
log.error(`Cannot find location for ${testItem.label}. Using "id" to sort instead.`)
log.error(`Cannot find location for "${testItem.label}". Using "id" to sort instead.`)
testItem.sortText = task.id
}
if (task.type === 'suite')
Expand Down
11 changes: 11 additions & 0 deletions src/worker/debug.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import v8 from 'node:v8'
import { register } from 'node:module'
import { WebSocket } from 'ws'
import type { WorkerRunnerOptions } from './types'
import { initVitest } from './init'
import { Vitest } from './vitest'
import { createWorkerRPC } from './rpc'
import { WorkerWSEventEmitter } from './emitter'

const _require = require

const ws = new WebSocket(process.env.VITEST_WS_ADDRESS!)
const emitter = new WorkerWSEventEmitter(ws)

Expand All @@ -19,6 +22,14 @@ ws.on('message', async function onMessage(_data) {
const data = message as WorkerRunnerOptions

try {
if (data.meta.pnpApi) {
_require(data.meta.pnpApi).setup()
}

if (data.meta.pnpLoader) {
register(data.meta.pnpLoader)
}

const pkg = data.meta

const vitest = await initVitest(pkg, {
Expand Down
24 changes: 24 additions & 0 deletions src/worker/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import type { WorkerMeta } from './types'
export async function initVitest(meta: WorkerMeta, options?: UserConfig) {
const vitestModule = await import(meta.vitestNodePath) as typeof import('vitest/node')
const reporter = new VSCodeReporter()
const pnpExecArgv = meta.pnpApi && meta.pnpLoader
? [
'--require',
meta.pnpApi,
'--experimental-loader',
meta.pnpLoader,
]
: undefined
const vitest = await vitestModule.createVitest(
'test',
{
Expand All @@ -19,6 +27,22 @@ export async function initVitest(meta: WorkerMeta, options?: UserConfig) {
reporters: [reporter],
ui: false,
includeTaskLocation: true,
poolOptions: meta.pnpApi && meta.pnpLoader
? {
threads: {
execArgv: pnpExecArgv,
},
forks: {
execArgv: pnpExecArgv,
},
vmForks: {
execArgv: pnpExecArgv,
},
vmThreads: {
execArgv: pnpExecArgv,
},
}
: undefined,
},
{
server: {
Expand Down
3 changes: 2 additions & 1 deletion src/worker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ export interface WorkerMeta {
configFile?: string
workspaceFile?: string
env: Record<string, any> | undefined
pnpApi?: string
pnpLoader?: string
}

export interface WorkerRunnerOptions {
type: 'init'
meta: WorkerMeta
loader?: string
}

export interface EventReady {
Expand Down
10 changes: 8 additions & 2 deletions src/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Vitest } from './vitest'
import { initVitest } from './init'
import { WorkerProcessEmitter } from './emitter'

const _require = require
const emitter = new WorkerProcessEmitter()

process.on('message', async function onMessage(message: any) {
Expand All @@ -14,8 +15,13 @@ process.on('message', async function onMessage(message: any) {
const data = message as WorkerRunnerOptions

try {
if (data.loader)
register(data.loader)
if (data.meta.pnpApi) {
_require(data.meta.pnpApi).setup()
}

if (data.meta.pnpLoader) {
register(data.meta.pnpLoader)
}

const { reporter, vitest } = await initVitest(data.meta)

Expand Down

0 comments on commit 9344570

Please sign in to comment.