Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ Features added
Bugs fixed
----------

* #13383: Emit a warning for duplicate internal hyperlink declarations.
Patch by James Addison.

Testing
-------
7 changes: 7 additions & 0 deletions sphinx/domains/std/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,13 @@ def process_doc(
env.doc2path(self.labels[name][0]),
location=node,
)
elif name in self.anonlabels:
logger.warning(
__('duplicate label %s, other instance in %s'),
name,
env.doc2path(self.anonlabels[name][0]),
location=node,
)
self.anonlabels[name] = docname, labelid
if node.tagname == 'section':
title = cast('nodes.title', node[0])
Expand Down
9 changes: 9 additions & 0 deletions tests/roots/test-warnings/hyperlinks.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
:orphan:

.. _ambiguous_hyperlink:

This hyperlink target is declared with the same name in two files.

.. include:: includes/shared-hyperlinks.rst

This is one of two places where the shared-hyperlinks file is included.
5 changes: 5 additions & 0 deletions tests/roots/test-warnings/includes/shared-hyperlinks.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:orphan:

.. _ambiguous_shared_hyperlink:

This hyperlink target is declared once and is included by multiple documents.
Comment on lines +3 to +5
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AA-Turner @picnixz is there a way to determine a unique declaration identifier/path -- e.g. where a hyperlink was defined, instead of where it is used?

When reporting warnings about duplicated anonymous hyperlinks, it'd be nice to be able to indicate clearly that the duplication originates from multiple includes of a common document, instead of weird/confusing warnings about the hyperlinks appearing in multiple documents.

8 changes: 8 additions & 0 deletions tests/roots/test-warnings/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ This used to crash: :option:`&option`
.. missing citation

[missing]_ citation

.. _ambiguous_hyperlink:

This hyperlink target is declared with the same name in two files.

.. include:: includes/shared-hyperlinks.rst

This is one of two places where the shared-hyperlinks file is included.
1 change: 1 addition & 0 deletions tests/test_builders/test_build_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
{root}/index.rst:\\d+: WARNING: Encoding 'utf-8-sig' used for reading included \
file '{root}/wrongenc.inc' seems to be wrong, try giving an :encoding: option \\[docutils\\]
{root}/index.rst:\\d+: WARNING: invalid single index entry '' \\[index\\]
{root}/index.rst:\\d+: WARNING: duplicate label ambiguous_hyperlink, other instance in {root}/hyperlinks.rst
{root}/index.rst:\\d+: WARNING: image file not readable: foo.png \\[image.not_readable\\]
{root}/index.rst:\\d+: WARNING: download file not readable: {root}/nonexisting.png \\[download.not_readable\\]
{root}/undecodable.rst:\\d+: WARNING: undecodable source characters, replacing \
Expand Down
Loading