Skip to content

Commit 23848ed

Browse files
committed
fix(storage): handle empty JSON files gracefully
- Check for empty files before parsing to avoid 'Unexpected end of JSON input' error - Throw NotFoundError instead of JSON parse error for empty files - Apply fix to both read() and update() functions
1 parent e59a836 commit 23848ed

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

packages/opencode/src/storage/storage.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ export namespace Storage {
170170
const target = path.join(dir, ...key) + ".json"
171171
return withErrorHandling(async () => {
172172
using _ = await Lock.read(target)
173-
const result = await Bun.file(target).json()
173+
const content = await Bun.file(target).text()
174+
if (!content.trim()) {
175+
throw new NotFoundError({ message: `Empty file: ${target}` })
176+
}
177+
const result = JSON.parse(content)
174178
return result as T
175179
})
176180
}
@@ -180,10 +184,14 @@ export namespace Storage {
180184
const target = path.join(dir, ...key) + ".json"
181185
return withErrorHandling(async () => {
182186
using _ = await Lock.write(target)
183-
const content = await Bun.file(target).json()
184-
fn(content)
185-
await Bun.write(target, JSON.stringify(content, null, 2))
186-
return content as T
187+
const content = await Bun.file(target).text()
188+
if (!content.trim()) {
189+
throw new NotFoundError({ message: `Empty file: ${target}` })
190+
}
191+
const parsed = JSON.parse(content)
192+
fn(parsed)
193+
await Bun.write(target, JSON.stringify(parsed, null, 2))
194+
return parsed as T
187195
})
188196
}
189197

0 commit comments

Comments
 (0)