Skip to content

Commit 3b97580

Browse files
authored
tweak: ensure read tool uses fs/promises for all paths (anomalyco#14027)
1 parent cb88fe2 commit 3b97580

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

packages/opencode/src/tool/read.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import z from "zod"
2-
import * as fs from "fs"
2+
import { createReadStream } from "fs"
3+
import * as fs from "fs/promises"
34
import * as path from "path"
45
import { createInterface } from "readline"
56
import { Tool } from "./tool"
@@ -52,14 +53,18 @@ export const ReadTool = Tool.define("read", {
5253
const dir = path.dirname(filepath)
5354
const base = path.basename(filepath)
5455

55-
const dirEntries = fs.readdirSync(dir)
56-
const suggestions = dirEntries
57-
.filter(
58-
(entry) =>
59-
entry.toLowerCase().includes(base.toLowerCase()) || base.toLowerCase().includes(entry.toLowerCase()),
56+
const suggestions = await fs
57+
.readdir(dir)
58+
.then((entries) =>
59+
entries
60+
.filter(
61+
(entry) =>
62+
entry.toLowerCase().includes(base.toLowerCase()) || base.toLowerCase().includes(entry.toLowerCase()),
63+
)
64+
.map((entry) => path.join(dir, entry))
65+
.slice(0, 3),
6066
)
61-
.map((entry) => path.join(dir, entry))
62-
.slice(0, 3)
67+
.catch(() => [])
6368

6469
if (suggestions.length > 0) {
6570
throw new Error(`File not found: ${filepath}\n\nDid you mean one of these?\n${suggestions.join("\n")}`)
@@ -69,12 +74,12 @@ export const ReadTool = Tool.define("read", {
6974
}
7075

7176
if (stat.isDirectory()) {
72-
const dirents = await fs.promises.readdir(filepath, { withFileTypes: true })
77+
const dirents = await fs.readdir(filepath, { withFileTypes: true })
7378
const entries = await Promise.all(
7479
dirents.map(async (dirent) => {
7580
if (dirent.isDirectory()) return dirent.name + "/"
7681
if (dirent.isSymbolicLink()) {
77-
const target = await fs.promises.stat(path.join(filepath, dirent.name)).catch(() => undefined)
82+
const target = await fs.stat(path.join(filepath, dirent.name)).catch(() => undefined)
7883
if (target?.isDirectory()) return dirent.name + "/"
7984
}
8085
return dirent.name
@@ -140,7 +145,7 @@ export const ReadTool = Tool.define("read", {
140145
const isBinary = await isBinaryFile(filepath, stat.size)
141146
if (isBinary) throw new Error(`Cannot read binary file: ${filepath}`)
142147

143-
const stream = fs.createReadStream(filepath, { encoding: "utf8" })
148+
const stream = createReadStream(filepath, { encoding: "utf8" })
144149
const rl = createInterface({
145150
input: stream,
146151
// Note: we use the crlfDelay option to recognize all instances of CR LF
@@ -267,7 +272,7 @@ async function isBinaryFile(filepath: string, fileSize: number): Promise<boolean
267272

268273
if (fileSize === 0) return false
269274

270-
const fh = await fs.promises.open(filepath, "r")
275+
const fh = await fs.open(filepath, "r")
271276
try {
272277
const sampleSize = Math.min(4096, fileSize)
273278
const bytes = Buffer.alloc(sampleSize)

0 commit comments

Comments
 (0)