Panic Error with .GetPage in Hugo 0.45

So with Hugo 0.45 the following throws a panic.

{{ $sections := (.Site.GetPage "section" .Section).Sections.ByWeight }}

Any advice?

CC/ @bep @vassudanagunta

Error message from the console

Building sites … ERROR 2018/07/23 14:20:48 Failed to render "_default/list.html": runtime error: invalid memory address or nil pointer dereference
ERROR 2018/07/23 14:20:48 Stack Trace:
goroutine 153 [running]:
github.com/gohugoio/hugo/hugolib.stackTrace(0x4b0, 0xef9837, 0x17)
	/home/alex/work/src/github.com/gohugoio/hugo/hugolib/page.go:280 +0x76
github.com/gohugoio/hugo/hugolib.(*Site).renderForLayouts.func1(0xc420b1f8a0, 0xc42046a300)
	/home/alex/work/src/github.com/gohugoio/hugo/hugolib/site.go:1742 +0x135
panic(0xdc2b00, 0x1591eb0)
	/usr/local/go/src/runtime/panic.go:502 +0x229
text/template.errRecover(0xc420b1f790)
	/usr/local/go/src/text/template/exec.go:137 +0x1d4
panic(0xdc2b00, 0x1591eb0)
	/usr/local/go/src/runtime/panic.go:502 +0x229
github.com/gohugoio/hugo/hugolib.(*Page).Sections(0x0, 0x0, 0x0, 0x0)
	/home/alex/work/src/github.com/gohugoio/hugo/hugolib/site_sections.go:139 +0x5
reflect.Value.call(0xea6320, 0xc420f8f220, 0x11693, 0xeabf9a, 0x4, 0x15d3cf0, 0x0, 0x0, 0xea0b40, 0x1, ...)
	/usr/local/go/src/reflect/value.go:447 +0x969
reflect.Value.Call(0xea6320, 0xc420f8f220, 0x11693, 0x15d3cf0, 0x0, 0x0, 0xc420fdaa60, 0x13, 0x45)
	/usr/local/go/src/reflect/value.go:308 +0xa4
text/template.(*state).evalCall(0xc420b1f710, 0xea8760, 0xc420716c80, 0x16, 0xea6320, 0xc420f8f220, 0x11693, 0x10883c0, 0xc4206daa00, 0xc42088ec27, ...)
	/usr/local/go/src/text/temp
Total in 5143 ms
Error: Error building site: logged 2 error(s)

The underlying GetPage is returning nil. It’s always a good idea to with that first call and not chain the next func unconditionally.

Try:

{{ with .Site.GetPage (printf "/%s" .Section) }}
    {{ $sections := .Sections.ByWeight }}
    ..
{{ end }}
1 Like

Thank you so much @kaushalmodi

The above fixed the panic error.

Now I need to update a Tips and Tricks I posted a while back.

It is also a bug and should be fixed (create a GH issue).

Note that the above should be the same as the simpler:

{{ with .FirstSection }}

{{ end }}

1 Like

I didn’t know about .FirstSection but in my case it throws the following error in every section:

ERROR 2018/07/23 17:35:51 Error while rendering "section" in "blog/": template: _default/list.html:44:8: executing "_default/list.html" at <.FirstSection>: can't evaluate field FirstSection in type *hugolib.PageOutput

GitHub Issue is here: Hugo 0.45 Panic with .Get.Page · Issue #4989 · gohugoio/hugo · GitHub

I must have dreamt about the FirstSection … I was pretty sure I added that at some point …

LOL! Apparently it was only a dream. (happens all the time over here also).

I’m pretty sure it’s in one of my branches …

Ok. It wasn’t a dream after all.

You got me searching and I found the commit for you:

But was this merged?

For some strange reason this commit doesn’t show up in issue #4891 even though you referenced it.

Also I posted the commit link in the related GitHub issue.

It does show:

When commits contain “Fixes #NNNN”, that’s how it shows up.

I missed it. Thanks @kaushalmodi

No problem. I started investigating this, and turns out that commit was reverted later. So .FirstSection doesn’t yet exist.

@kaushalmodi .FirstSection was added just now with commit 016dd4a69a765061bb3da8490d3cac6ec47a91eb so you should re-open that issue in Hugo Docs to document it.

And since I run Hugo Dev locally, I have already tested this new variable and I am happy to report that the following works great:

{{ with .FirstSection }}
…
{{ end }}

@bep Thanks! You are the best! :tada:

I didn’t connect this until I saw @kaushalmodi 's issue comment … I was 100% sure I had added this at some point …

1 Like

The following commit fixed this regression

There should be a bug fix release coming.

My old code works again in Hugo.

1 Like