Skip to content

bug: readJson() lacks error handling for malformed JSON #18351

Closed as not planned

Description

@nathan-nazareth

Bug Description

In packages/opencode/src/util/filesystem.ts:37-38, the readJson function doesn't have error handling:

export async function readJson<T = any>(p: string): Promise<T> {
  return JSON.parse(await readFile(p, "utf-8"))
}

If the file contains malformed JSON, this will throw an unhandled error that may crash the caller.

Impact

  • Malformed JSON files cause unhandled exceptions
  • No opportunity to provide a default value or recovery mechanism
  • Makes debugging harder in production

Files Affected

  • packages/opencode/src/util/filesystem.ts lines 37-38

Suggested Fix

Wrap JSON.parse in try/catch and provide a meaningful error:

export async function readJson<T = any>(p: string): Promise<T> {
  try {
    return JSON.parse(await readFile(p, "utf-8"))
  } catch (e) {
    throw new Error(`Failed to parse JSON from ${p}`, { cause: e })
  }
}

Acceptance Criteria

  • Malformed JSON throws a meaningful error with file path
  • Error chain preserves original exception

Activity

  1. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on Mar 20, 2026
  2. removed
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 3, 2026
  3. github-actions commented on Jul 4, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions