Skip to content

Commit

Permalink
Make readFile return nil when file not found (note)
Browse files Browse the repository at this point in the history
Fixes #9620
  • Loading branch information
bep committed Jan 4, 2023
1 parent dd6d0a6 commit 3c51625
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
26 changes: 26 additions & 0 deletions tpl/os/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,29 @@ START:|{{ range $entry := $entries }}{{ if not $entry.IsDir }}{{ $entry.Name }}|
START:|config.toml|myproject.txt|:END:
`)
}

// Issue 9620
func TestReadFileNotExists(t *testing.T) {
t.Parallel()

files := `
-- config.toml --
-- layouts/index.html --
{{ $fi := (readFile "doesnotexist") }}
{{ if $fi }}Failed{{ else }}OK{{ end }}
`

b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
NeedsOsFS: true,
},
).Build()

b.AssertFileContent("public/index.html", `
OK
`)
}
7 changes: 6 additions & 1 deletion tpl/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"

"github.com/bep/overlayfs"
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/deps"
"github.com/spf13/afero"
"github.com/spf13/cast"
Expand Down Expand Up @@ -101,7 +102,11 @@ func (ns *Namespace) ReadFile(i any) (string, error) {
s = ns.deps.PathSpec.RelPathify(s)
}

return readFile(ns.readFileFs, s)
s, err = readFile(ns.readFileFs, s)
if err != nil && herrors.IsNotExist(err) {
return "", nil
}
return s, err
}

// ReadDir lists the directory contents relative to the configured WorkingDir.
Expand Down

1 comment on commit 3c51625

@almai7x
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.