From 53c13638cd76819aa9581027ba38aaf0d973dcde Mon Sep 17 00:00:00 2001 From: Menghuan1918 Date: Mon, 5 Oct 2026 02:58:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(search-engines,e2e):=20Windows=20=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=8E=A8=E5=AF=BC=E6=8C=89=E5=85=A5=E5=8F=82=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E3=80=81fixture=20=E6=8D=A2=E5=90=88=E6=B3=95?= =?UTF-8?q?=E5=85=83=E5=AD=97=E7=AC=A6=E3=80=81=E5=B1=95=E5=BC=80=E6=96=AD?= =?UTF-8?q?=E8=A8=80=E6=94=B9=E4=B8=BA=E9=A2=84=E5=8F=96=E6=84=9F=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 集成批 17556fd 在 CI 上暴露三处红(Ubuntu `ci` 绿、Windows 测试红 2 条、挂载 lane 红 1 条), 逐条根因与修法: 1) tests/search-engines.spec.ts > bundledRgCandidates > walks every node_modules level above the CLI entry (argv[1])(仅 Windows 红) CI: AssertionError: expected [ …(8) ] to include '\Applications\Dsh.app\Contents\Resour…' 根因:bundledRgCandidates() 按「传入的 platform」推导候选,内部却用宿主的 sep / dirname / join。 测试传的是 POSIX 路径(/Applications/Dsh.app/…),Windows runner 上 sep === '\\' → endsWith('node_modules') 永不成立 → argv[1] 走查一个 root 都产不出,期望项缺失。 修法(修源头,不改测试语义):按 platform 选 node:path 方言 (`const P = platform === 'win32' ? win32 : posix`),walkRoots 与 %APPDATA% / execPath / profile / 候选拼装一律走 P.dirname / P.join / P.sep。函数自此是入参的纯函数:darwin 在任何 宿主上都产出 '/'-joined 候选,win32 都产出 '\'-joined 候选。 测试同步:darwin 用例期望改用 posix.join、win32 用例改用 win32.join——宿主 join 正是 Windows 上把 POSIX 期望拼成反斜杠、POSIX 上又把 win32 期望拼成混合分隔符的原因; argv[1] 那条补齐 win32 反向断言(在 macOS 上直接跑出真实反斜杠形状),%APPDATA%、 ~/.dsh profile、APPDATA 缺席三条从「结构包含」收紧为精确 pin win32 形状。 2) tests/search-engines.spec.ts > real engines (only when installed) > matches literals, splits dirs, skips noise when an engine is installed(仅 Windows 红) CI: Error: ENOENT: no such file or directory, open 'C:\Users\RUNNER~1\AppData\Local\Temp\dsh-sidebar-engine-real-oCa7RG\a*b.ts' (tests/search-engines.spec.ts:432) 根因:为验证「模式里的元字符按字面处理」而真建了一个叫 a*b.ts 的文件——'*'(连同 ? : " < > |) 是 Windows 非法文件名字符,fixture 自己就 ENOENT,一条断言都没跑到。 修法:按 platform 选一对「两平台都合法、且非字面解读必然命中诱饵」的见证。POSIX 保留 query 'a*b' / literal 'a*b.ts' / decoy 'anb.ts';win32 改用 '[' ']'(Windows 合法文件名 字符)——query 'a[b]' / literal 'a[b].ts' / decoy 'ab.ts'。'a[b]' 是 glob 字符组,漏转义时 匹配的是 'ab.ts' 而不是字面文件(实测 rg 15.2.0:未转义 '*a[b]*' 返回 ab.ts,转义 '*a\[b\]*' 返回 a[b].ts)。刻意不用未闭合的 'a[b':rg 直接拒绝该 glob(unclosed character class),引擎被判 broken 后回退普通 walk,断言会被兜底意外满足。整条用例不跳过、 「元字符不当通配符」的断言不削弱。 3) tests/e2e/zz-expand-refresh.e2e.ts > a folder toggle re-lists that folder and nothing else (挂载 lane 红) CI: Expected: "fs.trees[bravo]" / Received: undefined,同用例 phase dump: D-expand-bravo: requests: [], domAdded: 1, ... A-expand-big: requests: ["fs.trees[big]"], domAdded: 41, ... 根因(是预期行为,不是缺陷):src/fs-tree.ts 的 probeCompactRows 明确把「唯一子项是文件」的 目录也标 compact(客户端据此预取并稳定折叠标签),而 bravo 恰好只有一个文件;FileTree.tsx 的 loadLevels(compactLoadTargets(...)) 在**层级缓存变化时**就列了 bravo(根层到货即可触发), 阶段 D 的展开直接命中缓存 → 零请求,但子行确实出现。旧断言 sibling.requests[0] === 'fs.trees[bravo]' 编码的是「展开必然发请求」这条已不成立的前提。 修法:断言改为预取感知——允许 0 或恰好该层级两种取值,且必须同时证明不存在更大范围的 fs.trees 请求(requests.length ≤ 1 且每一条 === 'fs.trees[bravo]'),并要求子行出现 (added ≥ 1);注释写明「零请求是预取命中,不是没展开」以及「预取发生在层级缓存变化时」, 免得后人又把它当 bug 改回去。展开 big(40 子项、非 compact)仍必须恰好发 fs.trees[big] (防「重列整个可见集」的主守卫保留),tree body 不被替换、socket 不新增、collapse 无 killed rows、拓扑/展开语义等既有守卫一律保留。 门禁(本地,最终状态): pnpm typecheck → exit 0 pnpm exec vitest run --maxWorkers=3 tests/search-engines.spec.ts tests/fs-search.spec.ts \ tests/fs-tree-compact.spec.ts tests/exclude-patterns.spec.ts → 4 files / 71 tests passed, exit 0 pnpm exec vitest run --maxWorkers=3 → 172 files / 1943 passed | 13 skipped, exit 0 pnpm lint → exit 0 pnpm check:engines → exit 0(本机无 fd:SKIPPED,rg 实跑通过) 未验证:真机 fd(本机未安装,win32 fixture 分支用真实 rg 在同一台机器上端到端验过)与 plugin-mount lane(挂载 lane 需下载真实 DSH + Playwright,未在本地运行,交由 CI 验证)。 Refs #303 Refs #264 --- src/search-engines.ts | 34 ++++++++---- tests/e2e/zz-expand-refresh.e2e.ts | 27 ++++++++- tests/search-engines.spec.ts | 88 +++++++++++++++++++++++------- 3 files changed, 118 insertions(+), 31 deletions(-) diff --git a/src/search-engines.ts b/src/search-engines.ts index 1ad6c25c..ac782889 100644 --- a/src/search-engines.ts +++ b/src/search-engines.ts @@ -22,7 +22,7 @@ */ import { spawn, type ChildProcess } from 'node:child_process' import { createInterface } from 'node:readline' -import { dirname, join, sep } from 'node:path' +import { join, posix, sep, win32 } from 'node:path' import { homedir } from 'node:os' import { debugLog } from './search-debug.ts' @@ -70,6 +70,10 @@ const PATH_SEPARATOR = process.platform === 'win32' ? ';' : ':' * (verified: the candidate resolves to the bundled rg on macOS). * Wrong guesses are cheap: every candidate goes through verify() and * unusable ones are dropped. + * + * Pure in `platform`: every path below is built with that platform's + * `node:path` dialect (see `P` in the body), so the returned shape depends + * only on the arguments — never on the OS running the probe. */ export function bundledRgCandidates( platform: NodeJS.Platform, @@ -81,6 +85,16 @@ export function bundledRgCandidates( ): string[] { const binName = platform === 'win32' ? 'rg.exe' : 'rg' const platformPkg = `@vscode/ripgrep-${platform}-${arch}` + // Everything below is derived from the PLATFORM ARGUMENT, so it must use + // that platform's path dialect — never the host's. A host `sep`/`dirname` + // makes this function a different function on every OS: on a Windows host + // the `endsWith(sep + 'node_modules')` test below never matched a POSIX + // start path, so the whole argv[1] walk produced nothing and the win32 / + // darwin shapes could not be pinned from a non-Windows machine (the Windows + // CI failure this fixes). With the dialect selected here the shape derived + // from those arguments is the same everywhere: 'darwin' always yields + // '/'-joined candidates, 'win32' always yields '\'-joined ones. + const P = platform === 'win32' ? win32 : posix const roots: string[] = [] const addRoot = (root: string): void => { if (root !== '' && !roots.includes(root)) roots.push(root) @@ -92,10 +106,10 @@ export function bundledRgCandidates( // executable itself lives elsewhere (Homebrew node + a bundled tree). const walkRoots = (start: string): void => { if (start === '') return - let current = dirname(start) - while (current !== dirname(current)) { - if (current.endsWith(`${sep}node_modules`)) addRoot(current) - current = dirname(current) + let current = P.dirname(start) + while (current !== P.dirname(current)) { + if (current.endsWith(`${P.sep}node_modules`)) addRoot(current) + current = P.dirname(current) } } if (modulePath !== undefined && modulePath !== '') walkRoots(modulePath) @@ -105,24 +119,24 @@ export function bundledRgCandidates( // %APPDATA%\npm\node_modules (nvm-windows keeps the same %APPDATA%\npm). if (platform === 'win32') { if (env.APPDATA !== undefined && env.APPDATA !== '') { - addRoot(join(env.APPDATA, 'npm', 'node_modules')) + addRoot(P.join(env.APPDATA, 'npm', 'node_modules')) } } else { - addRoot(join(dirname(dirname(execPath)), 'lib', 'node_modules')) + addRoot(P.join(P.dirname(P.dirname(execPath)), 'lib', 'node_modules')) // Homebrew and pnpm global layouts missed by the execPath derivation. addRoot('/opt/homebrew/lib/node_modules') addRoot('/usr/local/lib/node_modules') } // The DSH profile layout: plugins and the host CLI share one tree. - addRoot(join(home, '.dsh', 'profiles', 'node_modules')) + addRoot(P.join(home, '.dsh', 'profiles', 'node_modules')) const candidates: string[] = [] const seen = new Set() for (const root of roots) { for (const candidate of [ - join(root, platformPkg, 'bin', binName), + P.join(root, platformPkg, 'bin', binName), // A dependency tree that did NOT hoist (pnpm's strict layout, npm with // a conflicting top-level @vscode/ripgrep). - join(root, '@deepseek-ai', 'dsh', 'node_modules', platformPkg, 'bin', binName), + P.join(root, '@deepseek-ai', 'dsh', 'node_modules', platformPkg, 'bin', binName), ]) { if (!seen.has(candidate)) { seen.add(candidate) diff --git a/tests/e2e/zz-expand-refresh.e2e.ts b/tests/e2e/zz-expand-refresh.e2e.ts index bfb8738f..e3982a1b 100644 --- a/tests/e2e/zz-expand-refresh.e2e.ts +++ b/tests/e2e/zz-expand-refresh.e2e.ts @@ -358,7 +358,32 @@ test('a folder toggle re-lists that folder and nothing else', async ({ page }) = expand.requests[0], 'expanding a folder must ask for that folder, not the whole visible set', ).toBe(`fs.trees[${BIG}]`) - expect(sibling.requests[0], 'expanding a sibling must ask for the sibling').toBe('fs.trees[bravo]') + // D is prefetch-AWARE, and zero requests here is a CACHE HIT, not a missing + // expansion. The host's `probeCompactRows` marks a directory whose sole + // child is a FILE as `compact` too — deliberately, so the client can preload + // marked levels and stabilize the fold label before expansion — and `bravo` + // holds exactly one file. The compact-preload effect in FileTree.tsx + // (`loadLevels(compactLoadTargets(...))`) therefore lists `bravo` whenever + // the LEVEL CACHE CHANGES (the root listing arriving is enough), while it is + // still collapsed. The click then renders straight from that cache: no + // request, but the child row does appear. Both readings are accepted, and + // the guard this phase really owes is the second one — bounding the SCOPE: + // whatever it asks for must be exactly the toggled level, never a payload + // carrying the root or the whole visible set (that is the empty-cache + // signature of a fresh mount, which is what used to re-list everything on + // every toggle). Do NOT "fix" the zero back into an expected request: an + // expansion is not supposed to re-list anything it already has. + expect( + sibling.requests.length, + 'expanding a prefetched compact level lists at most that one level', + ).toBeLessThanOrEqual(1) + for (const request of sibling.requests) { + expect(request, 'expanding a sibling must ask for the sibling, never a wider set').toBe('fs.trees[bravo]') + } + expect( + sibling.added, + 'the prefetched level must still render its child row (zero requests while rows appear)', + ).toBeGreaterThanOrEqual(1) // A2: the tree body survives every toggle. A replaced body unmounts the // explorer — level cache, scroll position and directory watcher with it — // and a reopened socket re-announces its whole watch set. diff --git a/tests/search-engines.spec.ts b/tests/search-engines.spec.ts index c89e3ffe..5b1d8800 100644 --- a/tests/search-engines.spec.ts +++ b/tests/search-engines.spec.ts @@ -12,7 +12,7 @@ import { afterEach, describe, expect, it } from 'vitest' import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' -import { join } from 'node:path' +import { join, posix, win32 } from 'node:path' import { searchFiles } from '../src/fs-search.ts' import { bundledRgCandidates, @@ -268,6 +268,14 @@ describe('engine argv symmetry', () => { }) describe('bundledRgCandidates', () => { + // Every expectation below joins with the DIALECT OF THE PLATFORM UNDER TEST + // (`posix.join` for darwin, `win32.join` for win32), never the host `join`: + // the function is a pure function of its `platform` argument, so the host + // separator would make these assertions test the runner instead of the + // derivation (on a POSIX runner the host join silently accepts a candidate + // shape Windows would never see, and on Windows it demands backslashes from + // a POSIX case). + // npm/pnpm hoist @vscode/ripgrep-- NEXT TO @deepseek-ai, // never under @deepseek-ai/dsh/node_modules — the derivation must point at // the package-manager root that really holds the binary. @@ -277,7 +285,7 @@ describe('bundledRgCandidates', () => { '/opt/homebrew/bin/node', {}, '/Users/me', undefined, ) - expect(paths).toContain(join( + expect(paths).toContain(posix.join( '/opt/homebrew/lib/node_modules', '@vscode/ripgrep-darwin-arm64/bin/rg', )) @@ -287,7 +295,7 @@ describe('bundledRgCandidates', () => { // package under the CLI's own node_modules — still a candidate. it('also covers the non-hoisted /@deepseek-ai/dsh/node_modules layout', () => { const paths = bundledRgCandidates('darwin', 'arm64', '/usr/local/bin/node', {}, '/Users/me', undefined) - expect(paths).toContain(join( + expect(paths).toContain(posix.join( '/usr/local/lib/node_modules', '@deepseek-ai/dsh/node_modules', '@vscode/ripgrep-darwin-arm64/bin/rg', @@ -305,10 +313,25 @@ describe('bundledRgCandidates', () => { {}, '/Users/me', '/Applications/Dsh.app/Contents/Resources/dsh/node_modules/@deepseek-ai/dsh/lib/bin.js', ) - expect(paths).toContain(join( + expect(paths).toContain(posix.join( '/Applications/Dsh.app/Contents/Resources/dsh/node_modules', '@vscode/ripgrep-darwin-arm64/bin/rg', )) + // The same walk under the win32 dialect: the candidate must come back in + // the REAL backslash shape on any host. This is the discriminating + // assertion for the platform-purity fix — with the host's `sep`/`dirname` + // a POSIX runner derived nothing from a '\'-separated start path, and a + // Windows runner derived nothing from the POSIX one above. + const win = bundledRgCandidates( + 'win32', 'x64', + 'C:\\Program Files\\nodejs\\node.exe', + {}, 'C:\\Users\\me', + 'C:\\Tools\\Dsh\\app\\resources\\dsh\\node_modules\\@deepseek-ai\\dsh\\lib\\bin.js', + ) + expect(win).toContain(win32.join( + 'C:\\Tools\\Dsh\\app\\resources\\dsh\\node_modules', + '@vscode/ripgrep-win32-x64/bin/rg.exe', + )) }) // Windows npm has NO lib/ layer: the global prefix is %APPDATA%\npm, so @@ -320,9 +343,10 @@ describe('bundledRgCandidates', () => { 'C:\\Program Files\\nodejs\\node.exe', { APPDATA: 'C:\\Users\\me\\AppData\\Roaming' }, 'C:\\Users\\me', undefined, ) - // join() renders the platform separator, so this also validates the - // real backslash shape on Windows CI. - expect(paths).toContain(join( + // win32.join renders the win32 separator on EVERY host, so this pins the + // real backslash shape here on a POSIX runner too — the platform + // argument decides the dialect, not the machine running the tests. + expect(paths).toContain(win32.join( 'C:\\Users\\me\\AppData\\Roaming', 'npm', 'node_modules', '@vscode', 'ripgrep-win32-x64', 'bin', 'rg.exe', )) @@ -330,15 +354,17 @@ describe('bundledRgCandidates', () => { it('covers the DSH profile layout under ~/.dsh on every platform', () => { const darwin = bundledRgCandidates('darwin', 'arm64', '/usr/local/bin/node', {}, '/Users/me', undefined) - expect(darwin).toContain(join( + expect(darwin).toContain(posix.join( '/Users/me/.dsh/profiles/node_modules', '@vscode', 'ripgrep-darwin-arm64', 'bin', 'rg', )) - // Windows: node:path is platform-bound, so a test on POSIX cannot pin - // the exact backslash shape — assert the layout structure (a real - // Windows CI/true-positive run pins the literal '\\' separators). + // Windows: the win32 dialect is selected from the platform argument, so + // the exact '\'-separated shape IS pinnable from a POSIX host now. const win = bundledRgCandidates('win32', 'x64', 'C:\\node.exe', {}, 'C:\\Users\\me', undefined) - expect(win.some(path => path.includes('.dsh') && path.includes('@vscode'))).toBe(true) + expect(win).toContain(win32.join( + 'C:\\Users\\me', '.dsh', 'profiles', 'node_modules', + '@vscode', 'ripgrep-win32-x64', 'bin', 'rg.exe', + )) }) it('dedupes identical candidates across derivations', () => { @@ -356,8 +382,11 @@ describe('bundledRgCandidates', () => { it('drops APPDATA roots when the env var is absent (win32)', () => { const paths = bundledRgCandidates('win32', 'x64', 'C:\\node.exe', {}, 'C:\\Users\\me', undefined) expect(paths.some(path => path.includes('AppData'))).toBe(false) - // The fixed profile root survives. - expect(paths.some(path => path.includes('.dsh'))).toBe(true) + // The fixed profile root survives — in its real win32 shape. + expect(paths).toContain(win32.join( + 'C:\\Users\\me', '.dsh', 'profiles', 'node_modules', + '@vscode', 'ripgrep-win32-x64', 'bin', 'rg.exe', + )) }) }) @@ -427,10 +456,29 @@ describe('real engines (only when installed)', () => { writeFileSync(join(dir, 'src', 'util', 'util-helper.ts'), 'x') writeFileSync(join(dir, 'web', 'comp-util', 'component.tsx'), 'x') writeFileSync(join(dir, 'node_modules', 'util-dep', 'index.js'), 'x') - // A '*' is a literal here (--fixed-strings for fd, escaped glob for - // rg): it must match the file that really contains it and nothing else. - writeFileSync(join(dir, 'a*b.ts'), 'x') - writeFileSync(join(dir, 'anb.ts'), 'x') + // A glob metacharacter in the query is a LITERAL here (--fixed-strings + // for fd, escapeGlob for rg): it must match the file whose NAME really + // contains it and must not act as a wildcard. The pair is picked so a + // lost escape is CAUGHT rather than merely unexercised — the decoy is + // exactly what the non-literal reading would match: + // POSIX : query 'a*b', literal 'a*b.ts', decoy 'anb.ts' + // (a glob/regex 'a*b' matches the decoy, verified on rg 15) + // win32 : query 'a[b]', literal 'a[b].ts', decoy 'ab.ts' + // '*' (and '?', ':', '"', '<', '>', '|') are ILLEGAL file-name + // characters on Windows: the old fixture wrote a real 'a*b.ts' there + // and died with ENOENT before a single assertion ran. '[' and ']' are + // legal everywhere and carry the same property — 'a[b]' is a character + // class, so an unescaped glob matches 'ab.ts' and NOT the literal file + // (verified against real rg 15.2.0: unescaped '*a[b]*' returns + // 'ab.ts', escaped '*a\[b\]*' returns 'a[b].ts'). Deliberately NOT an + // UNCLOSED 'a[b': rg refuses the glob outright ("unclosed character + // class", exit 2), the engine gets dropped as broken and the plain-walk + // fallback satisfies the assertion by accident. + const meta = process.platform === 'win32' + ? { file: 'a[b].ts', decoy: 'ab.ts', query: 'a[b]' } + : { file: 'a*b.ts', decoy: 'anb.ts', query: 'a*b' } + writeFileSync(join(dir, meta.file), 'x') + writeFileSync(join(dir, meta.decoy), 'x') // Directory hits are derived from the file paths (rg) or read from // fd's directory listing: one nested match, one direct child, and the @@ -440,8 +488,8 @@ describe('real engines (only when installed)', () => { expect(dirHit.dirs).toEqual(['src/util', 'web/comp-util']) expect(dirHit.truncated).toBe(false) - const star = await searchFiles(dir, 'a*b') - expect(star.matches).toEqual(['a*b.ts']) + const literal = await searchFiles(dir, meta.query) + expect(literal.matches).toEqual([meta.file]) } finally { rmSync(dir, { recursive: true, force: true }) }