Skip to content

import fails on UTF-16 files with BOM (Windows PowerShell redirection) #54306

Closed as not planned

Description

@reynold-hu

Description

opencode import <file> fails when the JSON file is UTF-16 with a BOM:

Error: Invalid JSON in C:\...\session.json: JSON Parse error: Unrecognized token '�'

On Windows this happens with the default shell redirection: PowerShell 5.1 writes > output as UTF-16LE with a BOM, so this common round-trip fails:

opencode export <session-id> > session.json
opencode import session.json

readJson in packages/core/src/fs-util.ts decodes every file as UTF-8 (fs.readFileString), so the UTF-16 bytes turn into replacement characters before JSON.parse runs. Files with a BOM have an unambiguous encoding and could be decoded accordingly; UTF-16 without a BOM can stay unsupported.

This was previously diagnosed in #21941, which the inactivity bot auto-closed without a fix. Still reproducible on dev. I have a fix for BOM-prefixed UTF-16LE/BE in readJson and will open a PR referencing this issue.

OpenCode version

dev branch, HEAD 3884062, run with bun dev

Steps to reproduce

  1. In Windows PowerShell 5.1, run opencode export <session-id> > session.json (the file is written as UTF-16LE with BOM)
  2. Run opencode import session.json
  3. Import fails with Invalid JSON ... Unrecognized token

Any UTF-16LE/BE JSON file with a BOM fails the same way; converting the file to UTF-8 without a BOM makes it import successfully.

Operating System

Windows 10 23H2 (build 22631)

Terminal

Windows PowerShell 5.1

Activity

  1. opencode-agent commented on Oct 10, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed report and for tracking down the cause!

    This was reproduced on the dev branch, which is OpenCode v1 (0.x/1.x). v1 now only gets fixes for critical bugs such as data loss, security problems or breakage with no workaround. This one has a straightforward workaround, so it's unlikely to be fixed in v1:

    opencode export <session-id> | Out-File -Encoding utf8 session.json
    # or convert an existing file:
    Get-Content session.json | Set-Content -Encoding utf8 session-utf8.json

    We'd encourage moving to v2 (npm package @opencode/cli), where the equivalent command is opencode session import <file>. From reading the v2 code, it also decodes local files as UTF-8, so the same PowerShell 5.1 round-trip may well fail there too. If you can confirm it happens on v2, or if you think this is critical for v1, please reply here and we'll take another look.

    Related:

  2. reynold-hu commented on Oct 10, 2026

    @reynold-hu
    Author

    Confirmed on v2 — this does not reproduce there.

    Tested @opencode/cli@2.0.26 on Windows 10 with PowerShell 5.1, importing the same session export twice:

    • UTF-16LE with BOM (from opencode export <id> > session.json): SchemaError: Missing key ["info"]["location"]
    • UTF-8 without BOM: SchemaError: Missing key ["info"]["location"]

    Both inputs reach schema validation with the identical error (the known v1-export/v2-schema difference, #49314), so v2 parses the UTF-16 file correctly.

    Why: v2's session import reads the file with Bun.file(input.file).text() (packages/cli/src/commands/handlers/session/import.ts), and Bun.file().text() decodes a UTF-16 BOM. FSUtil.readJson (packages/util/src/fs-util.ts) is not in that code path, so it never sees this file.

    So this is v1-only, and v1 has the workaround you posted. Closing as not planned — thanks for the triage.

  3. opencode-agent commented on Oct 10, 2026

    @opencode-agent
    Contributor

    Thanks for testing on v2 and for the correction. You're right: Bun.file().text() decodes the UTF-16 BOM, so v2's opencode session import reads the file fine. I was wrong to say v2 might have the same problem. Since v2 gives the same error for the UTF-16LE and UTF-8 files, this only affects v1, and v1 has the Out-File -Encoding utf8 workaround.

    The Missing key ["info"]["location"] error you got when importing a v1 export into v2 is a separate problem, tracked in #49314.

    This issue will be closed as not planned. Thanks again for the thorough report.

  4. opencode-agent commented on Oct 11, 2026

    @opencode-agent
    Contributor

    Closing as not planned. Your test on @opencode/cli@2.0.26 showed that v2 reads UTF-16 files with a BOM correctly, so this only affects v1. v1 only gets critical fixes, and this one has a workaround: in PowerShell 5.1, use Out-File -Encoding utf8 or Set-Content -Encoding utf8 instead of >. The schema error you hit when importing a v1 export into v2 is tracked in #49314. Thanks for the detailed report and for checking v2. If this shows up again on v2, please open a new issue.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions