Repository navigation
Description
Describe the bug
Using the singlehtml builder to generate index.html from the example below produces invalid internal HTML anchors having multiple # characters in them with certain combinations of glossaries and toctree inclusions. In the reproducer below, there is an index.rst file with a toctree that includes two glossary files in separate documents, each with their own glossary item definition. An equivalent behavior can be observed also by using .. include to include the files directly. The build command is sphinx-build -b singlehtml . _build/foo and the HTML file is tested with html5validator --root _build/foo --show-warnings. Issue #13037 might be related.
How to Reproduce
conf.py:
project = 'Glossary Test'
extensions = []
index.rst:
Main Documentation
==================
Welcome to the documentation.
.. toctree::
:maxdepth: 2
ch1
glossary/term1
glossary/term2
with the glossary files being trivial items:
.. glossary::
API
Lorem Ipsum.
The generate document contains:
<a class="reference internal" href="#document-glossary/term1#term-API"><span class="xref std std-term">API</span></a>
Environment Information
Platform: linux; (Linux-6.14.0-37-generic-x86_64-with-glibc2.39)
Python version: 3.12.3 (main, Nov 6 2025, 13:44:16) [GCC 13.3.0])
Python implementation: CPython
Sphinx version: 9.0.4
Docutils version: 0.21.2
Jinja2 version: 3.1.4
Pygments version: 2.19.2
Sphinx extensions
NoneAdditional context
Some build warnings are generated, but they should not affect the outcome of the test
writing... /home/nicola/test-sphinx/index.rst:6: WARNING: toctree contains reference to document 'glossary/term1' that doesn't have a title: no link will be generated [toc.no_title]
/home/nicola/test-sphinx/index.rst:6: WARNING: toctree contains reference to document 'glossary/term2' that doesn't have a title: no link will be generated [toc.no_title]
/home/nicola/test-sphinx/index.rst:6: WARNING: toctree contains reference to document 'glossary/term1' that doesn't have a title: no link will be generated [toc.no_title]
/home/nicola/test-sphinx/index.rst:6: WARNING: toctree contains reference to document 'glossary/term2' that doesn't have a title: no link will be generated [toc.no_title]
Activity
etrian-dev commentedon Dec 27, 2025
Also found #10106 which seems similar
jayaddison commentedon Jan 4, 2026
Thanks for the bugreport @etrian-dev - do you know whether this is a regression? (e.g. did a
singlehtmlbuild for this project produce functioning hyperlinks in a previous version of Sphinx?)I'll try to take a look at this within the next week or so.
etrian-dev commentedon Jan 4, 2026
Hi. This was noticed as part of the build of a large document that uses
singlehtml, and does not show issues with Sphinx8.1.3. After upgrading to8.2.3we noticed the problem, but I was able to confirm that it persisted even after that; while I didn't try8.1.3on this example, I would expect it to produce correct output, so I would say it is a regression that occurred (or was uncovered by other modifications such as the ones in the aforementioned issue) between8.1.3and8.2.3.etrian-dev commentedon Jan 4, 2026
I ran with
8.1.3in a venv and indeed I see no invalid anchors (the prefix using the document name is absent)jayaddison commentedon Jan 4, 2026
Great, thank you - it seems highly likely then that this is indeed a result of PR #13037. I think we should revert that and ideally also add test coverage that would prevent it from passing if it were re-applied. I would still like to deprecate/remove that method, but we'll need to figure out the remaining cases it manages.
jayaddison commentedon Jan 4, 2026
@etrian-dev I'm having some difficulty replicating the problem - I've created a project with an index, two glossary files in a
glossary/subdirectory, and have copied your provided example contents into those -- but I don't end up with duplicated#characters in thesinglehtmloutput, either from v9.0.4 or from v9.1.0 - I'll keep trying, though.nvetrini commentedon Jan 4, 2026
@jayaddison Interesting; what's the content of the
hrefs you are getting?jayaddison commentedon Jan 4, 2026
@etrian-dev Without titles in the two glossary documents, I do not find any
reference internalHREFs at all. If I add titles, then I get output like:etrian-dev commentedon Jan 4, 2026
@jayaddison Could you try using this content for
ch1.rst?I get this output with
9.0.4:and
for
8.1.3.jayaddison commentedon Jan 4, 2026
Perfect, thank you @etrian-dev - with that I'm able to get a build with duplicate anchor fragments:
That should be enough information to construct the test coverage from.
jayaddison commentedon Jan 5, 2026
So, what
fix_refurisseems to be used for is that for hyperlinks that would otherwise feature both a document-path (e.g.#glossary/term1) and also a term-label (e.g.#term-API), it drops the former --SingleHTMLBuilderis the sole internal user of this. It works because in the resulting HTML, glossary terms -- regardless of the use offix_refuris-- are emitted solely with the term-label as element ID. The HTML looks like:So: as a longer-term fix, I'd suggest:
A) Including a document-path part in the glossary term HTML element idenfitiers - e.g. expand them to
<dt id="glossary/term1#term-API">...</dt>AND
B) URLencode the term-label when they are referenced by HREF - e.g.
<a href="#document-glossary/term2%23term-API">...</a>I've tested that proposed approach in Firefox 140.5.0 and it results in functional glossary-term hyperlinks within a single HTML page. As a small bonus, it enables disambiguation of hyperlinks when the same glossary term (e.g.
API) is defined in multiple document glossaries.jayaddison commentedon Jan 5, 2026
@etrian-dev please could you confirm whether #14241 resolves the problem for you?
jayaddison commentedon Jan 6, 2026
@picnixz @tk0miya I've caused Sphinx to produce some invalid HTML
anchorshyperlink fragments -- particularly forglossaryterms in single-page HTML output (singlehtml) -- due to one of my previous pull requests, #13037.I have created a revert of that pull request, and, with confirmation from the bugreporter, I hope that this will resolve the immediate problem.
However: I think that the
fix_refurismethod oversimplifies (reduces the accuracy of) some of the affected glossary reference URIs insinglehtmlmode, as a side-effect of the code that de-duplicates the#symbol. I would like to make the glossary term IDs and HREFs both valid and also more precise.If possible, could you review the suggestions in my earlier comment and provide any feedback/guidance? Thank you.
Edit: clarification.
etrian-dev commentedon Jan 6, 2026
It does fix the issue, thank you.
jayaddison commentedon Jan 6, 2026
Ok, great - thank you for confirming that, @etrian-dev
python3-sphinx: backport patch to fix singlehtml URIs
pin sphinx==8.1.3 for singlehtml anchor issues
jdillard commentedon Aug 13, 2026
@jayaddison, here are my thoughts on the future design:
I think the overall direction makes sense:
singlehtmlshould produce globally unique, document-namespaced target IDs and generate references to those IDs directly.I'm not sure using a literal
#inside the ID and%23in thehrefis the best syntax. Browsers can handle that encoding, but it adds complexity for URL tooling, CSS selectors, JavaScript, validators, and inventories. I think a document-namespaced ID using an ordinary separator,--, would be simpler, for example:objects.inventries should also use the same target scheme in inventory URIs so that Intersphinx links resolve to the new IDs. #10106 already tracks incorrect inventory URIs generated bysinglehtmland should be considered as part of that work.One compatibility consideration, which may already be accounted for given the Sphinx 10 target, is that existing
singlehtmloutput exposes bare IDs such asterm-API. Bookmarks, external URLs, and manually authored links may therefore point toindex.html#term-API. Replacing that ID would break those links, so the release notes should identify the changedsinglehtmlfragment-ID scheme and note that existing external deep links may require updating.One separate note: namespaced IDs make it possible to link precisely to duplicate glossary terms, but they do not define how a
:term:reference naming onlyAPIshould choose among definitions in different documents. That remains a separate standard-domain reference-resolution question, and #1399 is likely the best tracking issue for it.Tests: simplify refuris testroot
Tests: cleanup: remove misleading comment
jayaddison commentedon Sep 22, 2026
@jdillard I meant to reply to this earlier, but had not found time. Although initially I felt that using
#within the IDs was a clever solution -- I think you're correct that it could become finnicky/complex in combination with other things like CSS selectors.I'm not sure I love the
--notation either, though. Are there other (ideally reserved) symbols that we could use (:or.perhaps?).jdillard commentedon Sep 28, 2026
I ended up making an extension for this (that is usable now, but the logic is ultimately meant to be upstreamed): https://github.com/jdillard/sphinx-singlehtml-anchors
@jayaddison I agree, it seemed like the best option at first glance, but after exploring the alternatives I think
:would work just fine. I tried to outline how I arrived at--in Separator choice and alternatives, but I can change the separator to:and update the logic/docs/etc and push a new release (AFAIK I am the only one using this extension as I haven't marketed it. I would also still consider it abetastate at this point).What are your thoughts?
edit: I went ahead and made a PR to change it: jdillard/sphinx-singlehtml-anchors#1
etrian-dev commentedon Sep 29, 2026
@jdillard That looks like exactly what I need. I will try to test it on our manual with a recent Sphinx, as the duplicate IDs are the reason why we stick to Sphinx 8.1.3
etrian-dev commentedon Sep 29, 2026
It seems to work in our usecase, using Sphinx 9.1.0 and various extensions
jayaddison commentedon Sep 29, 2026
Thanks @jdillard - I've left a review (approval) on your PR to update the separator symbol. Also, that's reassuring to hear @etrian-dev that the extension resolves the problem.
jdillard commentedon Sep 29, 2026
@jayaddison Thanks for the review!
@etrian-dev Thanks for testing! I just released v0.2.0 which changes the separator to
:. I don't expect any other changes that would effect the public behavior of the extension, but could be possible in the course of testing/etc in trying to get it upstreamed. Once it is upstreamed, is should be a smooth process to uninstall the extension an retain the same behavior.