Error calling TranslationBaseName: runtime error: invalid memory address or nil pointer dereference

Hi, I’m having problems with compiling my site. I’ve been getting warnings about TranslationBaseName ever since I made my site multilingual but only recently has it become impossible to compile the site anymore.
The errors I’m getting are:

ERROR render of "taxonomy" failed: "/home/user//website/hugo-site/themes/lugo/layouts/_default/baseof.html:29:28": execute of template failed: template: _default/list.html:29:28: executing "_default/list.html" at <partial "nav.html" .>: error calling partial: "/home/user/website/hugo-site/themes/lugo/layouts/partials/nav.html:4:24": execute of template failed: template: partials/nav.html:4:24: executing "partials/nav.html" at <.File.TranslationBaseName>: error calling TranslationBaseName: runtime error: invalid memory address or nil pointer dereference
ERROR render of "term" failed: "/home/user/website/hugo-site/themes/lugo/layouts/_default/baseof.html:29:28": execute of template failed: template: _default/list.html:29:28: executing "_default/list.html" at <partial "nav.html" .>: error calling partial: "/home/user/website/hugo-site/themes/lugo/layouts/partials/nav.html:4:24": execute of template failed: template: partials/nav.html:4:24: executing "partials/nav.html" at <.File.TranslationBaseName>: error calling TranslationBaseName: runtime error: invalid memory address or nil pointer dereference
Built in 27 ms
Error: error building site: render: failed to render pages: render of "home" failed: "/home/user/website/hugo-site/themes/lugo/layouts/_default/baseof.html:29:28": execute of template failed: template: index.html:29:28: executing "index.html" – File is nil; wrap it in if or with: {{ with partial "nav.html" .>: error calling partial: "/home/user/website/hugo-site/themes/lugo/layouts/partials/nav.html:4:24": execute
of template failed: template: partials/nav.html:4:24: executing "partials/nav.html" at <.File }}{{ .TranslationBaseName }}{{ end }}

I’ve tried different modifications of the nav.html partial, which seems to be causing the issue, but nothing helped. I also tried wrapping the problematic line in with but I’m not a programmer so it was just guesswork and didn’t help.
I discovered that commenting out all language-related options in site’s config allows the site to compile but at the cost of not having a navbar.
Could you please have a look at it and help me figure out how to modify the nav.html file or any other file that’s possibly causing this problem?
Here’s the minimal version of the site: GitHub - Mati20187/hugo-issue
Thank you in advance!

In nav partial Change

{{- $sec := .Page.Section }}
{{ $file := .File.TranslationBaseName -}}

to

{{- $sec := .Page.Section -}}
{{ $file := "" }}
{{ with .File }}
  {{ $file = .TranslationBaseName }}
{{ end }}

This won’t result in null pointer for .TranslationBaseName method

Also, HTML comments are not ignored by Hugo. You should comment out using {{/* */}}. The code you have commented out in the file below needs to be Hugo commented out.

However, using Filenames is not required in most cases to build a language switcher. A simple .Language method call can do it.

@panchtatvam is right.

You should always code defensively when obtaining file properties, because some pages may not be backed by a file:

  • The home page
  • Top level section pages
  • Taxonomy pages
  • Term pages

And pages created by content adapters are never backed by a file.

I suggest you replace your menu code with something like:

{{ $currentPage := . }}
{{ range site.Menus.main }}
  {{ if $currentPage.IsMenuCurrent .Menu . }}
    <a class="active" aria-current="page" href="{{ .URL }}">{{ .Name }}</a>
  {{ else if $currentPage.HasMenuCurrent .Menu . }}
    <a class="ancestor" aria-current="true" href="{{ .URL }}">{{ .Name }}</a>
  {{ else }}
    <a href="{{ .URL }}">{{ .Name }}</a>
  {{ end }}
{{ end }}

See:

1 Like