Files
apes-sidebar/scripts/win-engine-check.mjs
T
Menghuan1918andCold Cloud 194099e7f3 feat(search): fs.search 文件名搜索优先用 fd/rg,缺失/失败回退 JS 遍历
issue #203:十万级目录下纯 JS 递归遍历是秒级,10 万条访问预算一顶结果
还被静默截断。现在启动后惰性探测一次本机 fd / rg(每个候选 `--version`
500ms 内验证通过才算数,进程内缓存),探到就用引擎输出,探不到或运行失败
整段回退原来的遍历(导出为 `searchFilesPlain`);坏二进制在进程内禁用,
超时只降级本次(树大 ≠ 二进制坏)。

为什么按 dev 现状重写而不是机械解冲突(PR #303 的落点全部复核过):

- **#801 的必填 `dirs` 是语义级冲突,必须扩引擎契约**。dev 的
  `FsSearchResult` 多了 `dirs`(`TreePanel` 用它把命中行当目录导航,否则
  `fs.read` 报 “is a directory”),而 PR 的引擎侧只产出
  `{paths, truncated}`,`searchFilesPlain` 的空查询也还在返回两字段形状;
  照搬会让 #801 修的 bug 在引擎路径上静默回退。收口:fd 用第二次
  `--type d` 调用取目录子集(fd 自己不做标注),rg 由文件路径倒推匹配的
  目录段(`deriveRgMatches`),dispatch 再把 `dirs` 收敛成 `matches` 的
  子集。残留差异只有**空目录在 rg 下不可见**(没有文件路径能带上它)。
- **rg 的路径级 glob 补成三个**:PR 的 `**/*<q>*/**` 缺一个直接子文件
  (`util/helper.ts` 是最常见形态),只加 `**` 会让这类目录命中在
  rg-only 机器上凭空消失;`**/*<q>/*` + `**/*<q>/**` 两条并用(对真实
  rg 15 实测:单用任一条都丢结果)。
- **`bundledRgCandidates` 的路径推导按真机布局改写**:npm/pnpm 把
  `@vscode/ripgrep-<platform>-<arch>` 提升到 `@deepseek-ai` 的**同级**,
  不存在 `<root>/@deepseek-ai/dsh/node_modules/@vscode/...`;候选根改成
  「从 CLI 入口(argv[1])向上逐级 `node_modules`」+ 各包管理器全局前缀,
  桌面壳那种「自带依赖树 + 外部 node」的布局才探得到捆绑 rg(实测命中)。
- **丢掉 abort 形参**:`src/index.ts` 的生产路径不传 signal(PR 里也只有
  测试在用),单为测试保留一条贯穿三层的通道不划算;引擎子进程由 15s
  内部超时兜底。
- **文档锚点按 dev 重锚**:README 的 v0.12.3 条目已随发布历史迁到
  CHANGELOG(新增 Unreleased 条目),AGENTS §2 的引擎门禁入口挂到现有
  ci-windows 段落,指南 §14 参考实现行补 `src/search-engines.ts`。
- 未新增任何依赖:`@vscode/ripgrep` 只作为**路径**探测,不做模块导入。

Windows 侧由既有 ci-windows lane 承载:`choco install fd ripgrep` 把真
二进制放进 PATH,套件后 `pnpm check:engines`(`scripts/win-engine-check.mjs
--assert`,import `src/search-engines.ts` 取插件自己的 argv)断言
`--path-separator /` 的输出不含 `\`/CR、非 ASCII 文件名命中;引擎缺失则
跳过(fd 在 ubuntu lane 与多数开发机上都没有)。

测试:`tests/search-engines.spec.ts`(探测缓存 / 运行失败禁用 / 超时不禁
用 / argv 对称性与噪声目录排除 / `deriveRgMatches` / 捆绑 rg 候选推导 /
真引擎在场时的端到端——缺席即返回),`tests/fs-search.spec.ts` 拆成
「直接钉遍历」+「注入假引擎的 dispatch 组」(引擎优先、`dirs` 透传与
子集收敛、预算透传、运行失败回退并禁用)。真机核对:本仓库上 rg 路径与
JS 遍历对 `locales` / `util` / `client` / `src` / `e2e` 五个查询的
matches 与 dirs **逐项一致**。

Refs #303

Co-authored-by: Cold Cloud <64406210+yanzhaohui1999@users.noreply.github.com>
2026-10-05 02:10:45 +08:00

82 lines
3.5 KiB
JavaScript

/**
* Engine output-shape gate for the fd / ripgrep binaries behind fs.search's
* file-name search. `node scripts/win-engine-check.mjs` prints what each
* installed engine emits (human inspection); `--assert` turns it into the
* CI gate the ci-windows lane runs after `choco install fd ripgrep`.
*
* The assertions cover exactly what the plugin's own argvs promise:
*
* - `--path-separator /` output carries no '\' and no CR — on Windows rg
* emits '\' paths in cmd/PowerShell and CRLF line endings, so without the
* pin every result path would leak a backslash into the walk contract.
* - A non-ASCII file name comes back through the UTF-8 pipeline instead of
* the console code page.
*
* A missing engine SKIPS (the script stays runnable on a dev machine and on
* the ubuntu lane, where fd is absent); an assertion failure exits non-zero.
* The argv strings come from src/search-engines.ts itself, so a changed argv
* cannot leave this gate asserting a shape nothing runs any more — hence the
* `pnpm check:engines` wrapper (`--experimental-strip-types`; on Node ≥ 23
* plain `node scripts/win-engine-check.mjs --assert` works too).
*/
import { execFileSync } from 'node:child_process'
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fdArgv, rgArgv } from '../src/search-engines.ts'
const assertMode = process.argv.includes('--assert')
/** Every run builds its own scratch tree (a runner and a dev machine share no fixed path). */
const scratch = mkdtempSync(join(tmpdir(), 'dsh-engine-check-'))
mkdirSync(join(scratch, 'src'))
writeFileSync(join(scratch, 'README.md'), 'readme')
writeFileSync(join(scratch, 'src', 'a.ts'), 'code')
writeFileSync(join(scratch, '中文文件名.ts'), 'code')
let failed = false
function run(label, binary, args, { checkUnicode = false } = {}) {
let stdout
try {
stdout = execFileSync(binary, args, { cwd: scratch, encoding: 'buffer' })
} catch (error) {
// A missing engine is a SKIP, not a failure: fd is absent on the ubuntu
// lane and on most dev machines.
console.log(`=== ${label} (${binary}) SKIPPED ===`)
console.log(String(error.stderr ?? error.message).split('\n')[0])
return
}
const hasBackslash = stdout.includes(92)
const hasCR = stdout.includes(13)
const text = stdout.toString('utf8')
console.log(`=== ${label} (${binary}) ===`)
console.log('backslash:', hasBackslash, '| CR:', hasCR)
console.log('raw:', JSON.stringify(text))
if (!assertMode) return
if (hasBackslash || hasCR) {
console.error(`ASSERT FAIL: ${label} leaked backslash/CR into output`)
failed = true
}
if (checkUnicode && !text.includes('中文文件名.ts')) {
console.error(`ASSERT FAIL: ${label} missed the non-ASCII file name`)
failed = true
}
}
if (process.argv.includes('--probe')) {
// --probe: what the plugin's own argv produces for a query that hits a
// directory, a file and a non-ASCII name. Manual, not asserted.
run('rg argv (query=util)', 'rg', rgArgv('util'), { checkUnicode: false })
run('fd argv (query=util)', 'fd', fdArgv(10, 'util'))
} else {
run('rg argv (query=中文)', 'rg', rgArgv('中文'), { checkUnicode: true })
run('fd argv (query=中文)', 'fd', fdArgv(10, '中文'), { checkUnicode: true })
// A query with no non-ASCII bytes still exercises the path-separator pin.
run('rg argv (query=a)', 'rg', rgArgv('a'))
run('fd argv (query=a)', 'fd', fdArgv(10, 'a'))
}
rmSync(scratch, { recursive: true, force: true })
if (failed) process.exit(1)