Error: What to replace .File.Dir with in hugo-book

Hugo throws a warning of this type:

.File.Dir on zero object. Wrap it in if or with: {{ with .File }}{{ .Dir }}{{ end }}

and I’ve tracked it down to the following code:

{{- define "title" -}}
  {{- if .Pages -}}
	{{ $sections := split (trim .File.Dir "/") "/" }}
{{ $title := index ($sections | last 1) 0 | humanize | title }}
{{- default $title .Title -}}
  {{- else -}}
{{ $title :=  .File | humanize | title }}
{{- default $title .Title -}}
  {{- end -}}
{{- end -}}

in shared.html in the hugo-book theme. I’ve tried wrapping the whole thing in the suggested if block but that doesn’t seem to work. Right now I’m clueless how to solve this.

Can you show what you tried?

This:

{{- define "title" -}}
   {{ with .File }}
    	  {{- if .Pages -}}
    		{{ $sections := split (trim .Dir "/") "/" }}
    		{{ $title := index ($sections | last 1) 0 | humanize | title }}
    		{{- default $title .Title -}}
    	  {{- else -}}
    		{{ $title :=  .File | humanize | title }}
    		{{- default $title .Title -}}
    	  {{- end -}}
   {{ end }}
{{ end }}

But it throws an error now (instead of the warning about .File.Dir before).

There is a long story here about the dot (".") and scope etc. that I will not get into, but if you replace “with .File” with “if .File”, it should work.

Thanks, that did solve the issue but I now get another warning that, somewhat ironically I think, tells me not to use .Dir as that’s being deprecated. Specifically,

Page's .Dir is deprecated and will be removed in a future release. Use .File.Dir

Using .File.Dir within the if block again throws the zero object error.

I suspect it means that the “.Dir is deprecated” refers to some other template.

@bep Thanks for your support so far.

What I don’t understand is this: Using .File.Dir throws a zero object error while .Dir is being deprecated. My ‘corrected’ if block statement above uses .Dir which means the original .File.Dir was correct, so why does the suggestion following the warning (see my OP) tell me to use deprecated code?

Hi @DonaldFarfrae, it could be the slight different in with vs if

For example, here’s how you would reference .File.Dir in each case

with

{{ with .File }}
  {{ .Dir }}
{{ end }}

if

{{ if .File }}
  {{ .File.Dir }}
{{ end }}
1 Like

@zwbetz I tried that but to no end. I still get the same error:

WARN ... .File.Dir on zero object. Wrap it in if or with: {{ with .File }}{{ .Dir }}{{ end }}

And neither .File.Dir nor .Dir is used in any other file so I’m pretty sure whatever the problem is it’s with this one.

I cloned that theme and can reproduce your issue. Tried both the “if” and “with” variants, but the warning still shows.


Steps to replicate if anyone else wants to take a stab at this:

  • git clone https://github.com/alex-shpak/hugo-book.git
  • cd hugo-book/exampleSite
  • hugo --themesDir ../..
  • The following console warning shows (among others)

WARN .File.Dir on zero object. Wrap it in if or with: {{ with .File }}{{ .Dir }}{{ end }}

  • Edit lines 4-6 in file layouts\partials\docs\shared.html to be
{{ if .File }}
  {{ $sections := split (trim .File.Dir "/") "/" }}
  {{ $title := index ($sections | last 1) 0 | humanize | title }}
  {{- default $title .Title -}}
{{ end }}
  • Rerun hugo --themesDir ../..
  • The console warning still shows

@zwbetz Thanks for your time.

Since it’s just a warning right now I’ve been running things anyway, but I hope this can be solved somehow. At this point I’m even wondering if this is an issue with Hugo.

It’s not.

dev/dump/hugo-book  master ✔                                                    46d
▶ find . -name "*.html" | xargs grep ".File"
./layouts/partials/docs/shared.html:    {{ $title :=  .File | humanize | title }}
./layouts/partials/docs/git-footer.html:    <a href="{{ $.Site.Params.BookRepo }}/{{ . }}/{{ $.File.Path }}" target="_blank" rel="noopener">

Even when addressing the occurrences found in that find command, I still get the warning

WARN .File.Dir on zero object. Wrap it in if or with: {{ with .File }}{{ .Dir }}{{ end }}

So my fix for that one is not good enough, or I’m missing something

I take that back. I forgot that I did some template magic to handle these, and it looks like it is missing out in some of your if/else constructs … not sure, but I will have a look.

1 Like

Thank you sir

@bep @zwbetz Thank you both for this. I’ll follow on GitHub hoping for a solution and probably update this thread too if we find something, just for future reference.

See this PR:

Note that the fixes there will not have the wanted WARNING effect before Hugo 0.55.2 is out.

You would see those errors even if .File.Dir is not used directly i.e. even if you first saved it to a template variable and then used it.

Just grep for .File in your whole project and study how it’s getting used.

As an example, see the refactoring I did in my theme to resolve the v0.55.0 warnings (including the .File.xxx warnings).