]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Make readFile return nil when file not found (note)
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 30 Dec 2022 10:10:49 +0000 (11:10 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 4 Jan 2023 17:01:26 +0000 (18:01 +0100)
Fixes #9620

tpl/os/integration_test.go
tpl/os/os.go

index fe1bb3d6e0bc2976fd49e54443912d1c2a3ab061..d08374f8fb24e4afb77a237aa62d89e3060451d8 100644 (file)
@@ -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
+`)
+}
index e7fd0593901853bd2e41537bd330e89319799227..4c7089779c0d3221202d728954d3c3827f4740ac 100644 (file)
@@ -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"
@@ -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.