Skip to content

Incorrect HTML anchors generated by singlehtml builder #14221

Open

Description

@etrian-dev

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

None

Additional 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

  1. etrian-dev commented on Dec 27, 2025

    @etrian-dev
    Author

    Also found #10106 which seems similar

  2. jayaddison commented on Jan 4, 2026

    @jayaddison
    Contributor

    Thanks for the bugreport @etrian-dev - do you know whether this is a regression? (e.g. did a singlehtml build 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.

  3. etrian-dev commented on Jan 4, 2026

    @etrian-dev
    Author

    Hi. This was noticed as part of the build of a large document that uses singlehtml, and does not show issues with Sphinx 8.1.3. After upgrading to 8.2.3 we noticed the problem, but I was able to confirm that it persisted even after that; while I didn't try 8.1.3 on 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) between 8.1.3 and 8.2.3.

  4. etrian-dev commented on Jan 4, 2026

    @etrian-dev
    Author

    I ran with 8.1.3 in a venv and indeed I see no invalid anchors (the prefix using the document name is absent)

  5. jayaddison commented on Jan 4, 2026

    @jayaddison
    Contributor

    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.

  6. jayaddison commented on Jan 4, 2026

    @jayaddison
    Contributor

    @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 the singlehtml output, either from v9.0.4 or from v9.1.0 - I'll keep trying, though.

  7. nvetrini commented on Jan 4, 2026

    @nvetrini

    @jayaddison Interesting; what's the content of the hrefs you are getting?

  8. jayaddison commented on Jan 4, 2026

    @jayaddison
    Contributor

    @etrian-dev Without titles in the two glossary documents, I do not find any reference internal HREFs at all. If I add titles, then I get output like:

    _build/index.html:<li class="toctree-l1"><a class="reference internal" href="#document-glossary/term1">Title</a></li>
    _build/index.html:<li class="toctree-l1"><a class="reference internal" href="#document-glossary/term2">Title</a></li>
    
  9. etrian-dev commented on Jan 4, 2026

    @etrian-dev
    Author
    Chapter One
    ===========
    
    This chapter discusses :term:`API` design and :term:`REST` principles.
    
    We use :term:`API` throughout our application to communicate between
    different services.
    
    Section 1.1
    -----------
    
    More details about :term:`REST` architecture here.
    
    The :term:`API` should follow RESTful conventions.

    @jayaddison Could you try using this content for ch1.rst?

    I get this output with 9.0.4:

    <p>This chapter discusses <a class="reference internal" href="#document-glossary/term1#term-API"><span class="xref std std-term">API</span></a> design and <a class="reference internal" href="#document-glossary/term2#term-REST"><span class="xref std std-term">REST</span></a> principles.</p>

    and

    <p>This chapter discusses <a class="reference internal" href="#term-API"><span class="xref std std-term">API</span></a> design and <a class="reference internal" href="#term-REST"><span class="xref std std-term">REST</span></a> principles.</p>

    for 8.1.3.

  10. jayaddison commented on Jan 4, 2026

    @jayaddison
    Contributor

    Perfect, thank you @etrian-dev - with that I'm able to get a build with duplicate anchor fragments:

    _build/index.html:<p>This chapter discusses <a class="reference internal" href="#document-glossary/term2#term-API"><span class="xref std std-term">API</span></a> design and <span class="xref std std-term">REST</span> principles.</p>
    _build/index.html:<p>We use <a class="reference internal" href="#document-glossary/term2#term-API"><span class="xref std std-term">API</span></a> throughout our application to communicate between
    _build/index.html:<p>The <a class="reference internal" href="#document-glossary/term2#term-API"><span class="xref std std-term">API</span></a> should follow RESTful conventions.</p>
    

    That should be enough information to construct the test coverage from.

  11. jayaddison commented on Jan 5, 2026

    @jayaddison
    Contributor

    So, what fix_refuris seems 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 -- SingleHTMLBuilder is the sole internal user of this. It works because in the resulting HTML, glossary terms -- regardless of the use of fix_refuris -- are emitted solely with the term-label as element ID. The HTML looks like:

    <dt id="term-API">...</dt>

    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.

  12. jayaddison commented on Jan 5, 2026

    @jayaddison
    Contributor

    @etrian-dev please could you confirm whether #14241 resolves the problem for you?

  13. jayaddison commented on Jan 6, 2026

    @jayaddison
    Contributor

    @picnixz @tk0miya I've caused Sphinx to produce some invalid HTML anchors hyperlink fragments -- particularly for glossary terms 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_refuris method oversimplifies (reduces the accuracy of) some of the affected glossary reference URIs in singlehtml mode, 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.

  14. etrian-dev commented on Jan 6, 2026

    @etrian-dev
    Author

    @etrian-dev please could you confirm whether #14241 resolves the problem for you?

    It does fix the issue, thank you.

  15. jayaddison commented on Jan 6, 2026

    @jayaddison
    Contributor

    Ok, great - thank you for confirming that, @etrian-dev

  16. jdillard commented on Aug 13, 2026

    @jdillard
    SponsorMember

    @jayaddison, here are my thoughts on the future design:

    I think the overall direction makes sense: singlehtml should 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 %23 in the href is 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:

    <dt id="document-glossary/term1--term-API">...</dt>
    <a href="#document-glossary/term1--term-API">API</a>

    objects.inv entries 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 by singlehtml and 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 singlehtml output exposes bare IDs such as term-API. Bookmarks, external URLs, and manually authored links may therefore point to index.html#term-API. Replacing that ID would break those links, so the release notes should identify the changed singlehtml fragment-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 only API should 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.

  17. jayaddison commented on Sep 22, 2026

    @jayaddison
    Contributor

    @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?).

  18. jdillard commented on Sep 28, 2026

    @jdillard
    SponsorMember

    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

    I'm not sure I love the -- notation either, though. Are there other (ideally reserved) symbols that we could use (: or . perhaps?).

    @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 a beta state at this point).

    What are your thoughts?

    edit: I went ahead and made a PR to change it: jdillard/sphinx-singlehtml-anchors#1

  19. etrian-dev commented on Sep 29, 2026

    @etrian-dev
    Author

    @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

  20. etrian-dev commented on Sep 29, 2026

    @etrian-dev
    Author

    It seems to work in our usecase, using Sphinx 9.1.0 and various extensions

  21. jayaddison commented on Sep 29, 2026

    @jayaddison
    Contributor

    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.

  22. jdillard commented on Sep 29, 2026

    @jdillard
    SponsorMember

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions