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

Social plugin: runtime error with generated files #7520

Closed
4 tasks done
thorstenpeter-sophos opened this issue Sep 12, 2024 · 5 comments
Closed
4 tasks done

Social plugin: runtime error with generated files #7520

thorstenpeter-sophos opened this issue Sep 12, 2024 · 5 comments
Labels
bug Issue reports a bug resolved Issue is resolved, yet unreleased if open

Comments

@thorstenpeter-sophos
Copy link
Contributor

Context

No response

Bug description

When using the Insiders version of the Social plugin in combination with another plugin that creates generated files, it raises an AttributeError exception.

Generated files were added with mkdocs 1.6 and don't have an abs_scr_path attribute.

Related links

Reproduction

9.5.34+insiders.4.53.12-social-virtual-files-error.zip

The foo plugin used in the reproduction is defined as follows:

from mkdocs.plugins import BasePlugin
from mkdocs.structure.files import File, Files, InclusionLevel

class FooPlugin(BasePlugin):

    def on_files(self, files: Files, **kwargs):
        return files.append(File.generated(
            config=kwargs.get("config"),
            src_uri="foo.md",
            content="",
            inclusion=InclusionLevel.NOT_IN_NAV,
        ))

Steps to reproduce

Run mkdocs build in the reproduction.

The build will fail with the following exception:

File "C:\git\mkdocs_material_insiders_minimal_reproduction\venv\Lib\site-packages\mkdocs\plugins.py", line 566, in run_event
  result = method(item, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^
File "C:\git\mkdocs_material_insiders_minimal_reproduction\venv\Lib\site-packages\material\plugins\social\plugin.py", line 180, in on_files
  if file.abs_src_path.startswith(_templates_dirpath()):
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'startswith'

Browser

No response

Before submitting

@thorstenpeter-sophos
Copy link
Contributor Author

This zip contains the foo plugin used in the reproduction.

foo-0.0.1-py3-none-any.zip

@squidfunk
Copy link
Owner

Thanks for reporting. Here's a hook to reproduce it without a plugin:

from mkdocs.structure.files import File, Files, InclusionLevel
from mkdocs.plugins import event_priority

@event_priority(100)
def on_files(files: Files, **kwargs):
    return files.append(File.generated(
        config=kwargs.get("config"),
        src_uri="foo.md",
        content="",
        inclusion=InclusionLevel.NOT_IN_NAV,
    ))

@squidfunk squidfunk added the bug Issue reports a bug label Sep 12, 2024
@squidfunk
Copy link
Owner

Judging from a comment in the File class, abs_src_path was made optional in MkDocs 1.6.0:

*   Since MkDocs 1.6 a file may alternatively be stored in memory - `content_string`/`content_bytes`.

        Then `src_dir` and `abs_src_path` will remain `None`. `content_bytes`/`content_string` need
        to be written to, or populated through the `content` argument in the constructor.

        But `src_uri` is still populated for such files as well! The virtual file pretends as if it
        originated from that path in the `docs` directory, and other values are derived.

Well, so this is essentially a change in behavior that I would definitely consider to be a breaking change. Yet, it's not MkDocs 2.0 but 1.6. This might impact a lot of plugins, since up to now abs_src_path was always available. Sigh. Let's fix this one and see whether other plugins suffer from the same problem as we go.

@squidfunk
Copy link
Owner

squidfunk commented Sep 12, 2024

Fixed in ee8d243f9.

@squidfunk squidfunk added the resolved Issue is resolved, yet unreleased if open label Sep 12, 2024
@squidfunk
Copy link
Owner

Released as part of 9.5.34+insiders-4.53.13.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue reports a bug resolved Issue is resolved, yet unreleased if open
Projects
None yet
Development

No branches or pull requests

2 participants