sphinx-doc / sphinx

The Sphinx documentation generator
https://www.sphinx-doc.org/
Other
6.49k stars 2.11k forks source link

sphinx.ext.autosectionlabel throws an extension error #11786

Closed arwedus closed 9 months ago

arwedus commented 10 months ago

Describe the bug

When building documentation with the following configuration:

# Configure autosectionlabel
autosectionlabel_prefix_document = True
autosectionlabel_maxdepth = 3

I get the following output:

INFO: Build completed successfully, 1 total action

Extension error (sphinx.ext.autosectionlabel):
Handler <function register_sections_as_label at 0x7fbd82aeb0d0> for event 'doctree-read' threw an exception (exception: 'NoneType' object has no attribute 'parent')

This happens independent of the autosectionlabel_maxdepth value.

When I remove the autosectionlabel_maxdepth configuration, the extension error disappears.

Build command:

sphinx-build -b html /workspace/tools /workspace/build/docs/tools/html -W --keep-going -E -a -w /workspace/build/docs/tools/sphinx_warnings.log -j 1

How to Reproduce

This happens in a closed source project, which has a very large number of documents, many of them contain duplicate section labels, and sections down to level 5, so I can't provide a link here.

The error occurs for rst documents that contain an include directive including MyST markdown, e.g.:

#############
quality_views
#############

.. include:: ../quality_views/README.md
   :parser: myst_parser.sphinx_

The only use case I see for that is including common text blocks that have been written in markdown into a larger rst document.

NOTE: We have several such includes of markdown snippets parsed by mystparser.sphinx in rst documents, and only two of them fail. I have not yet been able to find out why such an import fails sometimes and works the other times.

Environment Information

Platform:              linux; (Linux-6.2.0-36-generic-x86_64-with-glibc2.29)
Python version:        3.8.10 (default, May 26 2023, 14:05:08) 
[GCC 9.4.0])
Python implementation: CPython
Sphinx version:        5.3.0
Docutils version:      0.17.1
Jinja2 version:        3.1.2

Sphinx extensions

extensions = [
    "myst_parser",  # for md files
    "rtd_theme_global_navbar",  # use jscript based navBar
    "sphinx_copybutton",
    "sphinx_needs",
    "sphinx_preview",
    "sphinx_rtd_theme",
    "sphinx.ext.duration",
    "sphinx.ext.ifconfig",
    "sphinx.ext.intersphinx",
    "sphinx.ext.mathjax",
    "sphinx.ext.todo",
    "sphinxcontrib.plantuml",
    "warnings_filter",
    "sphinxcontrib.doxylink",
    "sphinx.ext.autodoc",
    "sphinx.ext.napoleon",
    "sphinx.ext.autosectionlabel",
    "sphinx_autodoc_typehints",
    "sphinxcontrib.asciinema",
    "sphinx.ext.mathjax",
    "sphinx.ext.graphviz",
    "sphinx_click",
    "sphinx.ext.viewcode",
    "sphinx_design",
]

Additional context

No response

arwedus commented 10 months ago

I assume that the function get_node_depth(node: Node) is problematic. If it gets an invalid document (where a section node has no parent), it causes the exception.

I extended the function to see which document is the culprit.

I thereby learned that the problem occurs when including a myst-markdown document into a restructuredtext document (I am not resposible for people doing that stuff :D). I'll try to compile an example.

@chrisjsewell : Maybe you know, as Myst-markdown developer, what the root cause could be?

picnixz commented 9 months ago

NOTE: We have several such includes of markdown snippets parsed by mystparser.sphinx in rst documents, and only two of them fail. I have not yet been able to find out why such an import fails sometimes and works the other times.

Can you remove them and check whether the error still occurs? I think it's probably related to myst rather than Sphinx itself, so for now I'll close it (and you should instead ask this on myst).

Feel free to reopen it if you can make it without additional extensions or if Chris sends you back here (but I prefer not having too many opened issues that may not be directly concern the code base).