Skip to content

Commit 1e7cea5

Browse files
committed
bugs: don't let one existing bug end up depending on itself
scan_existing_bugs matches nodes against open bugs independently, so one bug can come back as the bug for several of them: a bug covering a package and its dependency matches both. Bugzilla knows a single bug there, and when a dependency path runs between two such nodes, filing walks it and asks for that bug to depend on a bug which already depends on it: POST /rest/bug depends_on: [597] -> creates 901 PUT /rest/bug/597 depends_on: {add: [901]} which bugzilla rejects part way through, having already filed some of the bugs. Merge the nodes matched to the same bug before anything is filed. What was a path between them becomes an ordinary cycle, which merge_cycles already folds together. For that to be enough merge_nodes has to carry the bug number through a merge rather than drop it, which also stops a merged node filing a duplicate of a bug it had already been matched to, and a merged node must not obsolete the bug it ends up keeping. Nodes matched to different bugs are refused rather than guessed at: the old behavior silently dropped both numbers and filed a third bug, leaving two stale ones behind. The same reasoning applies to obsoletion, so a bug kept as one node's own is no longer resolved as obsolete on behalf of another; that pair of answers to the same bug is contradictory, and keeping it open is the recoverable half. Name the node and bug on every write while here, so a rejection says which one it was about, as asked for on the issue. Resolves: #229 Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
1 parent bbac18e commit 1e7cea5

3 files changed

Lines changed: 167 additions & 16 deletions

File tree

‎NEWS.rst‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22
Release Notes
33
=============
44

