Skip to content

fix(explore): avoid panic when printing an empty result set - #68

Open
thegoodengineer wants to merge 1 commit into
openai:mainfrom
thegoodengineer:fix/explore-print-empty-panic
Open

fix(explore): avoid panic when printing an empty result set#68
thegoodengineer wants to merge 1 commit into
openai:mainfrom
thegoodengineer:fix/explore-print-empty-panic

Conversation

@thegoodengineer

Copy link
Copy Markdown

Summary

Pressing p ("print and exit") in the --format explore viewer panics when the result set is empty.

getSelectedContent indexes rowData at the table cursor without checking that a row exists:

https://github.com/openai/openai-cli/blob/7d87ee2/internal/jsonview/explorer.go#L423-L434

An empty array or object builds a TableView with no rows, table.Cursor() returns 0, and the index panics.

navigateForward already guards this exact case (added in TestNavigateForward_EmptyRowData), so this looks like the same oversight in the sibling path rather than an intended difference.

How it is reached

Any list endpoint that returns no results. ExploreJSONStream collects zero items, marshalItemsToJSONArray returns [], and newTableView builds a table with no rows:

https://github.com/openai/openai-cli/blob/7d87ee2/internal/jsonview/explorer.go#L331-L349

So on an account with no files:

openai files list --format explore

then press p, and the CLI exits with a runtime panic and a Go stack trace instead of printing anything. The same applies to a top level empty object via ExploreJSON.

Every other key binding (, , , , r, q) already handles the empty view fine. p is the only one that crashes.

Reproduction

Reverting just the one line change and running the test added here:

--- FAIL: TestGetSelectedContent_EmptyRowData (0.00s)
    --- FAIL: TestGetSelectedContent_EmptyRowData/[] (0.00s)
        Error: func (assert.PanicTestFunc) should not panic
            Panic value: runtime error: index out of range [0] with length 0
            Panic stack:
            github.com/openai/openai-cli/internal/jsonview.(*JSONViewer).getSelectedContent
                internal/jsonview/explorer.go:429
            github.com/openai/openai-cli/internal/jsonview.(*JSONViewer).Update
                internal/jsonview/explorer.go:415

With the fix applied, both cases pass.

Fix

Bounds check the cursor before indexing, and fall back to the container the view is already displaying, so p prints [] or {}. That matches the existing fallback for non table views a few lines above, which returns GetData().Raw.

The test drives Update with the actual p key message rather than calling the unexported helper directly, so it covers the real key binding path and asserts the printed output, not just the absence of a panic.

Notes

  • No behavior change for non empty views: the cursor is always in range there, so the existing branch is taken unchanged.
  • go build ./... and go vet ./... are clean; go test ./internal/... passes apart from internal/autocomplete, which fails identically on an unmodified checkout in my environment (it shells out to /bin/bash and I am on Windows), so it is unrelated to this change.

`getSelectedContent` indexed `rowData` at the table cursor without checking
that a row exists. An empty array or object builds a TableView with no rows,
so pressing "p" on one crashed the CLI with an index out of range panic.

This is reachable whenever a list endpoint returns no results, since
`ExploreJSONStream` marshals zero items into `[]` and builds the table from
that, e.g. `openai files list --format explore` on an account with no files.

`navigateForward` already guards the same empty `rowData` case; this applies
the equivalent check to the print path and falls back to the container that
the view is displaying, so "p" prints `[]` or `{}` instead of panicking.
@thegoodengineer
thegoodengineer requested a review from a team as a code owner August 17, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant