Skip to content

fix(session): return empty slice for nonexistent sessions directory - #1699

Closed
ege-arhan wants to merge 1 commit into
alibaba:mainfrom
ege-arhan:fix/issue-1696
Closed

ege-arhan wants to merge 1 commit into
alibaba:mainfrom
ege-arhan:fix/issue-1696

Conversation

@ege-arhan

Copy link
Copy Markdown

Fixes #1696

Summary

When a repository has no prior review sessions, internal/session.ListSessions previously returned nil, nil on the os.IsNotExist(err) path. When serialized by ocr session list --json, this produced JSON null instead of an empty array [], making CLI consumers handle two different shapes for an empty list of sessions.

Changes

  • Updated ListSessions to return an empty initialized slice []Summary{} when the sessions directory does not exist.
  • Updated TestListSessions_EmptyRepoReturnsEmptySlice in internal/session to verify non-nil empty slice.
  • Added TestRunSessionList_JSON_MissingSessionsDirReturnsEmptyArray in cmd/opencodereview to ensure the JSON command consistently outputs [].

@CLAassistant

CLAassistant commented Oct 10, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

@NanaseInori NanaseInori left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed d4dc107. Returning an initialized empty slice makes the missing-directory case consistent with an existing empty directory. runSessionList passes that slice directly to the JSON encoder, and the added tests cover both the non-nil slice and the command's [] output. No blocking issue found.

Code review only; tests were not run in this review.

@kittimzhe kittimzhe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally on the PR branch — this is the right fix, and it's safe beyond the JSON path.

Local run: go test ./internal/session/ ./cmd/opencodereview/ -run 'TestListSessions|TestRunSessionList' — both packages pass, including the new TestRunSessionList_JSON_MissingSessionsDirReturnsEmptyArray and the renamed empty-repo test.

Caller audit: all four production call sites handle the empty case with len()/range (session_cmd.go:183 completions, :241 empty check, :275 limit, viewer/handler.go:40), so none of them can tell nil from empty — no behavioral risk outside the JSON encoder, where nil marshals as null and breaks jq-style consumers (.[0] on null errors).

One semantic worth a sentence in the description: the viewer's separate ListSessions (internal/viewer/store.go:115, same name, different package) intentionally returns an error for a missing repo dir rather than an empty list — it needs repo identity to render the page. So the two functions now deliberately differ on missing-input semantics (CLI: empty list; viewer: error), which is correct but easy to trip over in future refactoring.

@ege-arhan ege-arhan closed this by deleting the head repository Oct 24, 2026
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.

bug(session): session list --json prints null for a repo with no sessions, but [] once the directory exists

4 participants