commands: Don't fail on template errors on go mod graph etc.

Fixes #8942
This commit is contained in:
Bjørn Erik Pedersen 2021-08-31 17:02:51 +02:00
parent 04b5959961
commit 7d1f806ecb
12 changed files with 45 additions and 27 deletions

View file

@ -61,6 +61,11 @@ type commandeer struct {
logger loggers.Logger logger loggers.Logger
serverConfig *config.Server serverConfig *config.Server
// Loading state
mustHaveConfigFile bool
failOnInitErr bool
running bool
// Currently only set when in "fast render mode". But it seems to // Currently only set when in "fast render mode". But it seems to
// be fast enough that we could maybe just add it for all server modes. // be fast enough that we could maybe just add it for all server modes.
changeDetector *fileChangeDetector changeDetector *fileChangeDetector
@ -153,7 +158,7 @@ func (c *commandeer) initFs(fs *hugofs.Fs) error {
return nil return nil
} }
func newCommandeer(mustHaveConfigFile, running bool, h *hugoBuilderCommon, f flagsToConfigHandler, cfgInit func(c *commandeer) error, subCmdVs ...*cobra.Command) (*commandeer, error) { func newCommandeer(mustHaveConfigFile, failOnInitErr, running bool, h *hugoBuilderCommon, f flagsToConfigHandler, cfgInit func(c *commandeer) error, subCmdVs ...*cobra.Command) (*commandeer, error) {
var rebuildDebouncer func(f func()) var rebuildDebouncer func(f func())
if running { if running {
// The time value used is tested with mass content replacements in a fairly big Hugo site. // The time value used is tested with mass content replacements in a fairly big Hugo site.
@ -175,11 +180,17 @@ func newCommandeer(mustHaveConfigFile, running bool, h *hugoBuilderCommon, f fla
visitedURLs: types.NewEvictingStringQueue(10), visitedURLs: types.NewEvictingStringQueue(10),
debounce: rebuildDebouncer, debounce: rebuildDebouncer,
fullRebuildSem: semaphore.NewWeighted(1), fullRebuildSem: semaphore.NewWeighted(1),
// Init state
mustHaveConfigFile: mustHaveConfigFile,
failOnInitErr: failOnInitErr,
running: running,
// This will be replaced later, but we need something to log to before the configuration is read. // This will be replaced later, but we need something to log to before the configuration is read.
logger: loggers.NewLogger(jww.LevelWarn, jww.LevelError, out, ioutil.Discard, running), logger: loggers.NewLogger(jww.LevelWarn, jww.LevelError, out, ioutil.Discard, running),
} }
return c, c.loadConfig(mustHaveConfigFile, running) return c, c.loadConfig()
} }
type fileChangeDetector struct { type fileChangeDetector struct {
@ -244,7 +255,7 @@ func (f *fileChangeDetector) PrepareNew() {
f.current = make(map[string]string) f.current = make(map[string]string)
} }
func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error { func (c *commandeer) loadConfig() error {
if c.DepsCfg == nil { if c.DepsCfg == nil {
c.DepsCfg = &deps.DepsCfg{} c.DepsCfg = &deps.DepsCfg{}
} }
@ -256,7 +267,7 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {
cfg := c.DepsCfg cfg := c.DepsCfg
c.configured = false c.configured = false
cfg.Running = running cfg.Running = c.running
var dir string var dir string
if c.h.source != "" { if c.h.source != "" {
@ -270,7 +281,7 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {
sourceFs = c.DepsCfg.Fs.Source sourceFs = c.DepsCfg.Fs.Source
} }
environment := c.h.getEnvironment(running) environment := c.h.getEnvironment(c.running)
doWithConfig := func(cfg config.Provider) error { doWithConfig := func(cfg config.Provider) error {
if c.ftch != nil { if c.ftch != nil {
@ -312,10 +323,10 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {
// We should improve the error handling here, // We should improve the error handling here,
// but with hugo mod init and similar there is a chicken and egg situation // but with hugo mod init and similar there is a chicken and egg situation
// with modules already configured in config.toml, so ignore those errors. // with modules already configured in config.toml, so ignore those errors.
if mustHaveConfigFile || !moduleNotFoundRe.MatchString(err.Error()) { if c.mustHaveConfigFile || !moduleNotFoundRe.MatchString(err.Error()) {
return err return err
} }
} else if mustHaveConfigFile && len(configFiles) == 0 { } else if c.mustHaveConfigFile && len(configFiles) == 0 {
return hugolib.ErrNoConfigFile return hugolib.ErrNoConfigFile
} }
@ -327,7 +338,7 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {
} }
// Set some commonly used flags // Set some commonly used flags
c.doLiveReload = running && !c.Cfg.GetBool("disableLiveReload") c.doLiveReload = c.running && !c.Cfg.GetBool("disableLiveReload")
c.fastRenderMode = c.doLiveReload && !c.Cfg.GetBool("disableFastRender") c.fastRenderMode = c.doLiveReload && !c.Cfg.GetBool("disableFastRender")
c.showErrorInBrowser = c.doLiveReload && !c.Cfg.GetBool("disableBrowserError") c.showErrorInBrowser = c.doLiveReload && !c.Cfg.GetBool("disableBrowserError")
@ -339,7 +350,7 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {
} }
} }
logger, err := c.createLogger(config, running) logger, err := c.createLogger(config)
if err != nil { if err != nil {
return err return err
} }
@ -399,7 +410,11 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {
var h *hugolib.HugoSites var h *hugolib.HugoSites
h, err = hugolib.NewHugoSites(*c.DepsCfg) var createErr error
h, createErr = hugolib.NewHugoSites(*c.DepsCfg)
if h == nil || c.failOnInitErr {
err = createErr
}
c.hugoSites = h c.hugoSites = h
close(c.created) close(c.created)
}) })

View file

@ -165,7 +165,7 @@ Complete documentation is available at http://gohugo.io/.`,
// prevent cobra printing error so it can be handled here (before the timeTrack prints) // prevent cobra printing error so it can be handled here (before the timeTrack prints)
cmd.SilenceErrors = true cmd.SilenceErrors = true
c, err := initializeConfig(true, cc.buildWatch, &cc.hugoBuilderCommon, cc, cfgInit) c, err := initializeConfig(true, true, cc.buildWatch, &cc.hugoBuilderCommon, cc, cfgInit)
if err != nil { if err != nil {
cmd.PrintErrln("Error:", err.Error()) cmd.PrintErrln("Error:", err.Error())
return err return err

View file

@ -62,7 +62,7 @@ func (b *commandsBuilder) newConfigCmd() *configCmd {
} }
func (c *configCmd) printMounts(cmd *cobra.Command, args []string) error { func (c *configCmd) printMounts(cmd *cobra.Command, args []string) error {
cfg, err := initializeConfig(true, false, &c.hugoBuilderCommon, c, nil) cfg, err := initializeConfig(true, false, false, &c.hugoBuilderCommon, c, nil)
if err != nil { if err != nil {
return err return err
} }
@ -78,7 +78,7 @@ func (c *configCmd) printMounts(cmd *cobra.Command, args []string) error {
} }
func (c *configCmd) printConfig(cmd *cobra.Command, args []string) error { func (c *configCmd) printConfig(cmd *cobra.Command, args []string) error {
cfg, err := initializeConfig(true, false, &c.hugoBuilderCommon, c, nil) cfg, err := initializeConfig(true, false, false, &c.hugoBuilderCommon, c, nil)
if err != nil { if err != nil {
return err return err
} }

View file

@ -102,7 +102,7 @@ func (cc *convertCmd) convertContents(format metadecoders.Format) error {
return newUserError("Unsafe operation not allowed, use --unsafe or set a different output path") return newUserError("Unsafe operation not allowed, use --unsafe or set a different output path")
} }
c, err := initializeConfig(true, false, &cc.hugoBuilderCommon, cc, nil) c, err := initializeConfig(true, false, false, &cc.hugoBuilderCommon, cc, nil)
if err != nil { if err != nil {
return err return err
} }

View file

@ -60,7 +60,7 @@ documentation.
c.Set("maxDeletes", cc.maxDeletes) c.Set("maxDeletes", cc.maxDeletes)
return nil return nil
} }
comm, err := initializeConfig(true, false, &cc.hugoBuilderCommon, cc, cfgInit) comm, err := initializeConfig(true, true, false, &cc.hugoBuilderCommon, cc, cfgInit)
if err != nil { if err != nil {
return err return err
} }

View file

@ -116,11 +116,11 @@ func Execute(args []string) Response {
} }
// InitializeConfig initializes a config file with sensible default configuration flags. // InitializeConfig initializes a config file with sensible default configuration flags.
func initializeConfig(mustHaveConfigFile, running bool, func initializeConfig(mustHaveConfigFile, failOnInitErr, running bool,
h *hugoBuilderCommon, h *hugoBuilderCommon,
f flagsToConfigHandler, f flagsToConfigHandler,
cfgInit func(c *commandeer) error) (*commandeer, error) { cfgInit func(c *commandeer) error) (*commandeer, error) {
c, err := newCommandeer(mustHaveConfigFile, running, h, f, cfgInit) c, err := newCommandeer(mustHaveConfigFile, failOnInitErr, running, h, f, cfgInit)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -128,7 +128,7 @@ func initializeConfig(mustHaveConfigFile, running bool,
return c, nil return c, nil
} }
func (c *commandeer) createLogger(cfg config.Provider, running bool) (loggers.Logger, error) { func (c *commandeer) createLogger(cfg config.Provider) (loggers.Logger, error) {
var ( var (
logHandle = ioutil.Discard logHandle = ioutil.Discard
logThreshold = jww.LevelWarn logThreshold = jww.LevelWarn
@ -172,7 +172,7 @@ func (c *commandeer) createLogger(cfg config.Provider, running bool) (loggers.Lo
loggers.InitGlobalLogger(stdoutThreshold, logThreshold, outHandle, logHandle) loggers.InitGlobalLogger(stdoutThreshold, logThreshold, outHandle, logHandle)
helpers.InitLoggers() helpers.InitLoggers()
return loggers.NewLogger(stdoutThreshold, logThreshold, outHandle, logHandle, running), nil return loggers.NewLogger(stdoutThreshold, logThreshold, outHandle, logHandle, c.running), nil
} }
func initializeFlags(cmd *cobra.Command, cfg config.Provider) { func initializeFlags(cmd *cobra.Command, cfg config.Provider) {
@ -792,7 +792,7 @@ func (c *commandeer) fullRebuild(changeType string) {
defer c.timeTrack(time.Now(), "Rebuilt") defer c.timeTrack(time.Now(), "Rebuilt")
c.commandeerHugoState = newCommandeerHugoState() c.commandeerHugoState = newCommandeerHugoState()
err := c.loadConfig(true, true) err := c.loadConfig()
if err != nil { if err != nil {
// Set the processing on pause until the state is recovered. // Set the processing on pause until the state is recovered.
c.paused = true c.paused = true

View file

@ -40,7 +40,7 @@ func (lc *listCmd) buildSites(config map[string]interface{}) (*hugolib.HugoSites
return nil return nil
} }
c, err := initializeConfig(true, false, &lc.hugoBuilderCommon, lc, cfgInit) c, err := initializeConfig(true, true, false, &lc.hugoBuilderCommon, lc, cfgInit)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -284,7 +284,7 @@ func (c *modCmd) withHugo(f func(*hugolib.HugoSites) error) error {
} }
func (c *modCmd) initConfig(failOnNoConfig bool) (*commandeer, error) { func (c *modCmd) initConfig(failOnNoConfig bool) (*commandeer, error) {
com, err := initializeConfig(failOnNoConfig, false, &c.hugoBuilderCommon, c, nil) com, err := initializeConfig(failOnNoConfig, false, false, &c.hugoBuilderCommon, c, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -71,7 +71,7 @@ func (n *newCmd) newContent(cmd *cobra.Command, args []string) error {
return nil return nil
} }
c, err := initializeConfig(true, false, &n.hugoBuilderCommon, n, cfgInit) c, err := initializeConfig(true, true, false, &n.hugoBuilderCommon, n, cfgInit)
if err != nil { if err != nil {
return err return err
} }

View file

@ -52,7 +52,7 @@ as you see fit.`,
// newTheme creates a new Hugo theme template // newTheme creates a new Hugo theme template
func (n *newThemeCmd) newTheme(cmd *cobra.Command, args []string) error { func (n *newThemeCmd) newTheme(cmd *cobra.Command, args []string) error {
c, err := initializeConfig(false, false, &n.hugoBuilderCommon, n, nil) c, err := initializeConfig(false, false, false, &n.hugoBuilderCommon, n, nil)
if err != nil { if err != nil {
return err return err
} }

View file

@ -239,7 +239,7 @@ func (sc *serverCmd) server(cmd *cobra.Command, args []string) error {
// silence errors in cobra so we can handle them here // silence errors in cobra so we can handle them here
cmd.SilenceErrors = true cmd.SilenceErrors = true
c, err := initializeConfig(true, true, &sc.hugoBuilderCommon, sc, cfgInit) c, err := initializeConfig(true, true, true, &sc.hugoBuilderCommon, sc, cfgInit)
if err != nil { if err != nil {
cmd.PrintErrln("Error:", err.Error()) cmd.PrintErrln("Error:", err.Error())
return err return err

View file

@ -299,6 +299,9 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
return nil, errors.New("Cannot provide Language in Cfg when sites are provided") return nil, errors.New("Cannot provide Language in Cfg when sites are provided")
} }
// Return error at the end. Make the caller decide if it's fatal or not.
var initErr error
langConfig, err := newMultiLingualFromSites(cfg.Cfg, sites...) langConfig, err := newMultiLingualFromSites(cfg.Cfg, sites...)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to create language config") return nil, errors.Wrap(err, "failed to create language config")
@ -376,7 +379,7 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
var l configLoader var l configLoader
if err := l.applyDeps(cfg, sites...); err != nil { if err := l.applyDeps(cfg, sites...); err != nil {
return nil, errors.Wrap(err, "add site dependencies") initErr = errors.Wrap(err, "add site dependencies")
} }
h.Deps = sites[0].Deps h.Deps = sites[0].Deps
@ -393,7 +396,7 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
h.ContentChanges = contentChangeTracker h.ContentChanges = contentChangeTracker
} }
return h, nil return h, initErr
} }
func (h *HugoSites) loadGitInfo() error { func (h *HugoSites) loadGitInfo() error {