Drafts vs. undefined values

Lately — this is not pinnable to a version number, it just stopped working, after August 12th — I am unable to use hugo server -D due to type casting errors:

hugo server:

❯ hugo server
Watching for changes in /home/patrick/github.com/davidsneighbour/kollitsch.dev/{archetypes,assets,content,data,documentation,i18n,layouts,package.json,postcss.tailwind.config.js,static,tailwind.config.js}
Watching for config changes in /home/patrick/github.com/davidsneighbour/kollitsch.dev/config/_default, /home/patrick/github.com/davidsneighbour/kollitsch.dev/config/development, /home/patrick/github.com/davidsneighbour/kollitsch.dev/go.mod
Start building sites … 
hugo v0.133.0-c9777473d1369f812d727a6c07dc57ad7be7bf62+extended linux/amd64 BuildDate=2024-08-17T19:57:41Z VendorInfo=gohugoio


                   |  EN   
-------------------+-------
  Pages            |  836  
  Paginator pages  |   92  
  Non-page files   |  193  
  Static files     |   67  
  Processed images | 1181  
  Aliases          |  231  
  Cleaned          |    0  

Built in 3609 ms
Environment: "development"
Serving pages from disk
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1) 
Press Ctrl+C to stop

hugo server -D:

❯ hugo server -D
Watching for changes in /home/patrick/github.com/davidsneighbour/kollitsch.dev/{archetypes,assets,content,data,documentation,i18n,layouts,package.json,postcss.tailwind.config.js,static,tailwind.config.js}
Watching for config changes in /home/patrick/github.com/davidsneighbour/kollitsch.dev/config/_default, /home/patrick/github.com/davidsneighbour/kollitsch.dev/config/development, /home/patrick/github.com/davidsneighbour/kollitsch.dev/go.mod
Start building sites … 
hugo v0.130.0-9b1b11c8a59a900458e9e460f197a44367c022ee+extended linux/amd64 BuildDate=2024-07-29T13:51:56Z VendorInfo=gohugoio

Built in 228783 ms
Error: error building site: render: failed to render pages: render of "home" failed: "/home/patrick/github.com/davidsneighbour/kollitsch.dev/layouts/index.html:27:22": execute of template failed: template: index.html:27:22: executing "main" at <partials.Include>: error calling Include: "/home/patrick/github.com/davidsneighbour/kollitsch.dev/layouts/partials/content/post.html:5:31": execute of template failed: template: partials/content/post.html:5:31: executing "partials/content/post.html" at <partials.IncludeCached>: error calling IncludeCached: "/home/patrick/.cache/hugo_cache/modules/filecache/modules/pkg/mod/github.com/davidsneighbour/hugo-modules/modules/functions@v1.2024.19/layouts/partials/func/getArticleClasses.html:2:23": execute of template failed: template: partials/func/getArticleClasses.html:2:23: executing "partials/func/getArticleClasses.html" at <.IsHome>: nil pointer evaluating interface {}.IsHome

The template in question is this:

{{- $classes := collections.Slice -}}
{{- if compare.Eq true .IsHome -}}{{/* <-- the error points here */}}
  {{- $classes = $classes | collections.Append "is-home" -}}
{{- end -}}
{{- if compare.Eq true .IsNode -}}
  {{- $classes = $classes | collections.Append "is-node" -}}
{{- end -}}
{{- if compare.Eq true .IsPage -}}
  {{- $classes = $classes | collections.Append "is-page" -}}
{{- end -}}
{{- if compare.Eq true .IsSection -}}
  {{- $classes = $classes | collections.Append (fmt.Printf "section section-%s" .Section ) -}}
{{- end -}}
{{- $classes = $classes | collections.Append (fmt.Printf "kind-%s" .Kind) -}}
{{- $classes = $classes | collections.Append (fmt.Printf "type-%s" .Type) -}}
{{- if compare.Eq true .Draft -}}
  {{- $classes = $classes | collections.Append "draft" -}}
{{- end -}}
{{- $classes = collections.Delimit $classes " " -}}
{{- return $classes -}}

and it worked before, I am pretty sure.

The only thing that changed is the version of Golang in my system (installed via Snap).

Could it be that something changed with the way Hugo works with typecasting? I can change the template by wrapping it with {{ with . }}, but then another template error takes over, this time complaining about another undefined value.

Error: error building site: render: failed to render pages: render of "home" failed: "/home/patrick/github.com/davidsneighbour/kollitsch.dev/layouts/index.html:27:22": execute of template failed: template: index.html:27:22: executing "main" at <partials.Include>: error calling Include: "/home/patrick/github.com/davidsneighbour/kollitsch.dev/layouts/partials/content/post.html:51:18": execute of template failed: template: partials/content/post.html:51:18: executing "partials/content/post.html" at <partials.Include>: error calling Include: "/home/patrick/github.com/davidsneighbour/hugo-modules/modules/hooks/layouts/partials/func/hook.html:37:24": execute of template failed: template: partials/func/hook.html:37:24: executing "partials/func/hook.html" at <partials.Include>: error calling Include: "/home/patrick/github.com/davidsneighbour/kollitsch.dev/layouts/partials/hooks/post-after.html:1:29": execute of template failed: template: partials/hooks/post-after.html:1:29: executing "partials/hooks/post-after.html" at <.context.Params.tags>: nil pointer evaluating interface {}.Params

I tried to minimize the frontmatter of the draft post, to no change. As soon as I have any post with draft: true in it’s frontmatter hugo server -D gives up.

Basically trying to use drafts results in my install having issues with undefined values that did not exist before.

My repo is a github.com/davidsneighbour/kollitsch.dev.

I’m not sure what you mean by … typecasting, but the example above indicates a page that is nil (e.g. not found), which is, I guess, a fairly common thing, and the with or if is the right medicine for that. There certainly may be a subtle site.GetPage change that used to find the page, but now doesn’t, but it’s impossible for me to tell from the above. And even with that, you should always write your templates a little defensive (unless you know that there will always be a value there).

Hmm. The error is not thrown in any connection with GetPage. Also, the two only drafts I am having in my repo are not new, they existed untouched for a while and hugo server -D worked.

The if started throwing the error on .IsHome. Only with works. Before this started the if worked well in that case. Also that layout is only reached when the page exists. I would expect errors before that, because the partial is called with a dictionary of stuff. The page exists. I add the page with the draft note, the error comes up. I remove the page with the draft note, it disappears. Removing the draft note removes the error. the frontmatter looks valid.

I started writing the post when I was searching for updates in Gohugo and ended up on release 0.130.0 where some changes happened on how to compare two values, but that version too does not work with drafts. It did (it’s the release before the image path changes, the draft existed back then and the error didn’t come up).

I am not a Go-programmer, so I don’t expect to know what happened, but it appears that Go(hugo) suddenly sees something in my bad coding that it ignored before. Probably better would have been a formulation along the lines of “Could it be that something changed with the way Hugo evaluates if a value exists.” I guess. Sorry about that.

The second error states, that .Params does not exist on a page object, and it seems to me that the page object itself exists, but not the .Params on it. Fixing that with with leads to the next error. I can fix all my errors with an extra wrapper. That doesn’t change that it worked magically before, now it doesn’t, and I can’t pin it to a Hugo version.

Maybe someone else runs into similar issues with if, I’ll keep an eye on it.

That was just an example, my main point is that it’s not possible for me to guess about this based on a template extract.

I “fixed” it with a with on the uppermost level of the partial “chain” and the error disappeared.

The blogpost is still shown at the place where it would have thrown the error before. This whole box in the following image is a partial that is loaded. hugo server throws the mentioned error without a with around it and adding a with stops ending up in errors (in partials that are loaded within partials) and shows the single post. I would have expected it to not being shown if there really would have been an error with the template logic down the partials.

Sometimes templates can be weird :frowning: It works… I guess I am looking for if’s now and see if they work better being wrapped in withs.