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

Pagination is broken with "invalid memory or nil pointer dereference" in 0.123.0-DEV #11949

Closed
TiGR opened this issue Jan 31, 2024 · 15 comments
Closed

Comments

@TiGR
Copy link

TiGR commented Jan 31, 2024

I get error like this:

ERROR render of "page" failed: execute of template failed: template: _internal/pagination.html:21:17: executing "_internal/pagination.html" at <$page.Paginator.TotalPages>: error calling TotalPages: runtime error: invalid memory address or nil pointer dereference

And somewhat related issue is error like this:

executing "main" at <$paginate.Pages>: error calling Pages: runtime error: invalid memory address or nil pointer dereference

in code like this:

{{ $paginate := .Paginate $sliceOfPages 10 }}
{{ range $index, $page := $paginate.Pages }}
    {{ $page.Render "list_item" }}
{{ end }}

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.123.0-DEV+extended linux/amd64 BuildDate=unknown

Does this issue reproduce with the latest release?

No, the latest stable release works well

@bep bep self-assigned this Jan 31, 2024
@bep bep added this to the v0.123.0 milestone Jan 31, 2024
@bep
Copy link
Member

bep commented Jan 31, 2024

Thanks for this, much appreciated.

I don't think "pagination is broken" is a generally valid statement (we have lots of tests). Do you have an example site that shows this problem?

@jmooring
Copy link
Member

jmooring commented Jan 31, 2024

@TiGR In which template does your code appear?

Or, more specifically, is the pagination code called from a list page (home, section, taxonomy, or term), or from a single page?

@TiGR
Copy link
Author

TiGR commented Jan 31, 2024

In general, the idea is that we have structure like /content/places/*/events/*/ and we're building a list of all places, but sorted by most recent event, and having each place only once. In order to achieve this we go through all events pages, sort them by some field. Then, as a final step we create a slice and then append all places pages in the order we need:

{{ $places := slice }}
{{ range $events }}
  {{/* in this example .place is an item of (site.GetPage "places").Pages list, that is a Page */}}
  {{ $places = $places | append .place }}
{{ end }}

And then we do the code I've mentioned above:

<div class="places">
  {{ $paginate := .Paginate $places 10 }}
  {{ range $index, $page := $paginate.Pages }}
    {{ $page.Render "list_item" }}
  {{ end }}
</div>
<div class="pagination__wrap">
  {{- template "_internal/pagination.html" . -}}
</div>

With that code I get errors on $paginate.Pages and on including pagination template.

@TiGR
Copy link
Author

TiGR commented Jan 31, 2024

Or, more specifically, is the pagination code called from a list page (home, section, taxonomy, or term), or from a single page?

It's a section page. But it's a tricky one, it's an empty sub-section with no pages inside (like content/news/events/_index.md), since we need to have it in a specific place of our website.

@jmooring
Copy link
Member

jmooring commented Jan 31, 2024

I am unable to reproduce the problem with this test site:

git clone --single-branch -b hugo-github-issue-11949 https://github.com/jmooring/hugo-testing hugo-github-issue-11949
cd hugo-github-issue-11949
hugo server

The only way I can reproduce the error (or something close to it) is to place the code (or call to the containing partial) in a single page template, which is not allowed. You can only paginate from list templates (home, section, taxonomy, or term).

When called from a single page template, the "invalid memory address or nil pointer dereference" error has been thrown since v0.54.1. Prior to that the error message was "error calling Paginate: Paginators not supported for pages of type "page". See #11953.

Your template code is in layouts/foo/list.html.

@jmooring
Copy link
Member

jmooring commented Jan 31, 2024

Please rebuild hugo from HEAD (6c3b6ba or later) and test again to see if the error message changes.

@TiGR
Copy link
Author

TiGR commented Feb 1, 2024

Well. That's weird. The message has indeed changed. Now I get:

ERROR Rebuild failed: render: failed to render pages: render of "page" failed: "...:25:33": execute of template failed at <.Paginate>: error calling Paginate: pagination not supported for pages of kind "page"

But the problem is, the page we're talking about is supposed to be a section according to the documentation, and everything worked fine on previous versions. I have the following structure:

content/
├── news/
│   ├── events/
│   │   └── _index.md
│   └── _index.md
└── _index.md

And the only place were I have this template rendered is in content/news/events/_index.md. Which is supposed to be a section, but the hugo complains it is not.

Also, moving events directory to content top level fixes the issue, but I need it where it is now and I can't get it why it is not considered a section.

@TiGR
Copy link
Author

TiGR commented Feb 1, 2024

Also, it might be important, we have a bunch of _index.xx.html translation files in content/news/events directory.

@bep
Copy link
Member

bep commented Feb 1, 2024

@TiGR the devil is in the detail here, I guess, which is not possible for me to determine without seeing the full source.

But one of the improvements in this next version is a simplified logic in this area, to prepare for other content sources than files. So, the logic around handling of duplicates may have changed.

So, if you have something ala:

content/
├── news/
│   ├── events/
│   │   └── _index.md
│   │   └── index.md
│   └── _index.md
└── _index.md

Then that may explain it, but again, it's hard for me to guess without seeing the source.

That said, I appreciate your bug reports about this. I'm confident that we needed to make these changes, but I also understood that it could create some breakage in the wild.

What I can do is to add some more details to the message so you can see what backing file is being used in the "non section".

@bep
Copy link
Member

bep commented Feb 1, 2024

Can you try this with my latest commit in 1891d5e

@TiGR
Copy link
Author

TiGR commented Feb 1, 2024

Well, that seems to be definitely related with #11958, as I get the following:

ERROR render of "page" failed: "…/layouts/news/events.html:25:33": execute of template failed: template: news/events.html:25:33: executing "main" at <.Paginate>: error calling Paginate: pagination not supported for this page: kind: "page", path: "/news/events/_index.fi", file: "…/content/news/events/_index.fi.html"
ERROR render of "page" failed: "…/layouts/news/events.html:25:33": execute of template failed: template: news/events.html:25:33: executing "main" at <.Paginate>: error calling Paginate: pagination not supported for this page: kind: "page", path: "/news/events/_index.de", file: "…/content/news/events/_index.de.html"
ERROR render of "page" failed: "…/layouts/news/events.html:25:33": execute of template failed: template: news/events.html:25:33: executing "main" at <.Paginate>: error calling Paginate: pagination not supported for this page: kind: "page", path: "/news/events/_index.es", file: "…/content/news/events/_index.es.html"
ERROR render of "page" failed: "…/layouts/news/events.html:25:33": execute of template failed: template: news/events.html:25:33: executing "main" at <.Paginate>: error calling Paginate: pagination not supported for this page: kind: "page", path: "/news/events/_index.it", file: "…/content/news/events/_index.it.html"
Built in 71556 ms
Error: error building site: render: failed to render pages: render of "page" failed: "…/layouts/news/events.html:25:33": execute of template failed: template: news/events.html:25:33: executing "main" at <.Paginate>: error calling Paginate: pagination not supported for this page: kind: "page", path: "/news/events/_index.fr", file: "…/content/news/events/_index.fr.html"

Let me try building the entire website (this might take some time)

@TiGR
Copy link
Author

TiGR commented Feb 1, 2024

Well, the entire website (without disabled languages) builds well.

@bep
Copy link
Member

bep commented Feb 1, 2024

I'm closing this, as this certainly is "another issue".

@bep bep closed this as completed Feb 1, 2024
@jmooring
Copy link
Member

jmooring commented Feb 1, 2024

I can reproduce the behavior if I erroneously add kind = "page" to the front matter of content/news/events/_index.md.

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants