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/commandeer.go | 2 +- commands/hugobuilder.go | 58 +++++++++++++++++++++++++++++-------------------- commands/server.go | 20 +++++++++-------- 3 files changed, 46 insertions(+), 34 deletions(-) (limited to 'commands') diff --git a/commands/commandeer.go b/commands/commandeer.go index ad2adf3a2..69077ad73 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -507,7 +507,7 @@ func (r *rootCommand) createLogger(running bool) (loggers.Logger, error) { return loggers.New(optsLogger), nil } -func (r *rootCommand) Reset() { +func (r *rootCommand) resetLogs() { r.logger.Reset() loggers.Log().Reset() } 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 { diff --git a/commands/server.go b/commands/server.go index 84d4165f0..6b801b158 100644 --- a/commands/server.go +++ b/commands/server.go @@ -648,9 +648,8 @@ func (c *serverCommand) setServerInfoInConfig() error { } func (c *serverCommand) getErrorWithContext() any { - errCount := c.errCount() - - if errCount == 0 { + buildErr := c.errState.buildErr() + if buildErr == nil { return nil } @@ -659,7 +658,7 @@ func (c *serverCommand) getErrorWithContext() any { m["Error"] = cleanErrorLog(c.r.logger.Errors()) m["Version"] = hugo.BuildVersionString() - ferrors := herrors.UnwrapFileErrorsWithErrorContext(c.errState.buildErr()) + ferrors := herrors.UnwrapFileErrorsWithErrorContext(buildErr) m["Files"] = ferrors return m @@ -830,22 +829,25 @@ func (c *serverCommand) fixURL(baseURLFromConfig, baseURLFromFlag string, port i return u.String(), nil } -func (c *serverCommand) partialReRender(urls ...string) error { +func (c *serverCommand) partialReRender(urls ...string) (err error) { defer func() { c.errState.setWasErr(false) }() - c.errState.setBuildErr(nil) visited := types.NewEvictingStringQueue(len(urls)) for _, url := range urls { visited.Add(url) } - h, err := c.hugo() + var h *hugolib.HugoSites + h, err = c.hugo() if err != nil { - return err + return } + // Note: We do not set NoBuildLock as the file lock is not acquired at this stage. - return h.Build(hugolib.BuildCfg{NoBuildLock: false, RecentlyVisited: visited, PartialReRender: true, ErrRecovery: c.errState.wasErr()}) + err = h.Build(hugolib.BuildCfg{NoBuildLock: false, RecentlyVisited: visited, PartialReRender: true, ErrRecovery: c.errState.wasErr()}) + + return } func (c *serverCommand) serve() error { -- cgit v1.2.3