From cb6e27b32a1d09956027b8e45bae0c18c1593d5c Mon Sep 17 00:00:00 2001 From: Bjørn Erik Pedersen Date: Wed, 23 Oct 2024 19:26:13 +0200 Subject: hugolib/commands: Fix stuck server error issues Fixes #11378 --- commands/hugobuilder.go | 58 +++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 24 deletions(-) (limited to 'commands/hugobuilder.go') diff --git a/commands/hugobuilder.go b/commands/hugobuilder.go index 42bf68a37..95129018f 100644 --- a/commands/hugobuilder.go +++ b/commands/hugobuilder.go @@ -27,7 +27,6 @@ import ( "sync/atomic" "time" - "github.com/bep/logg" "github.com/bep/simplecobra" "github.com/fsnotify/fsnotify" "github.com/gohugoio/hugo/common/herrors" @@ -136,10 +135,6 @@ func (e *hugoBuilderErrState) wasErr() bool { return e.waserr } -func (c *hugoBuilder) errCount() int { - return c.r.logger.LoggCount(logg.LevelError) + loggers.Log().LoggCount(logg.LevelError) -} - // getDirList provides NewWatcher() with a list of directories to watch for changes. func (c *hugoBuilder) getDirList() ([]string, error) { h, err := c.hugo() @@ -345,7 +340,6 @@ func (c *hugoBuilder) newWatcher(pollIntervalStr string, dirList ...string) (*wa for { select { case changes := <-c.r.changesFromBuild: - c.errState.setBuildErr(nil) unlock, err := h.LockBuild() if err != nil { c.r.logger.Errorln("Failed to acquire a build lock: %s", err) @@ -358,7 +352,7 @@ func (c *hugoBuilder) newWatcher(pollIntervalStr string, dirList ...string) (*wa } if c.s != nil && c.s.doLiveReload { doReload := c.changeDetector == nil || len(c.changeDetector.changed()) > 0 - doReload = doReload || c.showErrorInBrowser && c.errCount() > 0 + doReload = doReload || c.showErrorInBrowser && c.errState.buildErr() != nil if doReload { livereload.ForceRefresh() } @@ -372,7 +366,7 @@ func (c *hugoBuilder) newWatcher(pollIntervalStr string, dirList ...string) (*wa return } c.handleEvents(watcher, staticSyncer, evs, configSet) - if c.showErrorInBrowser && c.errCount() > 0 { + if c.showErrorInBrowser && c.errState.buildErr() != nil { // Need to reload browser to show the error livereload.ForceRefresh() } @@ -419,11 +413,17 @@ func (c *hugoBuilder) build() error { } func (c *hugoBuilder) buildSites(noBuildLock bool) (err error) { - h, err := c.hugo() + defer func() { + c.errState.setBuildErr(err) + }() + + var h *hugolib.HugoSites + h, err = c.hugo() if err != nil { - return err + return } - return h.Build(hugolib.BuildCfg{NoBuildLock: noBuildLock}) + err = h.Build(hugolib.BuildCfg{NoBuildLock: noBuildLock}) + return } func (c *hugoBuilder) copyStatic() (map[string]uint64, error) { @@ -619,6 +619,9 @@ func (c *hugoBuilder) fullRebuild(changeType string) { // Set the processing on pause until the state is recovered. c.errState.setPaused(true) c.handleBuildErr(err, "Failed to reload config") + if c.s.doLiveReload { + livereload.ForceRefresh() + } } else { c.errState.setPaused(false) } @@ -1081,37 +1084,44 @@ func (c *hugoBuilder) printChangeDetected(typ string) { c.r.logger.Println(htime.Now().Format(layout)) } -func (c *hugoBuilder) rebuildSites(events []fsnotify.Event) error { +func (c *hugoBuilder) rebuildSites(events []fsnotify.Event) (err error) { + defer func() { + c.errState.setBuildErr(err) + }() if err := c.errState.buildErr(); err != nil { ferrs := herrors.UnwrapFileErrorsWithErrorContext(err) for _, err := range ferrs { events = append(events, fsnotify.Event{Name: err.Position().Filename, Op: fsnotify.Write}) } } - c.errState.setBuildErr(nil) - h, err := c.hugo() + var h *hugolib.HugoSites + h, err = c.hugo() if err != nil { - return err + return } - - return h.Build(hugolib.BuildCfg{NoBuildLock: true, RecentlyVisited: c.visitedURLs, ErrRecovery: c.errState.wasErr()}, events...) + err = h.Build(hugolib.BuildCfg{NoBuildLock: true, RecentlyVisited: c.visitedURLs, ErrRecovery: c.errState.wasErr()}, events...) + return } -func (c *hugoBuilder) rebuildSitesForChanges(ids []identity.Identity) error { - c.errState.setBuildErr(nil) - h, err := c.hugo() +func (c *hugoBuilder) rebuildSitesForChanges(ids []identity.Identity) (err error) { + defer func() { + c.errState.setBuildErr(err) + }() + + var h *hugolib.HugoSites + h, err = c.hugo() if err != nil { - return err + return } whatChanged := &hugolib.WhatChanged{} whatChanged.Add(ids...) err = h.Build(hugolib.BuildCfg{NoBuildLock: true, WhatChanged: whatChanged, RecentlyVisited: c.visitedURLs, ErrRecovery: c.errState.wasErr()}) - c.errState.setBuildErr(err) - return err + + return } func (c *hugoBuilder) reloadConfig() error { - c.r.Reset() + c.r.resetLogs() c.r.configVersionID.Add(1) if err := c.withConfE(func(conf *commonConfig) error { -- cgit v1.2.3