From d70c485707edfd445bcfc0e84181bc15eb146e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 3 Aug 2021 09:57:14 +0200 Subject: [PATCH] Make sure module config loading errors have file positioning info Fixes #8845 --- common/herrors/error_locator.go | 11 +++++++++++ config/configLoader.go | 4 +++- hugolib/config.go | 8 +------- modules/collect.go | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/common/herrors/error_locator.go b/common/herrors/error_locator.go index 118ab851c..2c0d215b1 100644 --- a/common/herrors/error_locator.go +++ b/common/herrors/error_locator.go @@ -100,6 +100,17 @@ func WithFileContextForFile(e error, realFilename, filename string, fs afero.Fs, return WithFileContext(e, realFilename, f, matcher) } +// WithFileContextForFileDefault tries to add file context using the default line matcher. +func WithFileContextForFileDefault(err error, filename string, fs afero.Fs) error { + err, _ = WithFileContextForFile( + err, + filename, + filename, + fs, + SimpleLineMatcher) + return err +} + // WithFileContextForFile will try to add a file context with lines matching the given matcher. // If no match could be found, the original error is returned with false as the second return value. func WithFileContext(e error, realFilename string, r io.Reader, matcher LineMatcherFn) (error, bool) { diff --git a/config/configLoader.go b/config/configLoader.go index 8dcfcbdcc..145b95d7d 100644 --- a/config/configLoader.go +++ b/config/configLoader.go @@ -18,6 +18,8 @@ import ( "path/filepath" "strings" + "github.com/gohugoio/hugo/common/herrors" + "github.com/pkg/errors" "github.com/gohugoio/hugo/common/paths" @@ -58,7 +60,7 @@ func FromConfigString(config, configType string) (Provider, error) { func FromFile(fs afero.Fs, filename string) (Provider, error) { m, err := loadConfigFromFile(fs, filename) if err != nil { - return nil, err + return nil, herrors.WithFileContextForFileDefault(err, filename, fs) } return NewFrom(m), nil } diff --git a/hugolib/config.go b/hugolib/config.go index 5f07a4571..945d9eec6 100644 --- a/hugolib/config.go +++ b/hugolib/config.go @@ -507,11 +507,5 @@ func (configLoader) loadSiteConfig(cfg config.Provider) (scfg SiteConfig, err er } func (l configLoader) wrapFileError(err error, filename string) error { - err, _ = herrors.WithFileContextForFile( - err, - filename, - filename, - l.Fs, - herrors.SimpleLineMatcher) - return err + return herrors.WithFileContextForFileDefault(err, filename, l.Fs) } diff --git a/modules/collect.go b/modules/collect.go index 026373b54..29bf057a6 100644 --- a/modules/collect.go +++ b/modules/collect.go @@ -437,7 +437,7 @@ func (c *collector) applyThemeConfig(tc *moduleAdapter) error { var err error tc.cfg, err = config.FromFile(c.fs, configFilename) if err != nil { - return errors.Wrapf(err, "failed to read module config for %q in %q", tc.Path(), configFilename) + return err } }