diff --git a/sdk/src/__tests__/glob-cwd.test.ts b/sdk/src/__tests__/glob-cwd.test.ts new file mode 100644 index 0000000000..de2b70fdd8 --- /dev/null +++ b/sdk/src/__tests__/glob-cwd.test.ts @@ -0,0 +1,47 @@ +import * as fs from 'node:fs/promises' +import { tmpdir } from 'node:os' +import * as path from 'node:path' + +import { expect, it } from 'bun:test' + +import { glob } from '../tools/glob' + +// A false empty glob result hides existing project files from the agent. +// Exercise the real project scan: equivalent relative cwd paths must find them. +it('finds the same files for equivalent relative working directories', async () => { + const tempRoot = path.resolve(tmpdir()) + const projectPath = await fs.mkdtemp( + path.join(tempRoot, 'freebuff-glob-cwd-'), + ) + try { + const files = ['root.ts', 'src/a.ts', 'src/nested/b.ts', 'src-other/c.ts'] + for (const file of files) { + const fullPath = path.join(projectPath, file) + await fs.mkdir(path.dirname(fullPath), { recursive: true }) + await fs.writeFile(fullPath, '') + } + const inSrc = ['src/a.ts', 'src/nested/b.ts'] + const cases: [string, string[]][] = [ + ['.', files], + ['./', files], + ['./src', inSrc], + ['src/../src/', inSrc], + [path.join('src', 'nested', '..'), inSrc], + ] + for (const [cwd, expected] of cases) { + const [output] = await glob({ + pattern: '**/*.ts', + projectPath, + cwd, + fs, + }) + const value = output.value as { files: string[]; count: number } + expect(value.count).toBe(expected.length) + expect(value.files.sort()).toEqual([...expected].sort()) + } + } finally { + if (path.dirname(projectPath) !== tempRoot) + throw new Error('Invalid fixture path') + await fs.rm(projectPath, { recursive: true, force: true }) + } +}) diff --git a/sdk/src/tools/glob.ts b/sdk/src/tools/glob.ts index d176eb8005..f40ee0e833 100644 --- a/sdk/src/tools/glob.ts +++ b/sdk/src/tools/glob.ts @@ -1,3 +1,5 @@ +import * as path from 'node:path' + import micromatch from 'micromatch' import { listProjectFiles } from './list-project-files' @@ -38,13 +40,14 @@ export async function glob(params: { } = params try { + // Listing paths are project-relative and use '/' on every platform. + const normalizedCwd = cwd + ? path.normalize(cwd).split(path.sep).join('/').replace(/\/$/, '') + : undefined const inCwd = (filePath: string) => { - if (!cwd) return true - const cwdPrefix = cwd.endsWith('/') ? cwd : `${cwd}/` + if (!normalizedCwd || normalizedCwd === '.') return true return ( - filePath === cwd || - filePath.startsWith(cwdPrefix) || - filePath === cwd.replace(/\/$/, '') + filePath === normalizedCwd || filePath.startsWith(`${normalizedCwd}/`) ) } const listing = await listProjectFiles({