opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix: handle missing file gracefully when processing file attachments

Open ochen1 opened this issue 1 week ago • 0 comments

Problem

TL;DR - attach/mention (e.g., @somefile.txt) any file and ask OpenCode to delete it, wait for completion, up arrow, enter.

When a user attached a file (e.g., @somefile.txt) and that file was deleted before submitting the message, Bun.file(filepath).stat() threw an unhandled ENOENT error that printed raw to the TUI, breaking the display: e.g.

ENOENT: no such file or directory, statx '/workspace/ANALYSIS_INDEX.md'
    path: "/workspace/ANALYSIS_INDEX.md",
 syscall: "statx",
   errno: -2,
    code: "ENOENT"
Screenshot 2026-01-02 105744

Solution

Wrap the stat() call with .catch(() => null) and handle the missing file case:

  1. Publish Session.Event.Error - triggers a toast notification in TUI
  2. Add a synthetic text part with the error message - visible to the model
  3. Preserve the original file part - user's intent is recorded This follows the existing pattern used by ReadTool error handling in the same file.

Testing

Screenshot 2026-01-02 111516 Screenshot 2026-01-02 113007

ochen1 avatar Jan 02 '26 18:01 ochen1