Skip to content

[bug] Write tool silently writes empty content, overwriting existing files with nothing #33078

Closed as not planned

Description

Description

The Write tool (packages/core/src/tool/write.ts) has no guard against writing empty or whitespace-only content. If the model generates an empty string as content, the tool will overwrite the existing file with nothing. The data is permanently lost with no undo.

The tool's Input schema defines content as Schema.String with no minimum length validation. An empty string "" is accepted and written.

Code location

https://github.com/anomalyco/opencode/blob/dev/packages/core/src/tool/write.ts#L29-L33

Steps to reproduce

  1. Ask the model to write to an existing file with real content
  2. The model generates content as an empty string due to a generation error
  3. The Write tool writes an empty file, overwriting all existing data

Expected behavior

Write tool should warn or block empty content writes to existing files, or at minimum the content should have a minimum length validation.

Environment

OpenCode Desktop 1.17.8, Windows 11

Activity

  1. Necmttn commented on Jun 21, 2026

    @Necmttn

    Empty writes should be treated as destructive when the target exists. Before Write commits, compare target existence, previous size/hash, new byte length, and whether the model explicitly intended truncation.

    If content is empty or whitespace-only and the old file was non-empty, require an explicit truncate flag or user confirmation. The tool result should include previous size/hash and new size, so accidental zero-byte overwrites are detectable and recoverable from editor/VCS history when available.


    Generated with ax.

  2. rekram1-node commented on Jun 22, 2026

    @rekram1-node
    Collaborator

    STOP SPAMMING ISSUES.

  3. yeffmartinezmusic-source commented on Aug 26, 2026

    @yeffmartinezmusic-source

    I can confirm this is still happening as of August 2026. The write tool emptied my orbitian-studios.html file (712 lines → 0 bytes) while editing other HTML files in the same project. No git history was available for recovery. Running OpenCode Desktop on macOS Apple Silicon M4.

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