5+
pkgdev 0.2.18 (unreleased)
6+
--------------------------
7+
8+
**pkgdev bugs:**
9+
10+
- bugs: fix a "circular dependency" error part way through filing, when one
11+
existing bug was matched by several nodes and a dependency path ran between
12+
them, which made that bug depend on a bug depending on it. Nodes matched to
13+
the same bug are now merged into one, and a bug kept as a node's own bug is
14+
no longer obsoleted by another (Arthur Zamarin, #229)
15+
16+
- bugs: a bugzilla error while filing now names the bug and packages being
17+
filed or modified (Arthur Zamarin, #229)
18+
519
pkgdev 0.2.17 (2026-08-14)
620
--------------------------
721

‎src/pkgdev/scripts/pkgdev_bugs.py‎

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
BugQuery,
2727
BugUpdate,
2828
Bugzilla,
29+
BugzillaError,
2930
ListChange,
3031
NewBug,
3132
PackageList,
@@ -289,6 +290,15 @@ def parse_atom(pkg: str):
289290
raise exc
290291

291292

293+
@contextlib.contextmanager
294+
def _naming(what: str):
295+
"""Say what was being filed when bugzilla rejects a change."""
296+
try:
297+
yield
298+
except BugzillaError as exc:
299+
raise BugzillaError(f"{what}: {exc}") from exc
300+
301+
292302
class GraphNode:
293303
__slots__ = ("bugno", "category", "cc_arches", "edges", "obsoletes", "pkgs", "summary")
294304

@@ -387,7 +397,8 @@ def file_bug(
387397
if self.bugno is not None:
388398
# an already existing bug may still be missing deps, and may supersede older bugs
389399
if deps := self.file_missing_deps(bugzilla, auto_cc_arches, modified_repo, observer):
390-
bugzilla.update(self.bugno, BugUpdate(depends_on=ListChange.adding(*deps)))
400+
with _naming(f"adding dependencies to bug {self.bugno} for {self}"):
401+
bugzilla.update(self.bugno, BugUpdate(depends_on=ListChange.adding(*deps)))
391402
self.obsolete_bugs(bugzilla)
392403
return self.bugno
393404
self.file_missing_deps(bugzilla, auto_cc_arches, modified_repo, observer)
@@ -403,18 +414,19 @@ def file_bug(
403414
f" {pkg.versioned_atom.cpvstr}: no change for {days_old} days, since {modified:%Y-%m-%d}"
404415
)
405416

406-
self.bugno = bugzilla.create(
407-
NewBug.arch_request(
408-
self.category,
409-
self.package_list,
410-
maintainers=tuple(self.node_maintainers),
411-
cc_arches=self.should_cc_arches(auto_cc_arches),
412-
summary=self.bug_summary,
413-
description="\n".join(description).strip(),
414-
depends_on=tuple({dep.bugno for dep in self.edges}),
415-
blocks=tuple(block_bugs),
417+
with _naming(f"filing bug for {self}"):
418+
self.bugno = bugzilla.create(
419+
NewBug.arch_request(
420+
self.category,
421+
self.package_list,
422+
maintainers=tuple(self.node_maintainers),
423+
cc_arches=self.should_cc_arches(auto_cc_arches),
424+
summary=self.bug_summary,
425+
description="\n".join(description).strip(),
426+
depends_on=tuple({dep.bugno for dep in self.edges}),
427+
blocks=tuple(block_bugs),
428+
)
416429
)
417-
)
418430
if observer is not None:
419431
observer(self)
420432
self.obsolete_bugs(bugzilla)
@@ -438,7 +450,8 @@ def obsolete_bugs(self, bugzilla: Bugzilla):
438450
if not self.obsoletes:
439451
return
440452
assert self.bugno is not None
441-
bugzilla.update(sorted(self.obsoletes), BugUpdate.obsoleted_by(self.bugno))
453+
with _naming(f"obsoleting bugs by {self.bugno} for {self}"):
454+
bugzilla.update(sorted(self.obsoletes), BugUpdate.obsoleted_by(self.bugno))
442455
self.obsoletes.clear() # don't repeat the update if visited again
443456

444457

@@ -901,16 +914,26 @@ def load_graph_toml(self, toml_file: str):
901914
def merge_nodes(self, nodes: tuple[GraphNode, ...]) -> GraphNode:
902915
categories = {node.category for node in nodes}
903916
assert len(categories) == 1, f"refusing to merge nodes of mixed categories: {categories}"
917+
bugnos = {node.bugno for node in nodes if node.bugno is not None}
918+
if len(bugnos) > 1:
919+
bugs.error(
920+
"cannot merge nodes matched to different existing bugs: "
921+
+ ", ".join(f"https://bugs.gentoo.org/{bugno}" for bugno in sorted(bugnos)),
922+
status=3,
923+
)
904924
self.nodes.difference_update(nodes)
905925
is_start = bool(self.starting_nodes.intersection(nodes))
906926
self.starting_nodes.difference_update(nodes)
907927
new_node = GraphNode(
908-
list(chain.from_iterable(n.pkgs for n in nodes)), category=categories.pop()
928+
list(chain.from_iterable(n.pkgs for n in nodes)),
929+
category=categories.pop(),
930+
bugno=next(iter(bugnos), None),
909931
)
910932

911933
for node in nodes:
912934
new_node.edges.update(node.edges.difference(nodes))
913935
new_node.obsoletes.update(node.obsoletes) # inherit pending obsoletions
936+
new_node.obsoletes.discard(new_node.bugno) # never obsolete our own bug
914937

915938
for node in self.nodes:
916939
if node.edges.intersection(nodes):
@@ -922,6 +945,30 @@ def merge_nodes(self, nodes: tuple[GraphNode, ...]) -> GraphNode:
922945
self.starting_nodes.add(new_node)
923946
return new_node
924947

948+
def merge_matched_bugs(self):
949+
"""Merge the nodes which matched the same existing bug."""
950+
shared: dict[int, list[GraphNode]] = defaultdict(list)
951+
for node in self.nodes:
952+
if node.bugno is not None:
953+
shared[node.bugno].append(node)
954+
955+
for bugno, nodes in sorted(shared.items()):
956+
if len(nodes) > 1:
957+
self.out.write(
958+
f"Merging {len(nodes)} nodes matched to bug {bugno}: ",
959+
", ".join(map(str, nodes)),
960+
)
961+
self.merge_nodes(tuple(nodes))
962+
963+
# a bug still in use can't also be resolved as obsolete
964+
in_use = {node.bugno for node in self.nodes if node.bugno is not None}
965+
for node in self.nodes:
966+
for bugno in sorted(node.obsoletes.intersection(in_use)):
967+
self.out.warn(
968+
f"not obsoleting bug {bugno}, it is the bug of another node in the graph"
969+
)
970+
node.obsoletes.difference_update(in_use)
971+
925972
@staticmethod
926973
def _find_cycles(nodes: tuple[GraphNode, ...], stack: list[GraphNode]) -> tuple[GraphNode, ...]:
927974
node = stack[-1]
@@ -1123,6 +1170,7 @@ def main(options, out: Formatter, err: Formatter):
11231170
out.flush()
11241171
has_output = True
11251172

1173+
d.merge_matched_bugs()
11261174
if not d.merge_stabilization_groups(out, err):
11271175
out.write(out.fg("red"), "Aborted", out.reset)
11281176
return 1

‎tests/scripts/test_pkgdev_bugs.py‎

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from types import SimpleNamespace
55

66
import pytest
7-
from pkgcore.bugzilla import BugCategory
7+
from pkgcore.bugzilla import BugCategory, BugzillaError
88
from pkgcore.ebuild.atom import atom
99

1010
from pkgdev.scripts import pkgdev_bugs as bugs
@@ -142,7 +142,9 @@ def mk_graph(repo, category=BugCategory.STABLEREQ):
142142
# build a DependencyGraph without running its heavy __init__
143143
graph = bugs.DependencyGraph.__new__(bugs.DependencyGraph)
144144
graph.options = SimpleNamespace(repo=repo, search_repo=repo, category=category)
145-
graph.out = SimpleNamespace(write=lambda *a, **k: None, flush=lambda: None)
145+
graph.out = SimpleNamespace(
146+
write=lambda *a, **k: None, warn=lambda *a, **k: None, flush=lambda: None
147+
)
146148
graph.err = graph.out
147149
graph.nodes = set()
148150
graph.starting_nodes = set()
@@ -509,3 +511,90 @@ def test_merge_nodes_keeps_obsoletes(self, repo, monkeypatch):
509511

510512
merged = graph.merge_nodes((first, second))
511513
assert merged.obsoletes == {100, 101}
514+
515+
516+
class TestSharedExistingBug:
517+
"""Several nodes matching one existing bug must not make it depend on itself."""
518+
519+
def mk_shared(self, repo):
520+
"""A dependency path whose ends matched the same existing bug."""
521+
pkg = max(repo.itermatch(atom("=cat/u-0")))
522+
graph = mk_graph(repo)
523+
top = bugs.GraphNode((), bugno=597)
524+
middle = bugs.GraphNode(((pkg, {"*"}),))
525+
bottom = bugs.GraphNode((), bugno=597)
526+
top.edges.add(middle)
527+
middle.edges.add(bottom)
528+
graph.nodes.update((top, middle, bottom))
529+
graph.starting_nodes.add(top)
530+
return graph, middle
531+
532+
def test_shared_bug_is_merged(self, repo):
533+
mk_repo(repo)
534+
graph, middle = self.mk_shared(repo)
535+
graph.merge_matched_bugs()
536+
537+
merged = [node for node in graph.nodes if node.bugno == 597]
538+
assert len(merged) == 1, "nodes sharing a bug must become one node"
539+
# the path collapsed into a cycle, which merge_cycles then folds together
540+
assert merged[0].edges == {middle}
541+
assert middle.edges == {merged[0]}
542+
graph.merge_cycles()
543+
assert len(graph.nodes) == 1
544+
assert next(iter(graph.nodes)).bugno == 597, "the existing bug must be kept"
545+
546+
def test_no_self_dependency_is_filed(self, repo, bugzilla_cassette):
547+
mk_repo(repo)
548+
graph, _ = self.mk_shared(repo)
549+
graph.merge_matched_bugs()
550+
graph.merge_cycles()
551+
552+
node = next(iter(graph.nodes))
553+
node.file_bug(bugzilla_cassette.client(api_key="API"), frozenset(), (), None)
554+
# nothing to file: the existing bug covers the whole cycle, and 597 was
555+
# never made to depend on a bug which depends on it
556+
assert bugzilla_cassette.calls == []
557+
558+
def test_bug_in_use_is_not_obsoleted(self, repo):
559+
mk_repo(repo)
560+
graph, middle = self.mk_shared(repo)
561+
middle.obsoletes.add(597)
562+
graph.merge_matched_bugs()
563+
564+
assert not any(node.obsoletes for node in graph.nodes), (
565+
"a bug kept as another node's bug must not also be resolved obsolete"
566+
)
567+
568+
def test_merging_different_bugs_errors(self, repo, capsys):
569+
mk_repo(repo)
570+
graph = mk_graph(repo)
571+
nodes = (bugs.GraphNode((), bugno=1), bugs.GraphNode((), bugno=2))
572+
graph.nodes.update(nodes)
573+
with pytest.raises(SystemExit) as excinfo:
574+
graph.merge_nodes(nodes)
575+
assert excinfo.value.code == 3
576+
assert "different existing bugs" in capsys.readouterr().err
577+
578+
579+
class TestFilingErrorContext:
580+
"""A rejected change must name what was being filed."""
581+
582+
def test_creation_error_names_the_node(self, repo, bugzilla_cassette):
583+
mk_repo(repo)
584+
pkg = max(repo.itermatch(atom("=cat/u-0")))
585+
node = bugs.GraphNode(((pkg, {"*"}),))
586+
bugzilla_cassette.expect_error(116, "circular dependency")
587+
with pytest.raises(BugzillaError) as excinfo:
588+
node.file_bug(bugzilla_cassette.client(api_key="API"), frozenset(), (), None)
589+
assert "filing bug for =cat/u-0" in str(excinfo.value)
590+
assert "circular dependency" in str(excinfo.value)
591+
592+
def test_dependency_update_error_names_the_bug(self, repo, bugzilla_cassette):
593+
mk_repo(repo)
594+
pkg = max(repo.itermatch(atom("=cat/u-0")))
595+
node = bugs.GraphNode((), bugno=200)
596+
node.edges.add(bugs.GraphNode(((pkg, {"*"}),)))
597+
bugzilla_cassette.expect_created(300).expect_error(116, "circular dependency")
598+
with pytest.raises(BugzillaError) as excinfo:
599+
node.file_bug(bugzilla_cassette.client(api_key="API"), frozenset(), (), None)
600+
assert "adding dependencies to bug 200" in str(excinfo.value)

0 commit comments

Comments
 (0)