Skip to content

Commit 568eccb

Browse files
committed
Revert: all refactor commits migrating from Bun.file() to Filesystem module
1 parent 3a07dd8 commit 568eccb

25 files changed

Lines changed: 216 additions & 294 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
name: bun-file-io
3+
description: Use this when you are working on file operations like reading, writing, scanning, or deleting files. It summarizes the preferred file APIs and patterns used in this repo. It also notes when to use filesystem helpers for directories.
4+
---
5+
6+
## Use this when
7+
8+
- Editing file I/O or scans in `packages/opencode`
9+
- Handling directory operations or external tools
10+
11+
## Bun file APIs (from Bun docs)
12+
13+
- `Bun.file(path)` is lazy; call `text`, `json`, `stream`, `arrayBuffer`, `bytes`, `exists` to read.
14+
- Metadata: `file.size`, `file.type`, `file.name`.
15+
- `Bun.write(dest, input)` writes strings, buffers, Blobs, Responses, or files.
16+
- `Bun.file(...).delete()` deletes a file.
17+
- `file.writer()` returns a FileSink for incremental writes.
18+
- `Bun.Glob` + `Array.fromAsync(glob.scan({ cwd, absolute, onlyFiles, dot }))` for scans.
19+
- Use `Bun.which` to find a binary, then `Bun.spawn` to run it.
20+
- `Bun.readableStreamToText/Bytes/JSON` for stream output.
21+
22+
## When to use node:fs
23+
24+
- Use `node:fs/promises` for directories (`mkdir`, `readdir`, recursive operations).
25+
26+
## Repo patterns
27+
28+
- Prefer Bun APIs over Node `fs` for file access.
29+
- Check `Bun.file(...).exists()` before reading.
30+
- For binary/large files use `arrayBuffer()` and MIME checks via `file.type`.
31+
- Use `Bun.Glob` + `Array.fromAsync` for scans.
32+
- Decode tool stderr with `Bun.readableStreamToText`.
33+
- For large writes, use `Bun.write(Bun.file(path), text)`.
34+
35+
NOTE: Bun.file(...).exists() will return `false` if the value is a directory.
36+
Use Filesystem.exists(...) instead if path can be file or directory
37+
38+
## Quick checklist
39+
40+
- Use Bun APIs first.
41+
- Use `path.join`/`path.resolve` for paths.
42+
- Prefer promise `.catch(...)` over `try/catch` when possible.

packages/opencode/src/cli/cmd/tui/thread.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import { tui } from "./app"
33
import { Rpc } from "@/util/rpc"
44
import { type rpc } from "./worker"
55
import path from "path"
6-
import { fileURLToPath } from "url"
76
import { UI } from "@/cli/ui"
87
import { iife } from "@/util/iife"
98
import { Log } from "@/util/log"
109
import { withNetworkOptions, resolveNetworkOptions } from "@/cli/network"
11-
import { Filesystem } from "@/util/filesystem"
1210
import type { Event } from "@opencode-ai/sdk/v2"
1311
import type { EventSource } from "./context/sdk"
1412
import { win32DisableProcessedInput, win32InstallCtrlCGuard } from "./win32"
@@ -101,7 +99,7 @@ export const TuiThreadCommand = cmd({
10199
const distWorker = new URL("./cli/cmd/tui/worker.js", import.meta.url)
102100
const workerPath = await iife(async () => {
103101
if (typeof OPENCODE_WORKER_PATH !== "undefined") return OPENCODE_WORKER_PATH
104-
if (await Filesystem.exists(fileURLToPath(distWorker))) return distWorker
102+
if (await Bun.file(distWorker).exists()) return distWorker
105103
return localWorker
106104
})
107105
try {

packages/opencode/src/lsp/client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ export namespace LSPClient {
147147
notify: {
148148
async open(input: { path: string }) {
149149
input.path = path.isAbsolute(input.path) ? input.path : path.resolve(Instance.directory, input.path)
150-
const text = await Filesystem.readText(input.path)
150+
const file = Bun.file(input.path)
151+
const text = await file.text()
151152
const extension = path.extname(input.path)
152153
const languageId = LANGUAGE_EXTENSIONS[extension] ?? "plaintext"
153154

0 commit comments

Comments
 (0)