Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions sdk/src/__tests__/glob-cwd.test.ts
Original file line number Diff line number Diff line change
@@ -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 })
}
})
22 changes: 15 additions & 7 deletions sdk/src/tools/glob.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as path from 'node:path'

import {
DEFAULT_MAX_FILES,
flattenTree,
Expand Down Expand Up @@ -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)
Expand Down
Loading