readthedocs / sphinx-autoapi

A new approach to API documentation in Sphinx.
https://sphinx-autoapi.readthedocs.io/
MIT License
415 stars 126 forks source link

Extension error: list index out of range #412

Closed mcflugen closed 4 months ago

mcflugen commented 7 months ago

👋 Hello!

When trying to build documentation for my project using sphinx-autoapi, I'm getting the following error,

Extension error (sphinx.environment.collectors.toctree):
Handler <bound method TocTreeCollector.process_doc of <sphinx.environment.collectors.toctree.TocTreeCollector object at 0x103570680>> for event 'doctree-read' threw an exception (exception: list index out of range)

After digging around a bit, it appears the problem may lie in NestedParse when it removes a heading. Although the title node is removed with del node[0][0], perhaps the number of nodes is not decremented somewhere, which causes sphinx to try to iterate beyond the end of the list?

Anyway, below is a minimal reproducible example that produces the error (for me, anyway).

# file: docs/conf.py
extensions = ["autoapi.extension"]
autoapi_dirs = ['../foobar']
# file: foobar/foo.py
"""
This is a heading
=================
"""

Running the following generates the above error,

sphinx-build -b html docs/ _build/html

If I either remove the heading in foobar/foo.py or remove the del node[0][0] line from NestedParse, the error goes away and the docs build successfully.

I'm using sphinx-autoapi v3.0.0 and sphinx v7.2.6.

Any ideas? Am I missing something?

Thanks!

mcflugen commented 7 months ago

Popping the first child off the title_node's children seems to fix the issue—no ExtensionError and the heading is removed from the rendered html. If this seems like a reasonable fix to you, I'm happy to submit a pull request.

diff --git a/autoapi/directives.py b/autoapi/directives.py
index ee5e3ec..fb6caa2 100644
--- a/autoapi/directives.py
+++ b/autoapi/directives.py
@@ -61,7 +61,7 @@ class NestedParse(Directive):
         try:
             title_node = node[0][0]
             if isinstance(title_node, nodes.title):
-                del node[0][0]
+                title_node.children.pop(0)
         except IndexError:
             pass
         return node.children
AWhetter commented 7 months ago

That sounds like a good fix to me!

pbrod commented 4 months ago

I am experiencing the same error when building the documentation for karney package:

https://readthedocs.org/projects/karney/builds/23394851/

Any chance it can be fixed in the next release?

AWhetter commented 4 months ago

Fixed in c4db7eb14a6235750edeb151bb79754b9dd38e21

BrandonLiang commented 2 months ago

Hi, when is thew version planned to be released?