opencode
opencode copied to clipboard
fix: handle missing file gracefully when processing file attachments
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"
Solution
Wrap the stat() call with .catch(() => null) and handle the missing file case:
- Publish Session.Event.Error - triggers a toast notification in TUI
- Add a synthetic text part with the error message - visible to the model
- Preserve the original file part - user's intent is recorded This follows the existing pattern used by ReadTool error handling in the same file.