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

GetTerms: runtime error: invalid memory address or nil pointer dereference #7061

Closed
earthboundkid opened this issue Mar 16, 2020 · 7 comments
Milestone

Comments

@earthboundkid
Copy link
Contributor

Bug: If a page doesn't have its taxonomy set, .GetTerms panics.

Workaround (e.g. for topic):

{{ $topics := slice }}
{{ if .Param "topics" }}
  {{  $topic = .GetTerms "topics" }}
{{ end }}

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

Hugo Static Site Generator v0.67.1/extended darwin/amd64 BuildDate: unknown

Does this issue reproduce with the latest release?

Yes.

@earthboundkid earthboundkid changed the title page.GetTerms throws nil error if pages don't have taxonomy set If pages don't have taxonomy set, page.GetTerms throws error calling GetTerms: runtime error: invalid memory address or nil pointer dereference Mar 16, 2020
@bep bep added the Bug label Mar 16, 2020
@bep bep added this to the v0.67.2 milestone Mar 16, 2020
@bep
Copy link
Member

bep commented Mar 16, 2020

I tried to create a failing test for this, but failed ... So there must be something I don't understand.

@earthboundkid
Copy link
Contributor Author

Hmm, it definitely happens on my site, but I am having trouble reproducing it as a Hugo test. Maybe it's because I'm using non-standard taxonomies?

@earthboundkid
Copy link
Contributor Author

If you checkout spotlightpa/poor-richard@f89ff87#diff-5725f8ca9fd37c1438a052e56a003075 and change

{{ $topic := "" }}
{{ if .Param "topics" }}
  {{  $topic = index (.GetTerms "topics") 0 }}
{{ end }}

to

{{ $topic := index (.GetTerms "topics" | default slice) 0 }}

You get:


ERROR 2020/03/16 16:03:04 Render of "404" failed: execute of template failed: template: 404.html:56:5: executing "404.html" at <partial "ld-json.html" .>: error calling partial: execute of template failed: template: partials/ld-json.html:17:14: executing "partials/ld-json.html" at <partialCached "page-params.html" .Page .RelPermalink>: error calling partialCached: "/Users/cjohnson/src/github/poor-richard/layouts/partials/page-params.html:11:20": execute of template failed: template: partials/page-params.html:11:20: executing "partials/page-params.html" at <.GetTerms>: error calling GetTerms: runtime error: invalid memory address or nil pointer dereference

@bep bep modified the milestones: v0.67.2, v0.68 Mar 16, 2020
@bep bep changed the title If pages don't have taxonomy set, page.GetTerms throws error calling GetTerms: runtime error: invalid memory address or nil pointer dereference GetTerms: runtime error: invalid memory address or nil pointer dereference Mar 16, 2020
@earthboundkid
Copy link
Contributor Author

earthboundkid commented Mar 17, 2020

I think I've figured out how to reproduce it. The 404.html layout needs to be defined and reference .GetTerms.

@earthboundkid
Copy link
Contributor Author

earthboundkid commented Mar 17, 2020

diff --git a/hugolib/taxonomy_test.go b/hugolib/taxonomy_test.go
index f8dca6a1..52ad6a48 100644
--- a/hugolib/taxonomy_test.go
+++ b/hugolib/taxonomy_test.go
@@ -320,7 +320,7 @@ categories: ["This is Cool", "And new" ]
 ---
 
 Content.
