Skip to content

Gettext builder generates empty msgid entries from toctrees without caption #118

Description

@hwhsu1231

Describe the bug

context

When a Sphinx project using sphinx-external-toc includes subtrees without a caption in _toc.yml, and the gettext builder is used to generate .pot files.

expectation

No empty msgid entries should appear in the generated .pot files.

bug

The insert_toctrees() function in events.py unconditionally sets rawcaption on every toctree node via toctree.caption or "":

# TODO this wasn't in the original code,
# but alabaster theme intermittently raised `KeyError('rawcaption')`
subnode["rawcaption"] = toctree.caption or ""

When no caption is present, this assigns an empty string "", which Sphinx's extract_original_messages() then extracts as an empty msgid:

#: path/to/file.md:1
msgid ""
msgstr ""

These empty entries cause msgcat and other gettext tools to report "duplicate message definition" errors.

problem

Sphinx's toctree.extract_original_messages() checks 'rawcaption' in self — it only cares whether the key exists, not whether its value is non-empty.

    def extract_original_messages(self) -> list[str]:
        messages: list[str] = []

        # toctree entries
        messages.extend(self.get('rawentries', []))

        # :caption: option
        if 'rawcaption' in self:
            messages.append(self['rawcaption'])
        return messages

The workaround toctree.caption or "" (originally added for alabaster theme compatibility) forces the key to always exist, causing gettext to extract empty strings.

Reproduce the bug

  1. Create a _toc.yml with nested subtrees that lack a caption:

    root: intro
    subtrees:
    - entries:
      - file: doc1
        subtrees:
        - entries:
          - file: subfolder/page
  2. Run the gettext builder:

    sphinx-build -b gettext . _build/gettext
  3. Inspect the generated .pot files — they will contain empty msgid entries.

List your environment

  • sphinx-external-toc==1.1.0

Additional context

This issue was originally reported as sphinx-doc/sphinx#13168

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions