Skip to content

Commit

Permalink
commands, hugolib: Get file context in "config parse failed" errors
Browse files Browse the repository at this point in the history
Fixes #5325
  • Loading branch information
bep committed Oct 22, 2018
1 parent 2bf686e commit d055e5a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
3 changes: 2 additions & 1 deletion commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,9 +662,10 @@ func (c *commandeer) fullRebuild() {
c.commandeerHugoState = &commandeerHugoState{}
err := c.loadConfig(true, true)
if err != nil {
c.logger.ERROR.Println("Failed to reload config:", err)
// Set the processing on pause until the state is recovered.
c.paused = true
c.handleBuildErr(err, "Failed to reload config")

} else {
c.paused = false
}
Expand Down
18 changes: 15 additions & 3 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ package hugolib
import (
"errors"
"fmt"

"io"
"strings"

"github.com/gohugoio/hugo/common/herrors"

"github.com/gohugoio/hugo/hugolib/paths"
_errors "github.com/pkg/errors"

Expand Down Expand Up @@ -106,12 +107,23 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
v.SetConfigFile(configFilenames[0])
v.AddConfigPath(d.Path)

applyFileContext := func(filename string, err error) error {
err, _ = herrors.WithFileContextForFile(
err,
filename,
filename,
fs,
herrors.SimpleLineMatcher)

return err
}

var configFileErr error

err := v.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigParseError); ok {
return nil, configFiles, err
return nil, configFiles, applyFileContext(v.ConfigFileUsed(), err)
}
configFileErr = ErrNoConfigFile
}
Expand All @@ -129,7 +141,7 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
return nil, configFiles, fmt.Errorf("Unable to open Config file.\n (%s)\n", err)
}
if err = v.MergeConfig(r); err != nil {
return nil, configFiles, fmt.Errorf("Unable to parse/merge Config file (%s).\n (%s)\n", configFile, err)
return nil, configFiles, applyFileContext(configFile, err)
}
configFiles = append(configFiles, configFile)
}
Expand Down
15 changes: 10 additions & 5 deletions hugolib/hugo_sites_build_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (t testSiteBuildErrorAsserter) assertLineNumber(lineNumber int, err error)
func (t testSiteBuildErrorAsserter) assertErrorMessage(e1, e2 string) {
// The error message will contain filenames with OS slashes. Normalize before compare.
e1, e2 = filepath.ToSlash(e1), filepath.ToSlash(e2)
t.assert.Equal(e1, e2, trace())
t.assert.Contains(e2, e1, trace())

}

Expand Down Expand Up @@ -102,8 +102,8 @@ func TestSiteBuildErrors(t *testing.T) {
fe := a.getFileError(err)
assert.Equal(5, fe.LineNumber)
assert.Equal(14, fe.ColumnNumber)
assert.Equal("md", fe.ChromaLexer)
a.assertErrorMessage("asdfadf", fe.Error())
assert.Equal("go-html-template", fe.ChromaLexer)
a.assertErrorMessage("\"layouts/_default/single.html:5:14\": execute of template failed", fe.Error())

},
},
Expand All @@ -124,7 +124,12 @@ func TestSiteBuildErrors(t *testing.T) {
return strings.Replace(content, ".Title", ".Titles", 1)
},
assertBuildError: func(a testSiteBuildErrorAsserter, err error) {
a.assertLineNumber(4, err)
fe := a.getFileError(err)
assert.Equal(7, fe.LineNumber)
assert.Equal("md", fe.ChromaLexer)
// Make sure that it contains both the content file and template
a.assertErrorMessage(`content/myyaml.md:7:10": failed to render shortcode "sc"`, fe.Error())
a.assertErrorMessage(`shortcodes/sc.html:4:22: executing "shortcodes/sc.html" at <.Page.Titles>: can't evaluate`, fe.Error())
},
},
{
Expand Down Expand Up @@ -173,7 +178,7 @@ func TestSiteBuildErrors(t *testing.T) {
},
assertBuildError: func(a testSiteBuildErrorAsserter, err error) {
assert.Error(err)
assert.Contains(err.Error(), "single.html")
assert.Contains(err.Error(), `"content/mytoml.md": render of "page" failed: execute of template failed: panic in Execute`)
},
},
}
Expand Down

0 comments on commit d055e5a

Please sign in to comment.