Skip to content

Commit

Permalink
Add warning for multiple toc parents found
Browse files Browse the repository at this point in the history
  • Loading branch information
khanxmetu committed Sep 19, 2024
1 parent 909b161 commit 5d40fc3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ Bugs fixed
* #12796: Enable parallel reading if requested,
even if there are fewer than 6 documents.
Patch by Matthias Geier.
* #12888: Ensure deterministic resolution of global toctree in parallel builds
when document is included in multiple toctrees by choosing lexicographically
greatest parent document.
* #12888: Add a warning when document is included in multiple toctrees
and ensure deterministic resolution of global toctree in parallel builds
by choosing lexicographically greatest parent document.
Patch by A. Rafey Khan


Expand Down
17 changes: 17 additions & 0 deletions sphinx/environment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ def check_consistency(self) -> None:
continue
logger.warning(__("document isn't included in any toctree"),
location=docname)
_check_toc_parents(self.toctree_includes)

# call check-consistency for all extensions
for domain in self.domains.values():
Expand Down Expand Up @@ -788,3 +789,19 @@ def _traverse_toctree(
if sub_docname not in traversed:
yield sub_parent, sub_docname
traversed.add(sub_docname)


def _check_toc_parents(toctree_includes: dict[str, list[str]]):
toc_parents: dict[str, list[str]] = {}
for parent, children in toctree_includes.items():
for child in children:
toc_parents.setdefault(child, []).append(parent)

for doc, parents in sorted(toc_parents.items()):
if len(parents) > 1:
logger.warning(
__(f"document is referenced in multiple toctrees: {parents}, "
f"selecting: {max(parents)} <- {doc}"),
location=doc, type='toc',
subtype='multiple_toc_parents'
)
9 changes: 9 additions & 0 deletions tests/test_builders/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ def test_numbered_circular_toctree(app):
) in warnings


@pytest.mark.sphinx('text', testroot='toctree-multiple-parents')
def test_multiple_parents_toctree(app):
app.build(force_all=True)
warnings = app.warning.getvalue()
assert (
"document is referenced in multiple toctrees: ['bar', 'baz'], selecting: baz <- qux"
) in warnings


@pytest.mark.usefixtures('_http_teapot')
@pytest.mark.sphinx('dummy', testroot='images')
def test_image_glob(app):
Expand Down

0 comments on commit 5d40fc3

Please sign in to comment.