.File.ContentBaseName: can't evaluate field File in type *source.File

After updating Hugo from 0.95 to 0.125 I get an error and I am clueless about the reason.

How can I address this issue?

Start building sites … 
hugo v0.125.3-474c4c02212cf97712c6fbf4159c68822ea6e078+extended linux/amd64 BuildDate=2024-04-22T17:18:35Z VendorInfo=gohugoio

INFO  static: removing all files from destination that don't exist in static dirs
INFO  static: syncing static files to / duration 15.252404ms
INFO  build:  step process substep collect files 1414 files_total 1414 duration 69.357788ms
INFO  build:  step process duration 69.408412ms
INFO  build:  step assemble duration 27.512643ms
INFO  build:  step render pages 758 content 755 duration 8.021755044s
INFO  build:  step postProcess duration 11.802µs
INFO  build:  duration 8.118929724s
Total in 8145 ms
Error: error building site: render: failed to render pages: render of "section" failed: "/home/runner/work/cplace-doc-ops/cplace-doc-ops/themes/cplace/layouts/space/list.html:93:94": execute of template failed: template: space/list.html:93:94: executing "main" at <.File.ContentBaseName>: can't evaluate field File in type *source.File

[13:44:55] 
[13:44:55] 'hugo' errored after 8.21 s
[13:44:55] Error: Command failed: HUGO_ENV=production; hugo --cleanDestinationDir --printPathWarnings -v 

This is the relevant part of list.html:

                    {{- $sub_sections_pages := (union .Pages .Sections) -}}
                    {{ if gt (len $sub_sections_pages) 0 -}}
                        <div class="chapter-list">
                            <ol class="list-unstyled mb-0">
                                {{- range $sub_sections_pages.ByWeight }}
                                    <li>
                                        <a href="{{- .RelPermalink -}}">
                                            {{- if .Title -}}
                                                {{- .Title -}}
                                            {{- else -}}
                                                {{ with .File }}{{- humanize .ContentBaseName -}}{{ end }}
                                            {{- end -}}
                                        </a>

I found that I had some locations with {{ with .File }}{{- humanize .File.ContentBaseName -}}{{ end }}.
I fixed this by removing the .File prefix at such places.

The old Hugo version was more tolerant to this syntax problem it seems. Case closed =)

This is a common issue. If you are using with then whatever that with is checking will be the . (dot) inside of the loop.

So this:

{{ with .File }}
{{- humanize .File.ContentBaseName -}}
{{ end }}

is wrong, because inside of the loop the . is the File. Proper code would be:

{{ with .File }}
{{- humanize .ContentBaseName -}}
{{ end }}

Because inside of the loop the . is actually .File. I hope that clears it up somehow.

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