-		
+
 		`)
 	}
 
@@ -535,7 +535,7 @@ Funny:|/p2/|`)
 func TestTaxonomiesListPages(t *testing.T) {
 	b := newTestSitesBuilder(t)
 	b.WithTemplates("_default/list.html", `
-	
+
 {{ template "print-taxo" "categories.cats" }}
 {{ template "print-taxo" "categories.funny" }}
 
@@ -570,7 +570,7 @@ categories: ["cats"]
 	b.Build(BuildCfg{})
 
 	b.AssertFileContent("public/index.html", `
-    
+
 Len categories.cats: 2
 categories.cats:|/blog/|
 categories.cats:|/|
@@ -625,7 +625,9 @@ Category Paginator {{ range $categories.Paginator.Pages }}{{ .RelPermalink }}|{{
 Cats Paginator {{ range $cats.Paginator.Pages }}{{ .RelPermalink }}|{{ end }}:END
 
 `)
-
+	b.WithTemplatesAdded("404.html", `
+404 Terms: {{ range .GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END
+	`)
 	b.Build(BuildCfg{})
 
 	cat := b.GetPage("categories")
@@ -647,7 +649,7 @@ Home Terms: /categories/dogs/|/categories/gorillas/|:END
 Cats Paginator /section/p1/|/section/|:END
 Category Paginator /categories/birds/|/categories/cats/|/categories/dogs/|/categories/funny/|/categories/gorillas/|:END
 `)
-
+	b.AssertFileContent("public/404.html", "xxx")
 	b.AssertFileContent("public/categories/funny/index.xml", `<link>http://example.com/section/p1/</link>`)
 	b.AssertFileContent("public/categories/index.xml", `<link>http://example.com/categories/funny/</link>`)
 
$ go test . -run TestTaxonomiesPageCollections -v
=== RUN   TestTaxonomiesPageCollections
=== PAUSE TestTaxonomiesPageCollections
=== CONT  TestTaxonomiesPageCollections
github.com/gohugoio/hugo/hugolib.(*Site).renderForTemplate
        /tmp/hugo/hugolib/site.go:1667
github.com/gohugoio/hugo/hugolib.(*Site).renderAndWritePage
        /tmp/hugo/hugolib/site.go:1593
github.com/gohugoio/hugo/hugolib.(*Site).render404
        /tmp/hugo/hugolib/site_render.go:258
github.com/gohugoio/hugo/hugolib.(*Site).render
        /tmp/hugo/hugolib/site.go:1167
github.com/gohugoio/hugo/hugolib.(*HugoSites).render
        /tmp/hugo/hugolib/hugo_sites_build.go:305
github.com/gohugoio/hugo/hugolib.(*HugoSites).Build.func4
        /tmp/hugo/hugolib/hugo_sites_build.go:133
runtime/trace.WithRegion
        /usr/local/Cellar/go/1.14/libexec/src/runtime/trace/annotation.go:137
github.com/gohugoio/hugo/hugolib.(*HugoSites).Build
        /tmp/hugo/hugolib/hugo_sites_build.go:135
github.com/gohugoio/hugo/hugolib.(*sitesBuilder).build
        /tmp/hugo/hugolib/testhelpers_test.go:598
github.com/gohugoio/hugo/hugolib.(*sitesBuilder).Build
        /tmp/hugo/hugolib/testhelpers_test.go:560
github.com/gohugoio/hugo/hugolib.TestTaxonomiesPageCollections
        /tmp/hugo/hugolib/taxonomy_test.go:631
testing.tRunner
        /usr/local/Cellar/go/1.14/libexec/src/testing/testing.go:992
runtime.goexit
        /usr/local/Cellar/go/1.14/libexec/src/runtime/asm_amd64.s:1373
    TestTaxonomiesPageCollections: taxonomy_test.go:631: Build failed: render of "404" failed: "layouts/404.html:2:20": execute of template failed: template: 404.html:2:20: executing "404.html" at <.GetTerms>: error calling GetTerms: runtime error: invalid memory address or nil pointer dereference
--- FAIL: TestTaxonomiesPageCollections (0.01s)

@earthboundkid
Copy link
Contributor Author

In self = p.treeRef.key, treeRef is nil. I don't know enough about Hugo internals to know what this means, but seems like the quick fix is to say if nil, self = ""?

@bep bep modified the milestones: v0.68, v0.69 Mar 20, 2020
@bep bep removed the CantReproduce label Mar 20, 2020
@bep bep closed this as completed in 95f4921 Mar 20, 2020
pingswept added a commit to pingswept/me30-website that referenced this issue Jun 29, 2020
Rename breaks build because of gohugoio/hugo#7061. Need to figure out how to make a proper homepage.
@github-actions
Copy link

github-actions bot commented Feb 5, 2022

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 5, 2022
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

2 participants