fix(create): hard-fail multi-type same-target --deps (#4626) (#4833)

* fix(create): hard-fail multi-type same-target --deps (#4626)

Expand comma-separated deps, reject collisions that uk_dep_issue_target
cannot store, and fail create when AddDependency errors instead of warning.

* fix(create): validate --deps before CreateIssue (#4626)

parseDepSpecs uniqueness checks ran after CreateIssue, so multi-type
same-target rejections left an orphan issue with a non-zero exit.
Validate first; reuse parsed specs for discovered-from source_repo inherit.

* fix(create): address review — dedupe identical --deps, fix double comma-split, reuse parsed specs, drop internal index name from error

Addresses maphew's CHANGES_REQUESTED review on #4626/#4833:

- checkDepSpecsUniqueTargets -> dedupeDepSpecs: a repeated IDENTICAL
  normalized edge (e.g. blocked-by:X and depends-on:X, both aliasing to
  the same type/target) is now deduped instead of hard-failing. Storage
  already treats a same-pair/same-type add as idempotent; only a
  genuine type conflict on the same target is still rejected.
- Removed expandDepFlagValues's manual comma re-split. Cobra's
  StringSlice flag already CSV-decodes --deps, so the extra split was
  double-decoding and would mangle a CSV-quoted target containing a
  literal comma. parseDepSpecs now trims/skips-empty per already-split
  element only.
- Added discoveredFromParentSpec(specs), reusing the already-parsed
  dependency specs for the discovered-from source_repo lookup instead
  of re-deriving type:target parsing from the raw --deps strings a
  second time. A prior commit on this branch (7a78668) had this wiring;
  the origin/main merge (b956262d) silently dropped it. Wired into both
  call sites the review named: cmd/bd/create.go (direct) and
  cmd/bd/create_proxied_server.go (proxied). create_form.go's own
  raw-string parsing is untouched (separate lenient path, no
  parseDepSpecs involved).
- Reworded the same-target-conflict error to drop the internal
  uk_dep_issue_target index name; message stays domain-oriented.
- Added real-bd-binary regression coverage in
  create_deps_atomic_test.go: multi-type same-target is rejected with
  no orphan issue, and an identical-duplicate edge is deduped rather
  than rejected. Updated create_deps_test.go's comma cases to reflect
  the production representation (cobra hands parseDepSpecs an
  already-split slice, not one comma-joined string).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Vishnu J 2026-07-28 13:52:24 -07:00 • committed by GitHub
commit a66dc623b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 124 additions and 4 deletions

View file

@ -534,8 +534,10 @@ var createCmd = &cobra.Command{
ctx := createCtx
// If a discovered-from dependency is present, inherit source_repo
// from the referenced parent issue.
if dfParent := discoveredFromParent(deps); dfParent != "" {
// from the referenced parent issue. Reuse the already-parsed specs
// (not the raw --deps strings) so this can't drift from parseDepSpec's
// normalization rules.
if dfParent := discoveredFromParentSpec(depSpecs); dfParent != "" {
parentIssue, err := store.GetIssue(ctx, dfParent)
if err == nil && parentIssue.SourceRepo != "" {
issue.SourceRepo = parentIssue.SourceRepo

View file

@ -10,6 +10,10 @@ import (
)
func parseDepSpecs(deps []string) ([]domain.DependencySpec, error) {
// deps arrives already comma-split: cobra's StringSlice flag CSV-decodes
// each --deps value, so re-splitting on "," here would double-decode a
// CSV-quoted target that legitimately contains a comma. Only trim and
// drop empties; splitting is cobra's job.
var out []domain.DependencySpec
for _, raw := range deps {
raw = strings.TrimSpace(raw)
@ -22,6 +26,40 @@ func parseDepSpecs(deps []string) ([]domain.DependencySpec, error) {
}
out = append(out, spec)
}
return dedupeDepSpecs(out)
}
// dedupeDepSpecs collapses repeated identical edges and rejects edges that
// would collide on the (issue_id, target) dependency-uniqueness key with a
// *different* type. Type is not part of that key, so two different types on
// the same target can't both be stored — but the storage layer already
// treats a repeated identical (target, type) add as idempotent, so an exact
// repeat here must be deduped rather than rejected.
// GH#4626: discovered-from:X,blocked-by:X used to silently keep only one edge.
func dedupeDepSpecs(specs []domain.DependencySpec) ([]domain.DependencySpec, error) {
// Key: swapDirection|target — same effective endpoint pair for a new issue.
seen := make(map[string]types.DependencyType, len(specs))
out := make([]domain.DependencySpec, 0, len(specs))
for _, s := range specs {
key := fmt.Sprintf("%t|%s", s.SwapDirection, s.TargetID)
prev, ok := seen[key]
switch {
case !ok:
seen[key] = s.Type
out = append(out, s)
case prev != s.Type:
return nil, fmt.Errorf(
"--deps cannot attach both %q and %q to the same target %q: a target can only carry one dependency type at a time. Pick one type, or open a separate issue for the second relationship (GH#4626)",
prev, s.Type, s.TargetID,
)
default:
// Identical edge repeated (e.g. blocked-by and depends-on both
// normalize to the same type/target) — silently dedupe.
}
}
if len(out) == 0 {
return nil, nil
}
return out, nil
}
@ -102,6 +140,20 @@ func discoveredFromParent(deps []string) string {
return ""
}
// discoveredFromParentSpec is discoveredFromParent's counterpart for callers
// that already ran deps through parseDepSpecs. It reuses the canonical
// parsed/normalized specs instead of re-deriving type:target parsing from
// raw strings a second time, so it can't drift from parseDepSpec's rules
// (e.g. the depends-on/blocked-by aliasing).
func discoveredFromParentSpec(specs []domain.DependencySpec) string {
for _, s := range specs {
if s.Type == types.DepDiscoveredFrom && s.TargetID != "" {
return s.TargetID
}
}
return ""
}
func overlayYAMLPrefix(dbPrefix string) string {
if v := strings.TrimSpace(config.GetString("issue-prefix")); v != "" {
return v

View file

@ -309,4 +309,39 @@ func TestCreateDepsAtomicity(t *testing.T) {
t.Error("issue \"bad dep type issue\" persisted despite invalid dep type")
}
})
// GH#4626: --deps discovered-from:X,blocked-by:X used to silently keep
// only one edge, because dependency uniqueness is per (issue_id, target),
// not per type. Two DIFFERENT types on the same target must now be
// rejected before create runs (real-binary regression: exit code and
// absence of an orphan, which a direct parseDepSpecs unit test can't
// prove).
t.Run("multi_type_same_target_rejected_before_create_no_orphan", func(t *testing.T) {
out, err := runCreateDepsBDRaw(bd, dir, "create", "multi-type collision issue", "--json",
"--deps", "discovered-from:"+blocker, "--deps", "blocked-by:"+blocker)
if err == nil {
t.Errorf("create with multi-type same-target --deps exited 0; output:\n%s", out)
}
if !strings.Contains(out, blocker) {
t.Errorf("error output should name the colliding target %s, got:\n%s", blocker, out)
}
if createDepsIssueTitles(t, bd, dir)["multi-type collision issue"] {
t.Error("issue \"multi-type collision issue\" persisted despite rejected multi-type same-target --deps")
}
})
// Same target, same normalized type (blocked-by and depends-on both
// alias to DepBlocks with no swap) must NOT be rejected: storage already
// treats a repeated identical edge as idempotent, so --deps should
// dedupe rather than hard-fail.
t.Run("duplicate_identical_dep_is_deduped_not_rejected", func(t *testing.T) {
out := runCreateDepsBD(t, bd, dir, "create", "deduped dep issue", "--json",
"--deps", "blocked-by:"+blocker, "--deps", "depends-on:"+blocker)
child := createDepsExtractID(t, out)
depOut := runCreateDepsBD(t, bd, dir, "dep", "list", child, "--json")
if !strings.Contains(depOut, blocker) {
t.Errorf("dep list %s should include %s:\n%s", child, blocker, depOut)
}
})
}

View file

@ -87,6 +87,35 @@ func TestParseDepSpecs(t *testing.T) {
in: []string{":bd-1"},
wantErr: true,
},
{
// Cobra's StringSlice flag CSV-decodes "--deps a,b" into two
// elements before parseDepSpecs ever sees them; this is the
// representation parseDepSpecs actually receives in production,
// not a single comma-joined string (parseDepSpecs must not
// re-split on "," or it double-decodes a CSV-quoted value that
// legitimately contains a comma).
name: "multi-type different targets (already split by cobra)",
in: []string{"discovered-from:bd-20", "blocks:bd-15"},
want: []domain.DependencySpec{
{Type: types.DepDiscoveredFrom, TargetID: "bd-20"},
{Type: types.DepBlocks, TargetID: "bd-15", SwapDirection: true},
},
},
{
name: "multi-type same target rejected",
in: []string{"discovered-from:bd-1", "blocked-by:bd-1"},
wantErr: true,
},
{
name: "duplicate identical edge is deduped, not rejected",
in: []string{"blocked-by:bd-1", "depends-on:bd-1"},
// Both aliases normalize to the same {DepBlocks, bd-1, no swap}
// edge; storage already treats a repeated identical add as
// idempotent, so this must dedupe rather than error.
want: []domain.DependencySpec{
{Type: types.DepBlocks, TargetID: "bd-1"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View file

@ -131,8 +131,10 @@ func runCreateProxiedSingle(_ *cobra.Command, ctx context.Context, in createInpu
InheritLabelsFromParent: !in.noInheritLabels && in.parentID != "",
Dependencies: deps,
WaitsFor: waitsFor,
DiscoveredFromParent: discoveredFromParent(in.deps),
ForcePrefix: in.force,
// Reuse the already-parsed specs (not the raw --deps strings) so
// this can't drift from parseDepSpec's normalization rules.
DiscoveredFromParent: discoveredFromParentSpec(deps),
ForcePrefix: in.force,
}
var result domain.CreateIssueResult