Skip to content

Commit 1b08b66

Browse files
committed
Add warning for multiple toc parents found
1 parent 909b161 commit 1b08b66

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

CHANGES.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ Bugs fixed
9999
* #12796: Enable parallel reading if requested,
100100
even if there are fewer than 6 documents.
101101
Patch by Matthias Geier.
102-
* #12888: Ensure deterministic resolution of global toctree in parallel builds
103-
when document is included in multiple toctrees by choosing lexicographically
104-
greatest parent document.
102+
* #12888: Add a warning when document is included in multiple toctrees
103+
and ensure deterministic resolution of global toctree in parallel builds
104+
by choosing lexicographically greatest parent document.
105105
Patch by A. Rafey Khan
106106

107107

sphinx/environment/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ def check_consistency(self) -> None:
758758
continue
759759
logger.warning(__("document isn't included in any toctree"),
760760
location=docname)
761+
_check_toc_parents(self.toctree_includes)
761762

762763
# call check-consistency for all extensions
763764
for domain in self.domains.values():
@@ -788,3 +789,22 @@ def _traverse_toctree(
788789
if sub_docname not in traversed:
789790
yield sub_parent, sub_docname
790791
traversed.add(sub_docname)
792+
793+
794+
def _check_toc_parents(toctree_includes: dict[str, list[str]]):
795+
toc_parents: dict[str, list[str]] = {}
796+
for parent, children in toctree_includes.items():
797+
for child in children:
798+
toc_parents.setdefault(child, []).append(parent)
799+
800+
for doc, parents in sorted(toc_parents.items()):
801+
if len(parents) > 1:
802+
logger.warning(
803+
__("document is referenced in multiple toctrees: %s, "
804+
"selecting: %s <- %s"),
805+
parents,
806+
max(parents),
807+
doc,
808+
location=doc, type='toc',
809+
subtype='multiple_toc_parents'
810+
)

tests/test_builders/test_build.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ def test_numbered_circular_toctree(app):
100100
) in warnings
101101

102102

103+
@pytest.mark.sphinx('text', testroot='toctree-multiple-parents')
104+
def test_multiple_parents_toctree(app):
105+
app.build(force_all=True)
106+
warnings = app.warning.getvalue()
107+
assert (
108+
"document is referenced in multiple toctrees: ['bar', 'baz'], selecting: baz <- qux"
109+
) in warnings
110+
111+
103112
@pytest.mark.usefixtures('_http_teapot')
104113
@pytest.mark.sphinx('dummy', testroot='images')
105114
def test_image_glob(app):

0 commit comments

Comments
 (0)