From 41cebc5b391500af863e128226f4fe4b368bb59c Mon Sep 17 00:00:00 2001 From: VIOLETee0 <289644494+VIOLETee0@users.noreply.github.com> Date: Thu, 26 Nov 2026 01:18:19 +0800 Subject: [PATCH] Normalize relative working directories in glob searches --- sdk/src/__tests__/glob-cwd.test.ts | 47 ++++++++++++++++++++++++++++++ sdk/src/tools/glob.ts | 22 +++++++++----- 2 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 sdk/src/__tests__/glob-cwd.test.ts 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 9a5c8050b6..58c2bbbdd3 100644 --- a/sdk/src/tools/glob.ts +++ b/sdk/src/tools/glob.ts @@ -1,3 +1,5 @@ +import * as path from 'node:path' + import { DEFAULT_MAX_FILES, flattenTree, @@ -25,13 +27,19 @@ export async function glob(params: { .map((node) => node.filePath) if (cwd) { - const cwdPrefix = cwd.endsWith('/') ? cwd : `${cwd}/` - allFilePaths = allFilePaths.filter( - (filePath) => - filePath === cwd || - filePath.startsWith(cwdPrefix) || - filePath === cwd.replace(/\/$/, ''), - ) + // Tree paths are project-relative and use '/' on every platform. + const normalizedCwd = path + .normalize(cwd) + .split(path.sep) + .join('/') + .replace(/\/$/, '') + if (normalizedCwd !== '.') { + const cwdPrefix = `${normalizedCwd}/` + allFilePaths = allFilePaths.filter( + (filePath) => + filePath === normalizedCwd || filePath.startsWith(cwdPrefix), + ) + } } const allMatchingFiles = micromatch(allFilePaths, pattern)