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 }) }