From 88831b0729fe60c9074f162de7dceafc6ca0e011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E7=88=B1=E6=88=91?= <2381622265@qq.com> Date: Thu, 22 Oct 2026 18:15:43 +0800 Subject: [PATCH] fix(search): handle synchronous engine probe spawn failures --- src/search-engines.ts | 3 ++- tests/search-engines.spec.ts | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/search-engines.ts b/src/search-engines.ts index a454156f..f7e1a3b1 100644 --- a/src/search-engines.ts +++ b/src/search-engines.ts @@ -179,6 +179,7 @@ function candidates(engine: Engine): string[] { function verify(binary: string): Promise { return new Promise((resolve) => { let settled = false + let timer: ReturnType | undefined = undefined const finish = (ok: boolean): void => { if (settled) return settled = true @@ -192,7 +193,7 @@ function verify(binary: string): Promise { finish(false) return } - const timer = setTimeout(() => { + timer = setTimeout(() => { child.kill() finish(false) }, PROBE_TIMEOUT_MS) diff --git a/tests/search-engines.spec.ts b/tests/search-engines.spec.ts index d9f9e6ad..40205ee4 100644 --- a/tests/search-engines.spec.ts +++ b/tests/search-engines.spec.ts @@ -9,10 +9,10 @@ * machines have no fd (and only sometimes rg), and the probe/runner contracts * are what the dispatch depends on. */ -import { afterEach, describe, expect, it } from 'vitest' +import { afterEach, describe, expect, it, vi } from 'vitest' import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' -import type { ChildProcess } from 'node:child_process' +import { spawn, type ChildProcess } from 'node:child_process' import { PassThrough } from 'node:stream' import { join, posix, win32 } from 'node:path' import { searchFiles } from '../src/fs-search.ts' @@ -37,6 +37,26 @@ import { removeTree } from './temp-tree.ts' const fdProbe: EngineProbe = { engine: 'fd', binary: '/fake/fd' } const rgProbe: EngineProbe = { engine: 'rg', binary: '/fake/rg' } +vi.mock('node:child_process', { spy: true }) + +describe('engine probe spawn failures (#995)', () => { + it('falls back to the plain walk when spawning a probe throws synchronously', async () => { + resetEngines() + const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-probe-failure-')) + writeFileSync(join(dir, 'needle.txt'), 'found by the plain walk') + vi.mocked(spawn).mockImplementation(() => { throw new Error('spawn blocked') }) + try { + await expect(searchFiles(dir, 'needle')).resolves.toMatchObject({ matches: ['needle.txt'] }) + expect(spawn).toHaveBeenCalled() + await expect(probeEngines()).resolves.toEqual([]) + } finally { + vi.mocked(spawn).mockRestore() + resetEngines() + removeTree(dir) + } + }) +}) + describe('normalizeEnginePaths', () => { it('keeps root-relative /-separated lines as-is (fd contract)', () => { expect(normalizeEnginePaths(['src/util.ts', 'README.md'])).toEqual([