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

Provide backtrace to the point in template where a function gets called over "nil" #4710

Closed
kaushalmodi opened this issue May 8, 2018 · 4 comments

Comments

@kaushalmodi
Copy link
Contributor

kaushalmodi commented May 8, 2018

Reference: https://discourse.gohugo.io/t/runtime-error-invalid-memory-address-or-nil-pointer-dereference/11881/12?u=kaushalmodi

An example problematic template snippet:

{{ (.Site.GetPage "taxonomyTerm" "categories" "foo").Permalink }}

Now if that (.Site.GetPage "taxonomyTerm" "categories" "foo") returns nil, hugo would only print something like:

Building sites … ERROR 2018/05/08 14:55:46 Failed to render "theme/index.html": runtime error: invalid memory address or nil pointer dereference

That error does not mean that the referenced index.html has the problematic statement.. the error could be in any of the nested partials in that file. So the debug becomes time-consuming as you comment/uncomment the template files in halves to narrow down to the actual buggy line in the template.

@regisphilibert
Copy link
Member

regisphilibert commented Aug 30, 2018

This indeed would shorten many debugging situations. I've got a tiny tear in my eyes now when I see this dreaded error.

@bep bep modified the milestones: v0.50, v0.51 Oct 18, 2018
@bep bep modified the milestones: v0.51, v0.53 Nov 8, 2018
@bep bep modified the milestones: v0.53, v0.54 Dec 6, 2018
@bep bep modified the milestones: v0.54, v0.55 Dec 26, 2018
@bep bep modified the milestones: v0.55, v0.56 Feb 1, 2019
@bep bep modified the milestones: v0.56, v0.57 Jun 14, 2019
@bep bep modified the milestones: v0.57, v0.58 Jul 31, 2019
@bep bep modified the milestones: v0.58, v0.59 Aug 13, 2019
@bep bep modified the milestones: v0.59, v0.60 Sep 6, 2019
@bep bep modified the milestones: v0.60, v0.61 Oct 21, 2019
@bep bep modified the milestones: v0.61, v0.62, v0.63 Nov 25, 2019
@bep bep removed this from the v0.63 milestone Dec 11, 2019
@bep bep modified the milestones: v0.66, v0.67 Mar 2, 2020
@bep bep modified the milestones: v0.67, v0.68 Mar 9, 2020
@bep bep modified the milestones: v0.68, v0.69 Mar 20, 2020
@bep bep modified the milestones: v0.69, v0.70 Apr 8, 2020
@bep bep modified the milestones: v0.70, v0.71 Apr 28, 2020
@bep bep modified the milestones: v0.71, v0.72 May 18, 2020
@bep bep modified the milestones: v0.72, v0.73 May 31, 2020
@bep bep modified the milestones: v0.73, v0.74 Jun 23, 2020
@bep bep modified the milestones: v0.74, v0.75 Jul 13, 2020
@bep bep modified the milestones: v0.75, v0.76 Sep 14, 2020
@bep bep modified the milestones: v0.76, v0.77 Oct 6, 2020
@bep bep modified the milestones: v0.77, v0.78 Oct 30, 2020
@moorereason
Copy link
Contributor

I'm unable to reproduce this issue now. Can anyone confirm?

I have an old v0.54.0 binary, and it shows better results even back then. Not sure what version of Go I used to compile it, though (PS - maybe we should output the Go runtime version in our hugo version output).

Here's the changes I tested against the gohugoioTheme:

diff --git a/themes/gohugoioTheme/layouts/partials/home-page-sections/features-icons.html b/themes/gohugoioTheme/layouts/partials/home-page-sections/features-icons.html
index a7733acdc..239a5a57e 100644
--- a/themes/gohugoioTheme/layouts/partials/home-page-sections/features-icons.html
+++ b/themes/gohugoioTheme/layouts/partials/home-page-sections/features-icons.html
@@ -5,6 +5,7 @@
       {{ range $i, $e := $features  }}
         {{ $features_count := $e | len }}

+        {{ ($.Site.GetPage "foo").Permalink }}
         <div class="w-100{{ if and (eq $i $features_count) (ne (modBool $features_count 2) true) }} w-70-ns {{ else }} w-50-ns {{ end }}ph5-l pv5-l nested-copy-line-height">
           <!-- NOTE:  "if eq $i $features_count" makes the last item a bit wider for balance. If we use an even number of items, we'll want to remove this -->
           <div class="flex-l flex-wrap justify-between">

Output:

Error: Error building site: failed to render pages: render of "home" failed: "/hugoDocs/themes/gohugoioTheme/layouts/index.html:8:33": execute of template failed: template
: index.html:8:8: executing "main" at <partial "home-page-sections/features-icons" .>: error calling partial: "/hugoDocs/themes/gohugoioTheme/layouts/partials/home-page-se
ctions/features-icons.html:8:33": execute of template failed: template: partials/home-page-sections/features-icons.html:8:33: executing "partials/home-page-sections/features-icons.html" at <($.Site.GetPage "
foo").Permalink>: error calling Permalink: runtime error: invalid memory address or nil pointer dereference

@bep
Copy link
Member

bep commented Nov 28, 2020

@moorereason this is fixed.

@bep bep closed this as completed Nov 28, 2020
@github-actions
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 Jan 26, 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

4 participants