From 0fbab7cbc5a0b57ec875858111b178160d18acb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 14 Mar 2023 12:18:42 +0100 Subject: [PATCH] commands: Fix data race in test Note that this is a test fix only. --- commands/commandeer.go | 1 + commands/commands.go | 9 +++++---- commands/hugo.go | 1 + common/loggers/loggers.go | 7 ++++--- helpers/general.go | 2 +- helpers/general_test.go | 6 +++--- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/commands/commandeer.go b/commands/commandeer.go index b77e9e908..45385d509 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -302,6 +302,7 @@ func (c *commandeer) loadConfig() error { cfg := c.DepsCfg c.configured = false cfg.Running = c.running + loggers.PanicOnWarning.Store(c.h.panicOnWarning) var dir string if c.h.source != "" { diff --git a/commands/commands.go b/commands/commands.go index c1042459d..5b47ad82e 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -208,9 +208,10 @@ type hugoBuilderCommon struct { baseURL string environment string - buildWatch bool - poll string - clock string + buildWatch bool + panicOnWarning bool + poll string + clock string gc bool @@ -299,7 +300,7 @@ func (cc *hugoBuilderCommon) handleFlags(cmd *cobra.Command) { cmd.Flags().Bool("enableGitInfo", false, "add Git revision, date, author, and CODEOWNERS info to the pages") cmd.Flags().BoolVar(&cc.gc, "gc", false, "enable to run some cleanup tasks (remove unused cache files) after the build") cmd.Flags().StringVar(&cc.poll, "poll", "", "set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes") - cmd.Flags().BoolVar(&loggers.PanicOnWarning, "panicOnWarning", false, "panic on first WARNING log") + cmd.Flags().BoolVar(&cc.panicOnWarning, "panicOnWarning", false, "panic on first WARNING log") cmd.Flags().Bool("templateMetrics", false, "display metrics about template executions") cmd.Flags().Bool("templateMetricsHints", false, "calculate some improvement hints when combined with --templateMetrics") cmd.Flags().BoolP("forceSyncStatic", "", false, "copy all files when static is changed.") diff --git a/commands/hugo.go b/commands/hugo.go index 12575618b..1a35d1626 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -255,6 +255,7 @@ func initializeFlags(cmd *cobra.Command, cfg config.Provider) { setValueFromFlag(cmd.Flags(), "destination", cfg, "publishDir", false) setValueFromFlag(cmd.Flags(), "printI18nWarnings", cfg, "logI18nWarnings", false) setValueFromFlag(cmd.Flags(), "printPathWarnings", cfg, "logPathWarnings", false) + } func setValueFromFlag(flags *flag.FlagSet, key string, cfg config.Provider, targetKey string, force bool) { diff --git a/common/loggers/loggers.go b/common/loggers/loggers.go index 824fe18bd..fbbbca435 100644 --- a/common/loggers/loggers.go +++ b/common/loggers/loggers.go @@ -21,6 +21,7 @@ import ( "os" "regexp" "runtime" + "sync/atomic" "time" "github.com/gohugoio/hugo/common/terminal" @@ -31,7 +32,7 @@ import ( var ( // Counts ERROR logs to the global jww logger. GlobalErrorCounter *jww.Counter - PanicOnWarning bool + PanicOnWarning atomic.Bool ) func init() { @@ -136,14 +137,14 @@ const panicOnWarningMessage = "Warning trapped. Remove the --panicOnWarning flag func (l *logger) Warnf(format string, v ...any) { l.WARN.Printf(format, v...) - if PanicOnWarning { + if PanicOnWarning.Load() { panic(panicOnWarningMessage) } } func (l *logger) Warnln(v ...any) { l.WARN.Println(v...) - if PanicOnWarning { + if PanicOnWarning.Load() { panic(panicOnWarningMessage) } } diff --git a/helpers/general.go b/helpers/general.go index c8a676829..920376227 100644 --- a/helpers/general.go +++ b/helpers/general.go @@ -415,7 +415,7 @@ func Deprecated(item, alternative string, err bool) { DistinctErrorLog.Errorf("%s is deprecated and will be removed in Hugo %s. %s", item, hugo.CurrentVersion.Next().ReleaseVersion(), alternative) } else { var warnPanicMessage string - if !loggers.PanicOnWarning { + if !loggers.PanicOnWarning.Load() { warnPanicMessage = "\n\nRe-run Hugo with the flag --panicOnWarning to get a better error message." } DistinctWarnLog.Warnf("%s is deprecated and will be removed in a future release. %s%s", item, alternative, warnPanicMessage) diff --git a/helpers/general_test.go b/helpers/general_test.go index 95d9c5461..b2ee03f15 100644 --- a/helpers/general_test.go +++ b/helpers/general_test.go @@ -65,10 +65,10 @@ func TestDistinctLoggerDoesNotLockOnWarningPanic(t *testing.T) { // Set PanicOnWarning to true to reproduce issue 9380 // Ensure global variable loggers.PanicOnWarning is reset to old value after test - if loggers.PanicOnWarning == false { - loggers.PanicOnWarning = true + if !loggers.PanicOnWarning.Load() { + loggers.PanicOnWarning.Store(true) defer func() { - loggers.PanicOnWarning = false + loggers.PanicOnWarning.Store(false) }() }