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

#1

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.

0 Likes

#2

Can you show what you tried?

0 Likes

#3

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).

0 Likes

#4

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.

0 Likes

#5

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.

0 Likes

#6

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

0 Likes

#7

@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?

0 Likes

#8

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 }}
0 Likes

#9

@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.

0 Likes

#10

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
0 Likes

#11

@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.

0 Likes

#12

It’s not.

0 Likes

#13
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">

0 Likes

#14

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

0 Likes

#15

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.

0 Likes

#16
1 Like

#17

Thank you sir

0 Likes

#18

@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.

0 Likes

#19

See this PR:

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

0 Likes

#20

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).

0 Likes