From 786ed8a4b0b36c667d9e07792775e6abea9e41b2 Mon Sep 17 00:00:00 2001 From: Julian Knutsen Date: Mon, 19 Oct 2026 07:10:26 +0000 Subject: [PATCH] fix(show): exit 1 when bd show --as-of reads no issue (#7687) showIssueAsOf reported each failed AS OF lookup on stderr and continued, but never counted what it read and returned nil, so a bad ref (an instant or a versioned-history address the Dolt AS OF read does not take, an unknown branch or commit) or an issue absent at the ref exited 0 with an empty stdout, under --json too. Its proxied-server twin, runShowProxiedAsOf, returned nothing and its dispatcher returned nil. Both now count the issues they read and, when that is zero, end the way plain bd show ends a batch that found nothing: show's JSON error envelope on stdout under --json, a bare exit 1 otherwise, with the per-id reasons still on stderr. A batch that reads at least one issue still exits 0, as plain show's does. The read itself is unchanged (Dolt AS OF). Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) --- .github/scripts/embedded-cmd-test-shards.txt | 1 + CHANGELOG.md | 8 ++ cmd/bd/show_children.go | 18 ++++ cmd/bd/show_embedded_test.go | 103 +++++++++++++++++++ cmd/bd/show_proxied_integration_test.go | 39 ++++++- cmd/bd/show_proxied_server.go | 14 ++- cmd/bd/show_test.go | 40 +++++++ 7 files changed, 218 insertions(+), 5 deletions(-) diff --git a/.github/scripts/embedded-cmd-test-shards.txt b/.github/scripts/embedded-cmd-test-shards.txt index 7b45f7915..4ee97330a 100644 --- a/.github/scripts/embedded-cmd-test-shards.txt +++ b/.github/scripts/embedded-cmd-test-shards.txt @@ -138,6 +138,7 @@ 100 54 TestEmbeddedFormatJSONAliasMatchesJSON 100 54 TestEmbeddedInitAgentArtifactsGolden 100 54 TestEmbeddedSearchConcurrent +100 54 TestEmbeddedShowAsOfFailureExitCodes 100 55 TestEmbeddedUndefer 100 55 TestEmbeddedVersionedHistorySwitchScansEveryRowNotOnePage diff --git a/CHANGELOG.md b/CHANGELOG.md index ea893eb2f..f7428c36c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -790,6 +790,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 with no `.local_version` witness. A pre-1.0 witness, or a `.beads/dolt` that holds anything, is still refused, and commands other than `bd init` are decided as before. +- **`bd show --as-of` exits 1 when it reads no issue.** A ref the Dolt AS OF + read does not take (an instant, a versioned-history address), a ref that + does not exist, or an issue that did not exist at the ref was reported on + stderr while the command exited 0 with nothing on stdout, even under + `--json`. It now ends the way plain `bd show` does: exit 1, with the JSON + error envelope on stdout under `--json`. A batch that reads at least one of + its ids still exits 0. Both the direct and the proxied-server routes + changed. - **Time-bound filters and the keyset cursor compare the same instant on every engine, whatever zone they are given in.** The storage filter builder binds every time bound (`created_*`, `updated_*`, `closed_*`, `started_*`, diff --git a/cmd/bd/show_children.go b/cmd/bd/show_children.go index 010c11b01..5daea300b 100644 --- a/cmd/bd/show_children.go +++ b/cmd/bd/show_children.go @@ -87,6 +87,7 @@ func showIssueChildren(ctx context.Context, args []string, jsonOut bool, shortMo // This requires a versioned storage backend (e.g., Dolt). func showIssueAsOf(ctx context.Context, args []string, ref string, shortMode bool) error { var allIssues []*types.Issue + foundCount := 0 for idx, id := range args { issue, err := store.AsOf(ctx, id, ref) if err != nil { @@ -97,6 +98,7 @@ func showIssueAsOf(ctx context.Context, args []string, ref string, shortMode boo fmt.Fprintf(os.Stderr, "Issue %s did not exist at %s\n", id, ref) continue } + foundCount++ if shortMode { fmt.Println(formatShortIssue(issue)) @@ -122,8 +124,24 @@ func showIssueAsOf(ctx context.Context, args []string, ref string, shortMode boo fmt.Println() } + if foundCount == 0 { + return showAsOfFoundNothing() + } if jsonOutput && len(allIssues) > 0 { return outputJSON(allIssues) } return nil } + +// showAsOfFoundNothing ends an --as-of read, on either route, in which no +// lookup succeeded. Each failed lookup has already said why on stderr, so this +// is plain show's ending for a batch that found nothing: the JSON error +// envelope on stdout under --json, a bare exit 1 otherwise. A batch that read +// at least one issue still exits 0, as plain show's does. +func showAsOfFoundNothing() error { + if jsonOutput { + return HandleErrorWithHintRespectJSON("no issues found matching the provided IDs", + "check the --as-of ref (a branch name, or a full 32-character commit hash from 'bd history ') and that each issue existed at it") + } + return SilentExit() +} diff --git a/cmd/bd/show_embedded_test.go b/cmd/bd/show_embedded_test.go index b142aa2f8..1066cdeaa 100644 --- a/cmd/bd/show_embedded_test.go +++ b/cmd/bd/show_embedded_test.go @@ -484,6 +484,109 @@ func TestEmbeddedShowDetailAndCurrentHistoryAndCurrent(t *testing.T) { }) } +// TestEmbeddedShowAsOfFailureExitCodes pins `bd show --as-of` to plain show's +// failure contract on the direct route. Each lookup that fails is reported on +// stderr and the others still render; the command fails only when no issue +// could be read at the ref, with show's JSON error envelope on stdout under +// --json and a bare exit 1 otherwise. A ref the Dolt AS OF read does not take +// (a versioned-history address, an instant), a branch that does not exist, +// and an issue created after the ref each used to exit 0 with nothing on +// stdout, which a caller cannot tell from a successful read. +func TestEmbeddedShowAsOfFailureExitCodes(t *testing.T) { + if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { + t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests") + } + t.Parallel() + + bd := buildEmbeddedBD(t) + dir, beadsDir, _ := bdInit(t, bd, "--prefix", "sx") + + issue := bdCreate(t, bd, dir, "AsOf original", "--type", "task") + store := openStore(t, beadsDir, "sx") + ref, err := store.GetCurrentCommit(t.Context()) + if err != nil { + t.Fatalf("GetCurrentCommit: %v", err) + } + store.Close() // release flock before subprocess + later := bdCreate(t, bd, dir, "Created after the ref", "--type", "task") + + show := func(t *testing.T, args ...string) (stdout, stderr string, exitCode int) { + t.Helper() + cmd := exec.Command(bd, append([]string{"show"}, args...)...) + cmd.Dir = dir + cmd.Env = bdEnv(dir) + out, errOut, err := runCommandBuffers(t, cmd) + if err != nil { + var ee *exec.ExitError + if !errors.As(err, &ee) { + t.Fatalf("bd show %s did not run: %v", strings.Join(args, " "), err) + } + exitCode = ee.ExitCode() + } + return out.String(), errOut.String(), exitCode + } + + for _, tc := range []struct { + name string + args []string + // why is what the per-id report on stderr has to name. + why string + }{ + {"versioned_history_address", []string{issue.ID, "--as-of", "asof:" + issue.ID + ":1"}, "invalid ref"}, + {"instant", []string{issue.ID, "--as-of", "2026-10-17T00:00:00Z"}, "invalid ref"}, + {"unknown_branch", []string{issue.ID, "--as-of", "no-such-branch"}, "no-such-branch"}, + {"issue_created_after_ref", []string{later.ID, "--as-of", ref}, later.ID}, + {"issue_created_after_ref_short", []string{later.ID, "--as-of", ref, "--short"}, later.ID}, + } { + t.Run(tc.name, func(t *testing.T) { + stdout, stderr, code := show(t, tc.args...) + if code != 1 || stdout != "" || !strings.Contains(stderr, tc.why) { + t.Errorf("bd show %s exited %d, want 1 with empty stdout and %q on stderr\nstdout:\n%s\nstderr:\n%s", + strings.Join(tc.args, " "), code, tc.why, stdout, stderr) + } + + args := append(append([]string{}, tc.args...), "--json") + stdout, stderr, code = show(t, args...) + var envelope struct { + Error string `json:"error"` + Hint string `json:"hint"` + } + if code != 1 || json.Unmarshal([]byte(stdout), &envelope) != nil || + envelope.Error != "no issues found matching the provided IDs" || envelope.Hint == "" { + t.Errorf("bd show %s exited %d, want 1 with show's JSON error envelope on stdout\nstdout:\n%s\nstderr:\n%s", + strings.Join(args, " "), code, stdout, stderr) + } + if !strings.Contains(stderr, tc.why) { + t.Errorf("bd show %s stderr does not name %q:\n%s", strings.Join(args, " "), tc.why, stderr) + } + }) + } + + // Plain show's partial failure: the issues that were read are the + // answer, the miss is reported on stderr, and the command succeeds. + t.Run("partial_failure_exits_zero", func(t *testing.T) { + stdout, stderr, code := show(t, issue.ID, later.ID, "--as-of", ref, "--json") + var shown []struct { + ID string `json:"id"` + Title string `json:"title"` + } + if code != 0 || json.Unmarshal([]byte(stdout), &shown) != nil || len(shown) != 1 || + shown[0].ID != issue.ID || shown[0].Title != "AsOf original" { + t.Errorf("bd show %s %s --as-of %s --json exited %d, want 0 with an array of %s alone\nstdout:\n%s\nstderr:\n%s", + issue.ID, later.ID, ref, code, issue.ID, stdout, stderr) + } + if !strings.Contains(stderr, later.ID) { + t.Errorf("--json stderr does not report %s:\n%s", later.ID, stderr) + } + + stdout, stderr, code = show(t, issue.ID, later.ID, "--as-of", ref) + if code != 0 || !strings.Contains(stdout, "AsOf original") || !strings.Contains(stderr, later.ID) { + t.Errorf("bd show %s %s --as-of %s exited %d, want 0 rendering %s and reporting %s on stderr\nstdout:\n%s\nstderr:\n%s", + issue.ID, later.ID, ref, code, issue.ID, later.ID, stdout, stderr) + } + }) +} + // TestEmbeddedShowConcurrent exercises show operations concurrently. func TestEmbeddedShowConcurrent(t *testing.T) { if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" { diff --git a/cmd/bd/show_proxied_integration_test.go b/cmd/bd/show_proxied_integration_test.go index b518aac7d..353d4e844 100644 --- a/cmd/bd/show_proxied_integration_test.go +++ b/cmd/bd/show_proxied_integration_test.go @@ -799,10 +799,47 @@ func TestProxiedServerShow3(t *testing.T) { p := newSharedProxiedProject(t, bd, "saoi") issue := bdProxiedCreate(t, bd, p.dir, "AsOf invalid", "--type", "task") - _, stderr, _ := bdProxiedRunBuffers(t, bd, p.dir, "show", issue.ID, "--as-of", "'; DROP TABLE issues; --") + stdout, stderr := bdProxiedShowFail(t, bd, p.dir, issue.ID, "--as-of", "'; DROP TABLE issues; --") if !strings.Contains(stderr, "invalid ref") { t.Errorf("expected 'invalid ref' in stderr, got: %s", stderr) } + if stdout != "" { + t.Errorf("expected nothing on stdout when no issue was read, got: %s", stdout) + } + }) + + // The proxied twin of TestEmbeddedShowAsOfFailureExitCodes: when no issue + // can be read at the ref the command fails the way plain show does, and a + // batch that read at least one issue still succeeds. + t.Run("show_as_of_nothing_found_fails_like_show", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "saon") + issue := bdProxiedCreate(t, bd, p.dir, "AsOf kept", "--type", "task") + hash := proxiedCurrentCommit(t, p) + later := bdProxiedCreate(t, bd, p.dir, "AsOf created later", "--type", "task") + + stdout, stderr := bdProxiedShowFail(t, bd, p.dir, later.ID, "--as-of", hash) + if stdout != "" || !strings.Contains(stderr, later.ID) { + t.Errorf("want empty stdout and %s reported on stderr\nstdout:\n%s\nstderr:\n%s", later.ID, stdout, stderr) + } + + stdout, stderr = bdProxiedShowFail(t, bd, p.dir, later.ID, "--as-of", hash, "--json") + var envelope struct { + Error string `json:"error"` + Hint string `json:"hint"` + } + if err := json.Unmarshal([]byte(stdout), &envelope); err != nil || + envelope.Error != "no issues found matching the provided IDs" || envelope.Hint == "" { + t.Errorf("--json stdout is not show's error envelope (%v):\n%s", err, stdout) + } + if !strings.Contains(stderr, later.ID) { + t.Errorf("--json stderr does not report %s:\n%s", later.ID, stderr) + } + + arr := bdProxiedShowDetailsAll(t, bd, p.dir, issue.ID, later.ID, "--as-of", hash) + if len(arr) != 1 || arr[0]["id"] != issue.ID || arr[0]["title"] != "AsOf kept" { + t.Errorf("partial --as-of read: want only %s as of the ref, got %v", issue.ID, arr) + } }) t.Run("show_as_of_short_mode", func(t *testing.T) { diff --git a/cmd/bd/show_proxied_server.go b/cmd/bd/show_proxied_server.go index 741bbbb63..45b3582d6 100644 --- a/cmd/bd/show_proxied_server.go +++ b/cmd/bd/show_proxied_server.go @@ -126,7 +126,7 @@ func runShowProxiedServer(cmd *cobra.Command, ctx context.Context, args []string switch { case in.asOfRef != "": - runShowProxiedAsOf(ctx, uw, in) + return runShowProxiedAsOf(ctx, uw, in) case in.thread: return runShowProxiedThread(ctx, uw, in) case in.refs: @@ -218,8 +218,9 @@ func reportIssueLookupFailure(verb, id string, err error) { fmt.Fprintf(os.Stderr, "Error %s %s: %v\n", verb, id, err) } -func runShowProxiedAsOf(ctx context.Context, uw uow.UnitOfWork, in *showProxiedInput) { +func runShowProxiedAsOf(ctx context.Context, uw uow.UnitOfWork, in *showProxiedInput) error { var jsonIssues []*types.Issue + foundCount := 0 for idx, id := range in.ids { issue, err := uw.IssueUseCase().AsOf(ctx, id, in.asOfRef) if err != nil { @@ -230,6 +231,7 @@ func runShowProxiedAsOf(ctx context.Context, uw uow.UnitOfWork, in *showProxiedI fmt.Fprintf(os.Stderr, "Issue %s did not exist at %s\n", id, in.asOfRef) continue } + foundCount++ if in.shortMode { fmt.Println(formatShortIssue(issue)) @@ -250,9 +252,13 @@ func runShowProxiedAsOf(ctx context.Context, uw uow.UnitOfWork, in *showProxiedI } fmt.Println() } - if jsonOutput && len(jsonIssues) > 0 { - _ = outputJSON(jsonIssues) + if foundCount == 0 { + return showAsOfFoundNothing() } + if jsonOutput && len(jsonIssues) > 0 { + return outputJSON(jsonIssues) + } + return nil } func runShowProxiedRefs(ctx context.Context, uw uow.UnitOfWork, in *showProxiedInput) { diff --git a/cmd/bd/show_test.go b/cmd/bd/show_test.go index 0a4a46764..c5687630e 100644 --- a/cmd/bd/show_test.go +++ b/cmd/bd/show_test.go @@ -137,6 +137,46 @@ func TestShow_NotFoundJSON(t *testing.T) { } } +// TestShow_AsOfFoundNothingExitsNonZero holds --as-of to the contract the two +// tests above pin for plain show, on the server leg's direct route: when no +// issue can be read at the ref (a ref the Dolt AS OF read does not take, or an +// issue absent at it) the command exits non-zero, with show's JSON error +// envelope on stdout under --json. Both used to exit 0 with nothing on stdout. +func TestShow_AsOfFoundNothingExitsNonZero(t *testing.T) { + tmpDir := setupCLITestDB(t) + + out := runBDInProcess(t, tmpDir, "create", "AsOf server", "-p", "1", "--json") + jsonStart := strings.Index(out, "{") + if jsonStart < 0 { + t.Fatalf("no JSON found in create output: %s", out) + } + var issue map[string]interface{} + if err := json.Unmarshal([]byte(out[jsonStart:]), &issue); err != nil { + t.Fatalf("failed to parse create output: %v, output: %s", err, out) + } + id := issue["id"].(string) + + stdout, stderr, err := runBDInProcessAllowError(t, tmpDir, "show", id, "--as-of", "asof:"+id+":1") + if err == nil || stdout != "" || !strings.Contains(stderr, "invalid ref") { + t.Errorf("bd show %s --as-of asof:%s:1 returned %v, want an error with empty stdout and 'invalid ref' on stderr\nstdout: %s\nstderr: %s", id, id, err, stdout, stderr) + } + + stdout, stderr, err = runBDInProcessAllowError(t, tmpDir, "show", "test-nonexistent", "--as-of", "main", "--json") + if err == nil { + t.Errorf("expected error for an issue absent at the ref with --json, but command succeeded\nstdout: %s\nstderr: %s", stdout, stderr) + } + var errResp map[string]interface{} + if jsonErr := json.Unmarshal([]byte(stdout), &errResp); jsonErr != nil { + t.Fatalf("expected show's JSON error envelope on stdout, got parse error: %v\nStdout: %s", jsonErr, stdout) + } + if errField, _ := errResp["error"].(string); errField != "no issues found matching the provided IDs" { + t.Errorf("expected plain show's 'error' text, got: %s", stdout) + } + if !strings.Contains(stderr, "test-nonexistent") { + t.Errorf("expected the failed lookup reported on stderr, got: %s", stderr) + } +} + // TestShow_NotFoundHintExplainsAmbiguity guards ga-m6inyb: a never-existed ID // and a deleted/purged ID currently print the identical "not found" text, so // a reader has no way to know the two are different situations (deleted