Skip to content

Commit

Permalink
fix(vitest): cache fs code only for forks pool (#5909)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jun 17, 2024
1 parent e6020b9 commit e30d9b4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/vitest/src/node/pools/forks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function createChildProcessChannel(project: WorkspaceProject) {
postMessage: message => emitter.emit(events.response, message),
}

const rpc = createBirpc<RunnerRPC, RuntimeRPC>(createMethodsRPC(project), {
const rpc = createBirpc<RunnerRPC, RuntimeRPC>(createMethodsRPC(project, { cacheFs: true }), {
eventNames: ['onCancel'],
serialize: v8.serialize,
deserialize: v => v8.deserialize(Buffer.from(v)),
Expand Down
9 changes: 7 additions & 2 deletions packages/vitest/src/node/pools/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import type { WorkspaceProject } from '../workspace'
const created = new Set()
const promises = new Map<string, Promise<void>>()

export function createMethodsRPC(project: WorkspaceProject): RuntimeRPC {
interface MethodsOptions {
cacheFs?: boolean
}

export function createMethodsRPC(project: WorkspaceProject, options: MethodsOptions = {}): RuntimeRPC {
const ctx = project.ctx
const cacheFs = options.cacheFs ?? false
return {
snapshotSaved(snapshot) {
ctx.snapshot.add(snapshot)
Expand All @@ -30,7 +35,7 @@ export function createMethodsRPC(project: WorkspaceProject): RuntimeRPC {
async fetch(id, transformMode) {
const result = await project.vitenode.fetchResult(id, transformMode)
const code = result.code
if (result.externalize) {
if (!cacheFs || result.externalize) {
return result
}
if ('id' in result && typeof result.id === 'string') {
Expand Down
29 changes: 16 additions & 13 deletions packages/vitest/src/node/pools/vmForks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,23 @@ function createChildProcessChannel(project: WorkspaceProject) {
postMessage: message => emitter.emit(events.response, message),
}

const rpc = createBirpc<RunnerRPC, RuntimeRPC>(createMethodsRPC(project), {
eventNames: ['onCancel'],
serialize: v8.serialize,
deserialize: v => v8.deserialize(Buffer.from(v)),
post(v) {
emitter.emit(events.message, v)
const rpc = createBirpc<RunnerRPC, RuntimeRPC>(
createMethodsRPC(project, { cacheFs: true }),
{
eventNames: ['onCancel'],
serialize: v8.serialize,
deserialize: v => v8.deserialize(Buffer.from(v)),
post(v) {
emitter.emit(events.message, v)
},
on(fn) {
emitter.on(events.response, fn)
},
onTimeoutError(functionName) {
throw new Error(`[vitest-pool]: Timeout calling "${functionName}"`)
},
},
on(fn) {
emitter.on(events.response, fn)
},
onTimeoutError(functionName) {
throw new Error(`[vitest-pool]: Timeout calling "${functionName}"`)
},
})
)

project.ctx.onCancel(reason => rpc.onCancel(reason))

Expand Down

0 comments on commit e30d9b4

Please sign in to comment.