Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions commands/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ type commandeer struct {
logger loggers.Logger
serverConfig *config.Server

// Loading state
mustHaveConfigFile bool
failOnInitErr bool
running bool

// 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.
changeDetector *fileChangeDetector
Expand Down Expand Up @@ -153,7 +158,7 @@ func (c *commandeer) initFs(fs *hugofs.Fs) error {
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())
if running {
// The time value used is tested with mass content replacements in a fairly big Hugo site.
Expand All @@ -175,11 +180,17 @@ func newCommandeer(mustHaveConfigFile, running bool, h *hugoBuilderCommon, f fla
visitedURLs: types.NewEvictingStringQueue(10),
debounce: rebuildDebouncer,
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.
logger: loggers.NewLogger(jww.LevelWarn, jww.LevelError, out, ioutil.Discard, running),
}

return c, c.loadConfig(mustHaveConfigFile, running)
return c, c.loadConfig()
}

type fileChangeDetector struct {
Expand Down Expand Up @@ -244,7 +255,7 @@ func (f *fileChangeDetector) PrepareNew() {
f.current = make(map[string]string)
}

func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {
func (c *commandeer) loadConfig() error {
if c.DepsCfg == nil {
c.DepsCfg = &deps.DepsCfg{}
}
Expand All @@ -256,7 +267,7 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {

cfg := c.DepsCfg
c.configured = false
cfg.Running = running
cfg.Running = c.running

var dir string
if c.h.source != "" {
Expand All @@ -270,7 +281,7 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {
sourceFs = c.DepsCfg.Fs.Source
}

environment := c.h.getEnvironment(running)
environment := c.h.getEnvironment(c.running)

doWithConfig := func(cfg config.Provider) error {
if c.ftch != nil {
Expand Down Expand Up @@ -312,10 +323,10 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {
// We should improve the error handling here,
// 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.
if mustHaveConfigFile || !moduleNotFoundRe.MatchString(err.Error()) {
if c.mustHaveConfigFile || !moduleNotFoundRe.MatchString(err.Error()) {
return err
}
} else if mustHaveConfigFile && len(configFiles) == 0 {
} else if c.mustHaveConfigFile && len(configFiles) == 0 {
return hugolib.ErrNoConfigFile
}

Expand All @@ -327,7 +338,7 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {
}

// 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.showErrorInBrowser = c.doLiveReload && !c.Cfg.GetBool("disableBrowserError")

Expand All @@ -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 {
return err
}
Expand Down Expand Up @@ -399,7 +410,11 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {

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
close(c.created)
})
Expand Down
2 changes: 1 addition & 1 deletion commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 {
cmd.PrintErrln("Error:", err.Error())
return err
Expand Down
4 changes: 2 additions & 2 deletions commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (b *commandsBuilder) newConfigCmd() *configCmd {
}

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 {
return err
}
Expand All @@ -78,7 +78,7 @@ func (c *configCmd) printMounts(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 {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

c, err := initializeConfig(true, false, &cc.hugoBuilderCommon, cc, nil)
c, err := initializeConfig(true, false, false, &cc.hugoBuilderCommon, cc, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ documentation.
c.Set("maxDeletes", cc.maxDeletes)
return nil
}
comm, err := initializeConfig(true, false, &cc.hugoBuilderCommon, cc, cfgInit)
comm, err := initializeConfig(true, true, false, &cc.hugoBuilderCommon, cc, cfgInit)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ func Execute(args []string) Response {
}

// InitializeConfig initializes a config file with sensible default configuration flags.
func initializeConfig(mustHaveConfigFile, running bool,
func initializeConfig(mustHaveConfigFile, failOnInitErr, running bool,
h *hugoBuilderCommon,
f flagsToConfigHandler,
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 {
return nil, err
}

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 (
logHandle = ioutil.Discard
logThreshold = jww.LevelWarn
Expand Down Expand Up @@ -172,7 +172,7 @@ func (c *commandeer) createLogger(cfg config.Provider, running bool) (loggers.Lo
loggers.InitGlobalLogger(stdoutThreshold, logThreshold, outHandle, logHandle)
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) {
Expand Down Expand Up @@ -792,7 +792,7 @@ func (c *commandeer) fullRebuild(changeType string) {
defer c.timeTrack(time.Now(), "Rebuilt")

c.commandeerHugoState = newCommandeerHugoState()
err := c.loadConfig(true, true)
err := c.loadConfig()
if err != nil {
// Set the processing on pause until the state is recovered.
c.paused = true
Expand Down
2 changes: 1 addition & 1 deletion commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (lc *listCmd) buildSites(config map[string]interface{}) (*hugolib.HugoSites
return nil
}

c, err := initializeConfig(true, false, &lc.hugoBuilderCommon, lc, cfgInit)
c, err := initializeConfig(true, true, false, &lc.hugoBuilderCommon, lc, cfgInit)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (c *modCmd) withHugo(f func(*hugolib.HugoSites) error) 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 {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (n *newCmd) newContent(cmd *cobra.Command, args []string) error {
return nil
}

c, err := initializeConfig(true, false, &n.hugoBuilderCommon, n, cfgInit)
c, err := initializeConfig(true, true, false, &n.hugoBuilderCommon, n, cfgInit)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/new_theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ as you see fit.`,

// newTheme creates a new Hugo theme template
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 {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (sc *serverCmd) server(cmd *cobra.Command, args []string) error {
// silence errors in cobra so we can handle them here
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 {
cmd.PrintErrln("Error:", err.Error())
return err
Expand Down
7 changes: 5 additions & 2 deletions hugolib/hugo_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 error at the end. Make the caller decide if it's fatal or not.
var initErr error

langConfig, err := newMultiLingualFromSites(cfg.Cfg, sites...)
if err != nil {
return nil, errors.Wrap(err, "failed to create language config")
Expand Down Expand Up @@ -376,7 +379,7 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {

var l configLoader
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
Expand All @@ -393,7 +396,7 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
h.ContentChanges = contentChangeTracker
}

return h, nil
return h, initErr
}

func (h *HugoSites) loadGitInfo() error {
Expand Down