From 23848ed326d6fa2b0f3de04de7a09b462b386491 Mon Sep 17 00:00:00 2001 From: Cole Leavitt Date: Wed, 14 Jan 2026 16:44:54 -0700 Subject: [PATCH] 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 --- packages/opencode/src/storage/storage.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/opencode/src/storage/storage.ts b/packages/opencode/src/storage/storage.ts index 8b4042ea13..611b692897 100644 --- a/packages/opencode/src/storage/storage.ts +++ b/packages/opencode/src/storage/storage.ts @@ -170,7 +170,11 @@ export namespace Storage { const target = path.join(dir, ...key) + ".json" return withErrorHandling(async () => { using _ = await Lock.read(target) - const result = await Bun.file(target).json() + const content = await Bun.file(target).text() + if (!content.trim()) { + throw new NotFoundError({ message: `Empty file: ${target}` }) + } + const result = JSON.parse(content) return result as T }) } @@ -180,10 +184,14 @@ export namespace Storage { const target = path.join(dir, ...key) + ".json" return withErrorHandling(async () => { using _ = await Lock.write(target) - const content = await Bun.file(target).json() - fn(content) - await Bun.write(target, JSON.stringify(content, null, 2)) - return content as T + const content = await Bun.file(target).text() + if (!content.trim()) { + throw new NotFoundError({ message: `Empty file: ${target}` }) + } + const parsed = JSON.parse(content) + fn(parsed) + await Bun.write(target, JSON.stringify(parsed, null, 2)) + return parsed as T }) }