sphinx.ext.autosectionlabel and myst_parser not compatible
Describe the bug
context
When I use sphinx.ext.autosectionlabel and myst_parser for an rst project it triggers an exception:
See also https://github.com/sphinx-doc/sphinx/issues/10902.
sphinx.ext.autosectionlabel configuration:
autosectionlabel_prefix_document = True
autosectionlabel_maxdepth = 2
expectation I expected no exceptions to be raised.
bug
Extension error (sphinx.ext.autosectionlabel):
Handler <function register_sections_as_label at 0x7fcf255cf910> for event 'doctree-read' threw an exception (exception: 'NoneType' object has no attribute 'parent')
problem
When sphinx.ext.autosectionlabel is trying to find node depth it looks for cur_node.parent == node.document. However, with myst_parser the parent node of the document where this is included is not the parent. Instead the top-level parent is set to None. This is what triggers the exception. If the plan was to have None as the top-level parent, then https://github.com/sphinx-doc/sphinx/issues/10902 must be fixed. However, I believe myst_parser should attempt to set parent node to the document where the markdown is included.
Reproduce the bug
Enable sphinx.ext.autosectionlabel and use myst_parser
.. include:: a_markdown_file.md
:parser: myst_parser.sphinx_
List your environment
Ubuntu 20.04 in WSL2 on Windows 11 Python 3.10.6 docutils 0.17.1 myst_parser 0.18.0
Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.
If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).
Welcome to the EBP community! :tada:
I also encountered this bug when autosectionlabel_maxdepth is not None, and I write the 'including markdown file in rst file' syntax:
.. include:: ../../../CHANGELOG.md
:parser: myst_parser.sphinx_
When the parser is parsing this rst file, the error raises.
To avoid it, I set autosectionlabel_maxdepth=None. Or remove the include syntax.