Skip to content

Commit 4e9de20

Browse files
committed
sdk: Don't use fs.exists, create helper
1 parent 5ce0a34 commit 4e9de20

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

common/src/project-file-tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as ignore from 'ignore'
44
import { sortBy } from 'lodash'
55

66
import { DEFAULT_IGNORED_PATHS } from './old-constants'
7-
import { isValidProjectRoot } from './util/file'
7+
import { fileExists, isValidProjectRoot } from './util/file'
88

99
import type { CodebuffFileSystem } from './types/filesystem'
1010
import type { DirectoryNode, FileTreeNode } from './util/file'
@@ -167,7 +167,7 @@ export async function parseGitignore(params: {
167167
]
168168

169169
for (const ignoreFilePath of ignoreFiles) {
170-
if (!(await fs.exists(ignoreFilePath))) continue
170+
if (!(await fileExists({ filePath: ignoreFilePath, fs }))) continue
171171

172172
const ignoreContent = await fs.readFile(ignoreFilePath, 'utf8')
173173
const lines = ignoreContent.split('\n')

common/src/types/filesystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import type fs from 'fs'
66
*/
77
export type CodebuffFileSystem = Pick<
88
typeof fs.promises,
9-
'exists' | 'mkdir' | 'readdir' | 'readFile' | 'stat' | 'writeFile'
9+
'mkdir' | 'readdir' | 'readFile' | 'stat' | 'writeFile'
1010
>

common/src/util/file.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,31 @@ export const ensureEndsWithNewline = (
248248
return contents + '\n'
249249
}
250250

251+
/**
252+
* Node-compatible file existence check.
253+
* Uses fs.stat instead of Bun-specific fs.exists.
254+
*/
255+
export async function fileExists(params: {
256+
filePath: string
257+
fs: CodebuffFileSystem
258+
}): Promise<boolean> {
259+
const { filePath, fs } = params
260+
261+
try {
262+
await fs.stat(filePath)
263+
return true
264+
} catch {
265+
return false
266+
}
267+
}
268+
251269
export const ensureDirectoryExists = async (params: {
252270
baseDir: string
253271
fs: CodebuffFileSystem
254272
}) => {
255273
const { baseDir, fs } = params
256274

257-
if (!(await fs.exists(baseDir))) {
275+
if (!(await fileExists({ filePath: baseDir, fs }))) {
258276
await fs.mkdir(baseDir, { recursive: true })
259277
}
260278
}

evals/scaffolding.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,7 @@ function readMockFile(projectRoot: string, filePath: string): string | null {
4444

4545
let toolCalls: ClientToolCall[] = []
4646
let toolResults: ToolMessage[] = []
47-
const defaultFs: CodebuffFileSystem = {
48-
...(fs.promises as unknown as CodebuffFileSystem),
49-
exists: async (pathLike) => {
50-
try {
51-
await fs.promises.access(pathLike as fs.PathLike)
52-
return true
53-
} catch {
54-
return false
55-
}
56-
},
57-
}
47+
const defaultFs: CodebuffFileSystem = fs.promises as unknown as CodebuffFileSystem
5848
let projectRootForMocks: string | undefined
5949

6050
export function createFileReadingMock(projectRoot: string) {

sdk/src/tools/change-file.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import path from 'path'
33
import { applyPatch } from 'diff'
44
import z from 'zod/v4'
55

6+
import { fileExists } from '@codebuff/common/util/file'
7+
68
import type { CodebuffToolOutput } from '@codebuff/common/tools/list'
79
import type { CodebuffFileSystem } from '@codebuff/common/types/filesystem'
810

@@ -96,8 +98,8 @@ async function applyChanges(params: {
9698
const { path: filePath, content, type } = change
9799
try {
98100
const fullPath = path.join(projectRoot, filePath)
99-
const fileExists = await fs.exists(fullPath)
100-
if (!fileExists) {
101+
const exists = await fileExists({ filePath: fullPath, fs })
102+
if (!exists) {
101103
const dirPath = path.dirname(fullPath)
102104
await fs.mkdir(dirPath, { recursive: true })
103105
}
@@ -114,7 +116,7 @@ async function applyChanges(params: {
114116
await fs.writeFile(fullPath, newContent)
115117
}
116118

117-
if (fileExists) {
119+
if (exists) {
118120
modified.push(filePath)
119121
} else {
120122
created.push(filePath)

0 commit comments

Comments
 (0)