diff --git a/packages/vitest/src/typecheck/typechecker.ts b/packages/vitest/src/typecheck/typechecker.ts index 5a6345d988d7..530f126838d9 100644 --- a/packages/vitest/src/typecheck/typechecker.ts +++ b/packages/vitest/src/typecheck/typechecker.ts @@ -7,6 +7,7 @@ import type { Vitest } from '../node/core' import type { TestProject } from '../node/project' import type { File, Task, TaskEventPack, TaskResultPack, TaskState } from '../runtime/runner/types' import type { TscErrorInfo } from './types' +import { execFile } from 'node:child_process' import os from 'node:os' import { performance } from 'node:perf_hooks' import { eachMapping, generatedPositionFor, TraceMap } from '@jridgewell/trace-mapping' @@ -268,8 +269,11 @@ export class Typechecker { } public async stop(): Promise { - this.process?.kill() + const child = this.process this.process = undefined + if (child) { + await killProcessTree(child) + } } protected async ensurePackageInstalled(ctx: Vitest, checker: string): Promise { @@ -526,3 +530,24 @@ function findGeneratedPosition( } return { line: null, column: null } } + +function killProcessTree(child: ChildProcess): Promise { + // on Windows the checker runs behind a `cmd.exe` shim, and `kill()` stops only the shim + if (process.platform !== 'win32' || child.pid == null || child.exitCode != null) { + child.kill() + return Promise.resolve() + } + return new Promise((resolve) => { + execFile( + 'taskkill', + ['/pid', String(child.pid), '/T', '/F'], + { windowsHide: true }, + (error) => { + if (error) { + child.kill() + } + resolve() + }, + ) + }) +} diff --git a/test/typescript/test/typechecker.test.ts b/test/typescript/test/typechecker.test.ts index 9e53a191a3bb..80b38a124ce1 100644 --- a/test/typescript/test/typechecker.test.ts +++ b/test/typescript/test/typechecker.test.ts @@ -1,6 +1,7 @@ +import { chmodSync, existsSync, readFileSync } from 'node:fs' import { resolve } from 'pathe' -import { describe, expect, it } from 'vitest' -import { runVitest } from '../../test-utils' +import { describe, expect, it, onTestFinished } from 'vitest' +import { runVitest, ts, useTmpFS } from '../../test-utils' describe('Typechecker', () => { it('handles non-existing typechecker command gracefully', async () => { @@ -40,4 +41,59 @@ describe('Typechecker', () => { expect(stderr).toContain('before type checking finished') expect(stderr).toContain('ran out of memory') }) + + it('stops the watching checker when Vitest closes', async () => { + const fs = useTmpFS({ + 'fake-tsc.cmd': '@node "%~dp0fake-tsc.mjs" %*', + 'fake-tsc.mjs': ts`#!/usr/bin/env node + import { writeFileSync } from 'node:fs' + + writeFileSync('checker.pid', String(process.pid)) + console.log('Found 0 errors. Watching for file changes.') + setInterval(() => {}, 1000) + `, + 'basic.test-d.ts': ts` + import { expectTypeOf, test } from 'vitest' + + test('number', () => { + expectTypeOf(1).toEqualTypeOf() + }) + `, + }) + chmodSync(fs.resolveFile('fake-tsc.mjs'), 0o755) + + const { ctx, stderr } = await runVitest({ + root: fs.root, + watch: true, + typecheck: { + enabled: true, + only: true, + checker: fs.resolveFile(process.platform === 'win32' ? 'fake-tsc.cmd' : 'fake-tsc.mjs'), + }, + }) + + const pidFile = fs.resolveFile('checker.pid') + await expect.poll(() => existsSync(pidFile)).toBe(true) + const pid = Number(readFileSync(pidFile, 'utf-8')) + onTestFinished(() => { + if (isRunning(pid)) { + process.kill(pid) + } + }) + expect(isRunning(pid)).toBe(true) + + await ctx!.close() + + await expect.poll(() => isRunning(pid)).toBe(false) + expect(stderr).toBe('') + }) }) + +function isRunning(pid: number) { + try { + process.kill(pid, 0) + return true + } catch { + return false + } +}