Skip to content

Blog plugin failing with TypeError: can't compare offset-naive and offset-aware datetimes #7705

@perpil

Description

@perpil

Context

I have been successfully building my blog using the blog plugin. I'm not sure whether it was a recent insiders update, daylight savings or some filesystem change but I have not changed my mkdocs.yml file lately. When I attempt to build now or serve, I get this error:

INFO    -  Building documentation...
WARNING -  Debug mode is enabled for "social" plugin.
INFO    -  Cleaning site directory
Traceback (most recent call last):
  File "/opt/homebrew/bin/mkdocs", line 8, in <module>
    sys.exit(cli())
  File "/opt/homebrew/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/opt/homebrew/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/opt/homebrew/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/homebrew/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/homebrew/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/opt/homebrew/lib/python3.10/site-packages/mkdocs/__main__.py", line 272, in serve_command
    serve.serve(**kwargs)
  File "/opt/homebrew/lib/python3.10/site-packages/mkdocs/commands/serve.py", line 85, in serve
    builder(config)
  File "/opt/homebrew/lib/python3.10/site-packages/mkdocs/commands/serve.py", line 67, in builder
    build(config, serve_url=None if is_clean else serve_url, dirty=is_dirty)
  File "/opt/homebrew/lib/python3.10/site-packages/mkdocs/commands/build.py", line 292, in build
    files = config.plugins.on_files(files, config=config)
  File "/opt/homebrew/lib/python3.10/site-packages/mkdocs/plugins.py", line 593, in on_files
    return self.run_event('files', files, config=config)
  File "/opt/homebrew/lib/python3.10/site-packages/mkdocs/plugins.py", line 566, in run_event
    result = method(item, **kwargs)
  File "/Users/david/Documents/GitHub/mkdocs-material/material/plugins/blog/plugin.py", line 134, in on_files
    self.blog.posts = sorted(
TypeError: can't compare offset-naive and offset-aware datetimes

Bug description

The blog plugin appears to choke on the creation dates of some of the files on my filesystem. I'm able to workaround it by modifying line 134 in the blog plugin (material/plugins/blog/plugin.py) from:

        self.blog.posts = sorted(
            self._resolve_posts(files, config),
            key = lambda post: (
                post.config.pin,
                post.config.date.created
            ),
            reverse = True
        )

to (I also add import pytz at the top of the file)

        self.blog.posts = sorted(
            self._resolve_posts(files, config),
            key = lambda post: (
                post.config.pin,
                post.config.date.created.replace(tzinfo=pytz.utc)
            ),
            reverse = True
        )

Related links

Reproduction

I was not able to repro this, I tried several things but could not isolate it. The following is the markdown_extensions and plugins sections from the mkdocs.yml where it occurs.

markdown_extensions:
  - attr_list
  - md_in_html
  - pymdownx.emoji:
      emoji_index: !!python/name:material.extensions.emoji.twemoji
      emoji_generator: !!python/name:material.extensions.emoji.to_svg
  - admonition
  - pymdownx.details
  - pymdownx.snippets:
      url_download: true
      restrict_base_path: false
  - pymdownx.superfences
  - pymdownx.tasklist:
      custom_checkbox: true
  - tables
  - footnotes
plugins:
  - meta
  - blog
  - search
  - git-revision-date-localized:
      enable_creation_date: true
      type: timeago
      exclude:
        - blog/*
  - social:
      cards_layout_dir: layouts
      cards_layout: custom
      debug: true
      #debug_on_build: true
  - privacy:
      enabled: true
      cache: true
      cache_dir: .cache/plugins/privacy
      links_attr_map:
        target: _blank
      assets_exclude:
        - platform.twitter.com/widgets.js

And here is the output of my docs/blog directory:

❯ stat -f "%Sc %z %N" *
May 31 15:38:53 2024 4778 2023-year-in-review.md
Oct 28 14:44:33 2024 8815 a-better-readme.md
May 31 15:44:25 2024 7776 bifurcating-lambda-logs.md
May 31 15:44:19 2024 6025 cloudwatch-insights-tricks.md
Aug 14 16:04:14 2024 15969 coldstart-zero-part-duex.md
May 31 15:44:06 2024 9717 coldstart-zero.md
May 31 15:44:01 2024 4142 done-before-the-bass-drops.md
Jun 27 21:06:41 2024 9605 dynamodb-secretmanager.md
Oct 17 12:58:46 2024 8387 edge-metrics.md
May 31 15:44:38 2024 6188 lambda-env-variable-coldstarts.md
May 31 15:44:43 2024 5648 lambda-request-timeline.md
May 31 15:44:52 2024 15597 logging-for-scale.md
Oct 13 17:09:17 2024 988 migrating-to-cdk.md
May 31 15:44:58 2024 2162 mission.md
May 31 15:45:03 2024 23022 optimizing-lambda-coldstarts.md
May 31 15:45:07 2024 8994 stripped-down-coldstarts.md
May 31 15:45:11 2024 5769 the-global-query.md
May 31 15:45:16 2024 4372 totally-async-eventbridge.md
May 31 15:45:21 2024 7874 why-im-spicy-about-coldstarts.md

Steps to reproduce

I tried creating a minimal blog repro both with and without the git-revision-date-localized plugin but was unable to get this issue to reoccur, it only occurs in my main site directory.

plugins:
  - blog
  - git-revision-date-localized:
      enable_creation_date: true
      type: timeago
      exclude:
        - blog/*

I'm using mkdocs-material 9.5.44+insiders.4.53.14

Browser

Firefox

Before submitting

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue reports a bugresolvedIssue is resolved, yet unreleased if open

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions