fix(update): refuse a '='-bearing positional before any write (bd-5247) (#5350)

`--set-metadata` takes ONE key=value per flag, and `update` is variadic over
issue ids. So `bd update ID --set-metadata a=1 b=2 c=3` binds only `a=1` to the
flag; `b=2` and `c=3` become positional issue ids. They fail to resolve, but
`a=1` is still written — a silent 1-of-N write that a caller cannot distinguish
from a full write. #4726 made the command exit nonzero on the unresolved
positionals, but the partial write still lands and the exit code alone does not
tell a script which pairs were dropped.

No issue id contains '=' (the flag-value form is the only source of one), so a
'='-bearing positional is unambiguously a mis-typed flag value. Refuse it up
front, before any write path (direct or proxied), naming the offending pair and
the correct repeated-flag form. This prevents the partial write entirely rather
than only reporting it after the fact.

Fixes #5247

Co-authored-by: Ben Younes <2910651+ousamabenyounes@users.noreply.github.com>
Co-authored-by: Steve Yegge <steve.yegge@gmail.com>
This commit is contained in:
Ben Younes 2026-08-09 11:14:05 +02:00 • committed by GitHub
commit 5811fa8741
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 113 additions and 0 deletions

View file

@ -108,6 +108,13 @@ pointless).`,
}
}()
// Refuse a mis-typed flag value that cobra parsed as a positional issue
// id, before any write path (direct or proxied) can apply a partial
// update. See bd-5247.
if err := errStrayFlagValuePositional(args); err != nil {
return HandleErrorRespectJSON("%s", err)
}
if usesProxiedServer() {
return runUpdateProxiedServer(cmd, rootCtx, args)
}
@ -827,6 +834,21 @@ type updateIDFailure struct {
GuardMismatch bool `json:"guard_mismatch,omitempty"`
}
// errStrayFlagValuePositional refuses, before any write, a positional argument
// that contains '='. --set-metadata takes ONE key=value per flag, so
// `--set-metadata a=1 b=2` silently turns `b=2` into a positional issue id;
// no issue id contains '=', so such a positional is unambiguously a mis-typed
// flag value. Rejecting it up front prevents a partial write that would apply
// only the pairs that happened to bind to a flag (bd-5247).
func errStrayFlagValuePositional(args []string) error {
for _, arg := range args {
if strings.Contains(arg, "=") {
return fmt.Errorf("positional argument %q contains '=', which no issue id does — this is a mis-typed flag value; repeat the flag per pair, e.g. --set-metadata a=1 --set-metadata b=2", arg)
}
}
return nil
}
// reportUpdateFailures emits a per-ID failure report on stderr and returns a
// nonzero exit error — ExitGuardMismatch when every failure is a
// --if-assignee/--if-status guard refusal, 1 otherwise. In --json mode the

View file

@ -0,0 +1,91 @@
// Tests for bd-5247: `bd update --set-metadata a=1 b=2` silently turns `b=2`
// into a positional issue id (--set-metadata takes one key=value per flag).
// Before this guard, `a=1` was written and only the unbound pairs failed, so a
// caller could not tell a full write from a 1-of-N write. The guard refuses a
// `=`-bearing positional before ANY write, so no partial update lands.
//
// This file MUST NOT carry a cgo build tag: it exercises the default sqlite
// backend via a bd binary built with the gms_pure_go tag, reusing the helpers
// in update_multi_id_exit_test.go (same package).
package main
import (
"encoding/json"
"strings"
"testing"
)
func TestErrStrayFlagValuePositional(t *testing.T) {
// A positional carrying '=' is a mis-typed flag value and must be refused.
if err := errStrayFlagValuePositional([]string{"test-abc", "probe_b=2"}); err == nil {
t.Fatal("errStrayFlagValuePositional with a '='-bearing positional = nil, want error")
}
// Plain issue ids (no '=') must pass through untouched.
if err := errStrayFlagValuePositional([]string{"test-abc", "test-def"}); err != nil {
t.Fatalf("errStrayFlagValuePositional with plain ids = %v, want nil", err)
}
if err := errStrayFlagValuePositional(nil); err != nil {
t.Fatalf("errStrayFlagValuePositional(nil) = %v, want nil", err)
}
}
// showStrayMetadata fetches an issue's metadata map via bd show --json.
func showStrayMetadata(t *testing.T, bd, dir, id string) map[string]interface{} {
t.Helper()
stdout, stderr, code := runBDMultiID(t, bd, dir, "show", id, "--json")
if code != 0 {
t.Fatalf("bd show %s failed (exit %d):\nstdout:\n%s\nstderr:\n%s", id, code, stdout, stderr)
}
var details []struct {
ID string `json:"id"`
Metadata map[string]interface{} `json:"metadata"`
}
if err := json.Unmarshal([]byte(stdout), &details); err != nil {
t.Fatalf("parsing show --json for %s: %v\n%s", id, err, stdout)
}
if len(details) != 1 || details[0].ID != id {
t.Fatalf("show --json for %s returned unexpected issues:\n%s", id, stdout)
}
return details[0].Metadata
}
func TestUpdateStrayMetadataPositionalRefusedBeforeWrite(t *testing.T) {
bd, dir := setupMultiIDUpdateDB(t)
id := createMultiIDUpdateIssue(t, bd, dir, "stray metadata target")
// Only `probe_a=1` binds to the flag; `probe_b=2` and `probe_c=3` land as
// positional ids. The command must refuse before writing anything.
stdout, stderr, code := runBDMultiID(t, bd, dir,
"update", id, "--set-metadata", "probe_a=1", "probe_b=2", "probe_c=3")
if code == 0 {
t.Errorf("bd update with a '='-bearing positional exited 0, want nonzero\nstdout:\n%s\nstderr:\n%s", stdout, stderr)
}
if !strings.Contains(stderr, "probe_b=2") {
t.Errorf("stderr does not name the mis-typed pair probe_b=2:\n%s", stderr)
}
// The refusal is before any write: no pair — not even the bound probe_a —
// may have landed. This is the half a message-only fix would leave broken.
meta := showStrayMetadata(t, bd, dir, id)
if len(meta) != 0 {
t.Errorf("metadata is %v, want empty: refusal must prevent the partial write", meta)
}
}
func TestUpdateSetMetadataRepeatedFlagStillWrites(t *testing.T) {
bd, dir := setupMultiIDUpdateDB(t)
id := createMultiIDUpdateIssue(t, bd, dir, "correct form target")
// The correct form (one flag per pair) has no stray positional and must
// still apply normally — the guard does not break valid usage.
stdout, stderr, code := runBDMultiID(t, bd, dir,
"update", id, "--set-metadata", "probe_a=1", "--set-metadata", "probe_b=2")
if code != 0 {
t.Fatalf("bd update with repeated --set-metadata exited %d, want 0\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
}
meta := showStrayMetadata(t, bd, dir, id)
if meta["probe_a"] == nil || meta["probe_b"] == nil {
t.Errorf("metadata = %v, want both probe_a and probe_b written", meta)
}
}