fix: file_read should handle directory paths gracefully #15

Closed
opened 2026-05-14 08:52:08 +00:00 by mathias · 1 comment
Owner

Summary

file_read currently returns an error when given a directory path instead of gracefully falling back to dir_list behaviour. This forces callers to know in advance whether a path is a file or directory.

Current behaviour

file_read(path: "cmd") → error: "json: cannot unmarshal array into Go value of type gitea.FileContents"

Expected behaviour

Option A (preferred): if path is a directory, return directory listing (same format as dir_list).
Option B: return a clear error message: "path 'cmd' is a directory — use dir_list instead".

Gitea API

GET /api/v1/repos/{owner}/{repo}/contents/{filepath} returns array for dirs, object for files — already detectable from response type.

Required token permission

No new permissions — read:repository, same as current.

Risk classification

LOW — read-only, UX improvement only.

Implementation notes

  • Unmarshal response: try object first, fall back to array
  • If array: either return dir listing or return typed error (decide in PR)
  • Update file_read docstring to document behaviour
  • Add test case: call file_read on a directory path

Motivation

During 2026-05-14 session: called file_read(path: "cmd") on coo-agent, got cryptic unmarshal error. Required a separate dir_list call. Small friction, but repeated often during repo exploration.

## Summary `file_read` currently returns an error when given a directory path instead of gracefully falling back to `dir_list` behaviour. This forces callers to know in advance whether a path is a file or directory. ## Current behaviour ``` file_read(path: "cmd") → error: "json: cannot unmarshal array into Go value of type gitea.FileContents" ``` ## Expected behaviour Option A (preferred): if path is a directory, return directory listing (same format as `dir_list`). Option B: return a clear error message: `"path 'cmd' is a directory — use dir_list instead"`. ## Gitea API `GET /api/v1/repos/{owner}/{repo}/contents/{filepath}` returns array for dirs, object for files — already detectable from response type. ## Required token permission No new permissions — `read:repository`, same as current. ## Risk classification **LOW** — read-only, UX improvement only. ## Implementation notes - Unmarshal response: try object first, fall back to array - If array: either return dir listing or return typed error (decide in PR) - Update `file_read` docstring to document behaviour - Add test case: call file_read on a directory path ## Motivation During 2026-05-14 session: called `file_read(path: "cmd")` on `coo-agent`, got cryptic unmarshal error. Required a separate `dir_list` call. Small friction, but repeated often during repo exploration.
Author
Owner

Shipped in v0.2.3. Closing during cleanup pass.

Shipped in v0.2.3. Closing during cleanup pass.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: mathias/gitea-mcp#15