readthedocs / sphinx-autoapi

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

Sphinx Viewcode and autoapi conflict: IndexError list index out of range if child class file (.py) is smaller than parent class file. #422

Closed hansonmcoombs closed 3 months ago

hansonmcoombs commented 7 months ago

This is a port of an issue from Sphinx (https://github.com/sphinx-doc/sphinx/issues/12026)

The original issue:

Describe the bug

building sphinx the build fails if the child class is smaller than the parent class file.

Error message

../sphinx/ext/viewcode.py", line 289, in collect_pages
    + lines[start])
      ~~~~~^^^^^^^
IndexError: list index out of range

Adding blank lines to the child class (at the end of the file) resolves the problem....

It seems like it must be a rather minimal problem, but it took some time to find the issue

How to Reproduce

The easiest way to reproduce the error is to clone the demo repo I have created: It has full environment information (conda) as well. https://github.com/hansonmcoombs/sphinx_bug_test

Environment Information

Platform:              linux; (Linux-5.15.0-94-generic-x86_64-with-glibc2.35)
Python version:        3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 20:50:58) [GCC 12.3.0])
Python implementation: CPython
Sphinx version:        7.2.6
Docutils version:      0.20.1
Jinja2 version:        3.1.3
Pygments version:      2.17.2

Sphinx extensions

['sphinx.ext.todo', # since found irrelevant  to the issue
 'sphinx.ext.autodoc', # since found irrelevant  to the issue
'sphinx.ext.viewcode',
 'sphinx.ext.autosummary']

Additional learning:

I have slimmed down the example repo:

It appears to be a conflict between autoapi and viewcode extensions, with the following settings:

I have pushed a commit with the slimmed down example.

aborecki commented 3 months ago

Changing order of extensions helped in my case: I got error when: extensions = [ "sphinx.ext.viewcode", "autoapi.extension",

When viewcode is after autoapi, everything works fine: extensions = [ "autoapi.extension", "sphinx.ext.viewcode",

hansonmcoombs commented 3 months ago

Even more strangely I just re-ran my original example (with the same env) and I was un able to replicate the error. Obviously something changed (I rebuilt my machine with ubuntu 24.04), but search me... Closing the issue.