diff --git a/CHANGES.rst b/CHANGES.rst index c257b3b11b1..6d4af124ebd 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -16,5 +16,8 @@ Features added Bugs fixed ---------- +* #13383: Emit a warning for duplicate internal hyperlink declarations. + Patch by James Addison. + Testing ------- diff --git a/sphinx/domains/std/__init__.py b/sphinx/domains/std/__init__.py index e123ce85786..ba9fea13e37 100644 --- a/sphinx/domains/std/__init__.py +++ b/sphinx/domains/std/__init__.py @@ -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]) diff --git a/tests/roots/test-warnings/hyperlinks.rst b/tests/roots/test-warnings/hyperlinks.rst new file mode 100644 index 00000000000..0a789e10eb9 --- /dev/null +++ b/tests/roots/test-warnings/hyperlinks.rst @@ -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. diff --git a/tests/roots/test-warnings/includes/shared-hyperlinks.rst b/tests/roots/test-warnings/includes/shared-hyperlinks.rst new file mode 100644 index 00000000000..98f554c4dea --- /dev/null +++ b/tests/roots/test-warnings/includes/shared-hyperlinks.rst @@ -0,0 +1,5 @@ +:orphan: + +.. _ambiguous_shared_hyperlink: + +This hyperlink target is declared once and is included by multiple documents. diff --git a/tests/roots/test-warnings/index.rst b/tests/roots/test-warnings/index.rst index ac52d9076ec..24f05a6b0de 100644 --- a/tests/roots/test-warnings/index.rst +++ b/tests/roots/test-warnings/index.rst @@ -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. diff --git a/tests/test_builders/test_build_warnings.py b/tests/test_builders/test_build_warnings.py index 65e359ad666..78e10820421 100644 --- a/tests/test_builders/test_build_warnings.py +++ b/tests/test_builders/test_build_warnings.py @@ -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 \