Skip to content

Commit

Permalink
commands: Fix data race in test
Browse files Browse the repository at this point in the history
Note that this is a test fix only.
  • Loading branch information
bep committed Mar 14, 2023
1 parent f5eddf8 commit 0fbab7c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions commands/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
9 changes: 5 additions & 4 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.")
Expand Down
1 change: 1 addition & 0 deletions commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions common/loggers/loggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"regexp"
"runtime"
"sync/atomic"
"time"

"github.com/gohugoio/hugo/common/terminal"
Expand All @@ -31,7 +32,7 @@ import (
var (
// Counts ERROR logs to the global jww logger.
GlobalErrorCounter *jww.Counter
PanicOnWarning bool
PanicOnWarning atomic.Bool
)

func init() {
Expand Down Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion helpers/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions helpers/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}()
}

Expand Down

0 comments on commit 0fbab7c

Please sign in to comment.