"io"
"os"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/hugofs"
"github.com/spf13/afero"
counter += count
if err != nil {
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
continue
}
return counter, fmt.Errorf("failed to prune cache %q: %w", k, err)
err = c.Fs.Remove(name)
}
- if err != nil && !os.IsNotExist(err) {
+ if err != nil && !herrors.IsNotExist(err) {
return err
}
counter++
}
- if err != nil && !os.IsNotExist(err) {
+ if err != nil && !herrors.IsNotExist(err) {
return err
}
func (c *Cache) pruneRootDir(force bool) (int, error) {
info, err := c.Fs.Stat(c.pruneAllRootDir)
if err != nil {
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
return 0, nil
}
return 0, err
func (c *commandeer) copyStatic() (map[string]uint64, error) {
m, err := c.doWithPublishDirs(c.copyStaticTo)
- if err == nil || os.IsNotExist(err) {
+ if err == nil || herrors.IsNotExist(err) {
return m, nil
}
return m, err
}
unlock()
case err := <-watcher.Errors():
- if err != nil && !os.IsNotExist(err) {
+ if err != nil && !herrors.IsNotExist(err) {
c.logger.Errorln("Error while watching:", err)
}
}
package commands
import (
- "os"
"path/filepath"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/hugolib/filesystems"
"github.com/fsnotify/fsnotify"
// the source of that static file. In this case Hugo will incorrectly remove that file
// from the published directory.
if ev.Op&fsnotify.Rename == fsnotify.Rename || ev.Op&fsnotify.Remove == fsnotify.Remove {
- if _, err := sourceFs.Fs.Stat(relPath); os.IsNotExist(err) {
+ if _, err := sourceFs.Fs.Stat(relPath); herrors.IsNotExist(err) {
// If file doesn't exist in any static dir, remove it
logger.Println("File no longer exists in static dir, removing", relPath)
_ = c.Fs.PublishDirStatic.RemoveAll(relPath)
-// Copyright 2018 The Hugo Authors. All rights reserved.
+// Copyright 2022 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
"errors"
"fmt"
"io"
+ "os"
"runtime"
"runtime/debug"
"strconv"
// Recover is a helper function that can be used to capture panics.
// Put this at the top of a method/function that crashes in a template:
-// defer herrors.Recover()
+//
+// defer herrors.Recover()
func Recover(args ...any) {
if r := recover(); r != nil {
fmt.Println("ERR:", r)
panic(err)
}
}
+
+// IsNotExist returns true if the error is a file not found error.
+// Unlike os.IsNotExist, this also considers wrapped errors.
+func IsNotExist(err error) bool {
+ if os.IsNotExist(err) {
+ return true
+ }
+
+ // os.IsNotExist does not consider wrapped errors.
+ if os.IsNotExist(errors.Unwrap(err)) {
+ return true
+ }
+
+ return false
+}
--- /dev/null
+// Copyright 2022 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package herrors
+
+import (
+ "fmt"
+ "testing"
+
+ qt "github.com/frankban/quicktest"
+ "github.com/spf13/afero"
+)
+
+func TestIsNotExist(t *testing.T) {
+ c := qt.New(t)
+
+ c.Assert(IsNotExist(afero.ErrFileNotFound), qt.Equals, true)
+ c.Assert(IsNotExist(afero.ErrFileExists), qt.Equals, false)
+ c.Assert(IsNotExist(afero.ErrDestinationExists), qt.Equals, false)
+ c.Assert(IsNotExist(nil), qt.Equals, false)
+
+ c.Assert(IsNotExist(fmt.Errorf("foo")), qt.Equals, false)
+
+ // os.IsNotExist returns false for wrapped errors.
+ c.Assert(IsNotExist(fmt.Errorf("foo: %w", afero.ErrFileNotFound)), qt.Equals, true)
+}
"strings"
"unicode"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/text"
"github.com/gohugoio/hugo/config"
// os.Create will create any new files with mode 0666 (before umask).
f, err := fs.Create(filename)
if err != nil {
- if !os.IsNotExist(err) {
+ if !herrors.IsNotExist(err) {
return nil, err
}
if err = fs.MkdirAll(filepath.Dir(filename), 0777); err != nil { // before umask
"path/filepath"
"strings"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/spf13/afero"
)
// We need to resolve any symlink info.
fi, _, err := lstatIfPossible(l.fs.Fs, filename)
if err != nil {
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
continue
}
return nil, err
"path/filepath"
"strings"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/hugofs/files"
radix "github.com/armon/go-radix"
fi, err := fs.Stat(rm.To)
if err != nil {
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
continue
}
return nil, err
"errors"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/spf13/afero"
)
return fi, i, nil
}
- if !os.IsNotExist(err) {
+ if !herrors.IsNotExist(err) {
// Real error
return nil, -1, err
}
collect := func(lfs *FileMeta) ([]os.FileInfo, error) {
d, err := lfs.Fs.Open(name)
if err != nil {
- if !os.IsNotExist(err) {
+ if !herrors.IsNotExist(err) {
return nil, err
}
return nil, nil
"sort"
"strings"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/loggers"
"errors"
} else {
info, _, err := lstatIfPossible(w.fs, w.root)
if err != nil {
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
return nil
}
return true
}
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
// The file may be removed in process.
// This may be a ERROR situation, but it is not possible
// to determine as a general case.
import (
"io"
- "os"
"path"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/resources/page"
"github.com/hairyhenderson/go-codeowners"
_, err := afs.Stat(f)
if err != nil {
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
continue
}
return nil, err
"github.com/gohugoio/hugo/htesting"
"github.com/gohugoio/hugo/hugofs/glob"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/common/loggers"
// StatResource looks for a resource in these filesystems in order: static, assets and finally content.
// If found in any of them, it returns FileInfo and the relevant filesystem.
-// Any non os.IsNotExist error will be returned.
-// An os.IsNotExist error wil be returned only if all filesystems return such an error.
+// Any non herrors.IsNotExist error will be returned.
+// An herrors.IsNotExist error wil be returned only if all filesystems return such an error.
// Note that if we only wanted to find the file, we could create a composite Afero fs,
// but we also need to know which filesystem root it lives in.
func (s SourceFilesystems) StatResource(lang, filename string) (fi os.FileInfo, fs afero.Fs, err error) {
for _, fsToCheck := range []afero.Fs{s.StaticFs(lang), s.Assets.Fs, s.Content.Fs} {
fs = fsToCheck
fi, err = fs.Stat(filename)
- if err == nil || !os.IsNotExist(err) {
+ if err == nil || !herrors.IsNotExist(err) {
return
}
}
import (
"bytes"
"fmt"
- "os"
"path"
"path/filepath"
"sort"
}
if err := src.Publish(); err != nil {
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
// The resource has been deleted from the file system.
// This should be extremely rare, but can happen on live reload in server
// mode when the same resource is member of different page bundles.
import (
"context"
"fmt"
- "os"
pth "path"
"path/filepath"
"reflect"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/parser/pageparser"
func (c *pagesCollector) collectDir(dirname string, partial bool, inFilter func(fim hugofs.FileMetaInfo) bool) error {
fi, err := c.fs.Stat(dirname)
if err != nil {
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
// May have been deleted.
return nil
}
"log"
"mime"
"net/url"
- "os"
"path"
"path/filepath"
"regexp"
"strings"
"time"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/htime"
"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/common/types"
//
// 1. A list of Files is parsed and then converted into Pages.
//
-// 2. Pages contain sections (based on the file they were generated from),
-// aliases and slugs (included in a pages frontmatter) which are the
-// various targets that will get generated. There will be canonical
-// listing. The canonical path can be overruled based on a pattern.
+// 2. Pages contain sections (based on the file they were generated from),
+// aliases and slugs (included in a pages frontmatter) which are the
+// various targets that will get generated. There will be canonical
+// listing. The canonical path can be overruled based on a pattern.
//
-// 3. Taxonomies are created via configuration and will present some aspect of
-// the final page and typically a perm url.
+// 3. Taxonomies are created via configuration and will present some aspect of
+// the final page and typically a perm url.
//
-// 4. All Pages are passed through a template based on their desired
-// layout based on numerous different elements.
+// 4. All Pages are passed through a template based on their desired
+// layout based on numerous different elements.
//
// 5. The entire collection of files is written to disk.
type Site struct {
// Throw away any directories
isRegular, err := s.SourceSpec.IsRegularSourceFile(ev.Name)
- if err != nil && os.IsNotExist(err) && (ev.Op&fsnotify.Remove == fsnotify.Remove || ev.Op&fsnotify.Rename == fsnotify.Rename) {
+ if err != nil && herrors.IsNotExist(err) && (ev.Op&fsnotify.Remove == fsnotify.Remove || ev.Op&fsnotify.Rename == fsnotify.Rename) {
// Force keep of event
isRegular = true
}
return nil
}
-// Run go vet linter
+// Run go vet linter
func Vet() error {
if err := sh.Run(goexe, "vet", "./..."); err != nil {
return fmt.Errorf("error running go vet: %v", err)
"time"
"github.com/gohugoio/hugo/common/collections"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/hexec"
hglob "github.com/gohugoio/hugo/hugofs/glob"
//
// We, by default, use the /_vendor folder first, if found. To disable,
// run with
-// hugo --ignoreVendorPaths=".*"
+//
+// hugo --ignoreVendorPaths=".*"
//
// Given a module tree, Hugo will pick the first module for a given path,
// meaning that if the top-level module is vendored, that will be the full
configFiles = append(configFiles, filepath.Join(dir, "theme.toml"))
for _, configFile := range configFiles {
if err := hugio.CopyFile(c.fs, configFile, filepath.Join(vendorDir, t.Path(), filepath.Base(configFile))); err != nil {
- if !os.IsNotExist(err) {
+ if !herrors.IsNotExist(err) {
return err
}
}
b := &bytes.Buffer{}
f, err := c.fs.Open(filepath.Join(c.ccfg.WorkingDir, name))
if err != nil {
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
// It's been deleted.
return nil, nil
}
"time"
"github.com/bep/debounce"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/loggers"
"github.com/spf13/cast"
f, err := c.fs.Open(filename)
if err != nil {
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
return nil
}
var err error
fi, err = sourceFs.Stat(fd.SourceFilename)
if err != nil {
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
return nil, nil
}
return nil, err
"fmt"
"io"
"io/fs"
- "os"
"path/filepath"
"reflect"
"regexp"
}
if err := helpers.SymbolicWalk(t.Layouts.Fs, "", walker); err != nil {
- if !os.IsNotExist(err) {
+ if !herrors.IsNotExist(err) {
return err
}
return nil
"time"
"github.com/fsnotify/fsnotify"
+ "github.com/gohugoio/hugo/common/herrors"
)
var (
r.clear()
fi, err := os.Stat(filename)
- if err != nil && !os.IsNotExist(err) {
+ if err != nil && !herrors.IsNotExist(err) {
return err
}
if fi.IsDir() {
f, err := os.Open(filename)
if err != nil {
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
return nil
}
return err
fis, err := f.Readdir(-1)
if err != nil {
- if os.IsNotExist(err) {
+ if herrors.IsNotExist(err) {
return nil
}
return err
}
err := item.right.record(item.filename)
- if err != nil && !os.IsNotExist(err) {
+ if err != nil && !herrors.IsNotExist(err) {
return nil, err
}