Error when appending to a slice

I just updated my hugo_extended [to v0.121.2] and this line:

{{ $.Page.Store.Add "preload-list" (slice $medium.RelPermalink $src $Sizes) }}

Which worked before as intended, now occasions this message:

Error: error building site: "/home/drm/WEBSITE/content/docs/Love/meta.md:1:1":
"/home/drm/WEBSITE/layouts/_default/_markup/render-image.html:80:6":
execute of template failed: template: 
_default/_markup/render-image.html:80:6: executing "partial/defaultImage" at
<$.Page.Store.Add>: error calling Add: cannot append slice of string to slice of interface {}

This was setup with

{{.Page.Store.Set "preload-list" (slice (slice))}}
{{ $fuzzy := .FuzzyWordCount }}

before .content appears in the template.

I don’t think this was introduced/changed in v0.121.2. Which version were you running prior to upgrading?

I think you are seeing the effects of #11104 in v0.114.0. There are a couple of open issues related to that change:

As a general note, using collections.Append with a slice of slices can be tricky… it depends on what you’re starting with (i.e., an empty slice vs. a slice of slices as described in the documentation).

You could start with a slice of a string slice that contains one empty element, then append:

{{ $s := slice (slice "") }}
{{ $s = $s | append (slice "a" "b") }}
{{ $s = $s | append (slice "c" "d") }}

{{ $s }} → [[] [a b] [c d]]

Note that the first element ([]) contains one element (an empty string), so you’ll need to skip that when ranging over the nested slices:

{{ $s := slice (slice "") }}
{{ $s = $s | append (slice "a" "b") }}
{{ $s = $s | append (slice "c" "d") }}

{{ range $s }}
  {{ range . }}
    {{ with . }}
      {{ . | warnf "%[1]v (%[1]T)" }}
    {{ end }}
  {{ end }}
{{ end }}

Why though ? Is that a feature or a bug (asking seriously) ? Is it a drawback of lazy typing ?
Is there another solution ? Does appending to a slice of dict work better ? I’m pretty sure I used the last version before this one, but no matter.

Have a look at the two issues that were addressed by the commit.

See my example above.

It is certainly easier to think about.

2 Likes

I see, a bug seen too late to be fixed without breaking configs. unfortunate.
it does work with a slice of map, without the empty element hack. For now :sweat_smile:. I’ll stick with that.
Thanks for the explanation.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.