Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out of bounds array access in dataclasses.py:218 #24

Open
segfaultxavi opened this issue Feb 5, 2025 · 4 comments
Open

Out of bounds array access in dataclasses.py:218 #24

segfaultxavi opened this issue Feb 5, 2025 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@segfaultxavi
Copy link

In dataclasses.py line 218 there's the Source class with the contents method.
This method returns the self.line line from the self.parent.root.files.filepath(self.parent.root_module.id) file.

In my case, the line number is beyond the number of lines of the file and the handler crashes.

Upon inspection I found that the file in self.parent.root.files just contains a bunch of imports, but self refers to one of the imported files.
self.line and self.character really refer to the file in self.file_name, which is NOT the file retrieved from self.parent.root.files.

self.file_name is not in the list returned by self.parent.root.files.

Thanks!

@pawamoy pawamoy added the bug Something isn't working label Feb 5, 2025
@pawamoy
Copy link
Member

pawamoy commented Feb 5, 2025

Thank you for the report @segfaultxavi! It would be awesome if you could provide a minimal reproduction example too 🙏 😊

@pawamoy pawamoy self-assigned this Feb 5, 2025
@segfaultxavi
Copy link
Author

I am trying to build the minimal example. Meanwhile, found something else that I will report in another issue.

@segfaultxavi
Copy link
Author

This sample reproduces the problem for me:
bug3.zip

console output:

$ mkdocs build
INFO    -  DeprecationWarning: Setting a fallback anchor function is deprecated and will be removed in a future release.
             File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings/plugin.py", line 190, in on_config
               autorefs.get_fallback_anchor = self.handlers.get_anchors
             File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocs_autorefs/plugin.py", line 130, in get_fallback_anchor
               warn(
INFO    -  Cleaning site directory
INFO    -  Building documentation to directory: /home/xartigas/repos/symbol/k/docs/site
INFO    -  griffe_typedoc: json generated at /tmp/tmpoddksjz8
ERROR   -  Error reading page 'index.md': list index out of range
Traceback (most recent call last):
  File "/home/xartigas/.local/bin/mkdocs", line 8, in <module>
    sys.exit(cli())
             ^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/click/core.py", line 1161, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/click/core.py", line 1082, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/click/core.py", line 1697, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/click/core.py", line 1443, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/click/core.py", line 788, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocs/__main__.py", line 288, in build_command
    build.build(cfg, dirty=not clean)
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocs/commands/build.py", line 310, in build
    _populate_page(file.page, config, files, dirty)
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocs/commands/build.py", line 167, in _populate_page
    page.render(config, files)
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocs/structure/pages.py", line 285, in render
    self.content = md.convert(self.markdown)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/markdown/core.py", line 357, in convert
    root = self.parser.parseDocument(self.lines).getroot()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/markdown/blockparser.py", line 117, in parseDocument
    self.parseChunk(self.root, '\n'.join(lines))
  File "/home/xartigas/.local/lib/python3.12/site-packages/markdown/blockparser.py", line 136, in parseChunk
    self.parseBlocks(parent, text.split('\n\n'))
  File "/home/xartigas/.local/lib/python3.12/site-packages/markdown/blockparser.py", line 158, in parseBlocks
    if processor.run(parent, blocks) is not False:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings/extension.py", line 130, in run
    html, handler, data = self._process_block(identifier, block, heading_level)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings/extension.py", line 230, in _process_block
    rendered = handler.render(data, options)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/handler.py", line 188, in render
    return template.render(
           ^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/jinja2/environment.py", line 1295, in render
    self.environment.handle_exception()
  File "/home/xartigas/.local/lib/python3.12/site-packages/jinja2/environment.py", line 942, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/dispatch.html.jinja", line 1, in top-level template code
    {% extends "_base/dispatch.html.jinja" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/_base/dispatch.html.jinja", line 32, in top-level template code
    {% include "class.html.jinja" with context %}
^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/class.html.jinja", line 1, in top-level template code
    {% extends "_base/class.html.jinja" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/_base/class.html.jinja", line 52, in top-level template code
    {% block contents scoped %}
^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/_base/class.html.jinja", line 58, in block 'contents'
    {% block children scoped %}
^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/_base/class.html.jinja", line 61, in block 'children'
    {% include "children.html.jinja" with context %}
^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/children.html.jinja", line 1, in top-level template code
    {% extends "_base/children.html.jinja" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/_base/children.html.jinja", line 18, in top-level template code
    {% include "dispatch.html.jinja" with context %}
^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/dispatch.html.jinja", line 1, in top-level template code
    {% extends "_base/dispatch.html.jinja" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/_base/dispatch.html.jinja", line 48, in top-level template code
    {% include "method.html.jinja" with context %}
^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/method.html.jinja", line 1, in top-level template code
    {% extends "_base/method.html.jinja" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/_base/method.html.jinja", line 52, in top-level template code
    {% block contents scoped %}

^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/_base/method.html.jinja", line 58, in block 'contents'

    {% block signatures scoped %}
^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/_base/method.html.jinja", line 60, in block 'signatures'
    {% include "dispatch.html.jinja" with context %}
^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/dispatch.html.jinja", line 1, in top-level template code
    {% extends "_base/dispatch.html.jinja" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/_base/dispatch.html.jinja", line 52, in top-level template code
    {% include "call_signature.html.jinja" with context %}
^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/call_signature.html.jinja", line 1, in top-level template code
    {% extends "_base/call_signature.html.jinja" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/mkdocstrings_handlers/typescript/templates/material/_base/call_signature.html.jinja", line 7, in top-level template code
    {{ call_signature.source_contents }}
^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/jinja2/environment.py", line 490, in getattr
    return getattr(obj, attribute)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/griffe_typedoc/dataclasses.py", line 338, in source_contents
    return "\n".join(source.contents for source in self.sources).rstrip().removesuffix("{")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/griffe_typedoc/dataclasses.py", line 338, in <genexpr>
    return "\n".join(source.contents for source in self.sources).rstrip().removesuffix("{")
                     ^^^^^^^^^^^^^^^
  File "/home/xartigas/.local/lib/python3.12/site-packages/griffe_typedoc/dataclasses.py", line 223, in contents
    return l[self.line - 1]
           ~^^^^^^^^^^^^^^^
IndexError: list index out of range

requirements.txt:

mkdocs==1.6.1
mkdocstrings==0.27.0
mkdocstrings-typescript @ file:///home/xartigas/repos/pawamoy-insiders/mkdocstrings-typescript

For comparison, this other project is very similar, same source files, but without imports, and this one works fine:
bug2.zip

@pawamoy
Copy link
Member

pawamoy commented Feb 5, 2025

Awesome, thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants