Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle error from pool.run #1348

Merged
merged 4 commits into from May 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 19 additions & 8 deletions packages/vitest/src/node/pool.ts
Expand Up @@ -9,6 +9,7 @@ import type { RawSourceMap } from 'vite-node'
import type { WorkerContext, WorkerRPC } from '../types'
import { distDir } from '../constants'
import type { Vitest } from './core'
import { AggregateError } from '../utils'

export type RunWithFiles = (files: string[], invalidates?: string[]) => Promise<void>

Expand Down Expand Up @@ -41,10 +42,12 @@ export function createFakePool(ctx: Vitest): WorkerPool {
id: 1,
}

await worker[name](data, { transferList: [workerPort] })

port.close()
workerPort.close()
try {
await worker[name](data, { transferList: [workerPort] })
} finally {
port.close()
workerPort.close()
}
}
}

Expand Down Expand Up @@ -80,7 +83,7 @@ export function createWorkerPool(ctx: Vitest): WorkerPool {
return async (files, invalidates) => {
let id = 0
const config = ctx.getSerializableConfig()
await Promise.all(files.map(async (file) => {
const results = await Promise.allSettled(files.map(async (file) => {
const { workerPort, port } = createChannel(ctx)

const data: WorkerContext = {
Expand All @@ -91,10 +94,18 @@ export function createWorkerPool(ctx: Vitest): WorkerPool {
id: ++id,
}

await pool.run(data, { transferList: [workerPort], name })
port.close()
workerPort.close()
try {
await pool.run(data, { transferList: [workerPort], name })
} finally {
port.close()
workerPort.close()
}
}))

const errors = results.filter((r): r is PromiseRejectedResult => r.status === 'rejected').map(r => r.reason)
if (errors.length > 0) {
throw new AggregateError(errors)
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions packages/vitest/src/utils/index.ts
Expand Up @@ -129,3 +129,14 @@ export function getCallLastIndex(code: string) {
}

export { resolve as resolvePath }

class AggregateErrorPonyfill extends Error {
errors: unknown[]
constructor(errors: Iterable<unknown>, message = '') {
super(message)
this.errors = [...errors]
}
}

// AggregateError is supported in Node.js 15.0.0+
export const AggregateError = global.AggregateError || AggregateErrorPonyfill
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved