From 3f9d0ad2b6045849cbafe133cb9fb82ed5f5ee06 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 16 Feb 2026 00:09:17 +0100 Subject: [PATCH] commands: Fix --panicOnWarning flag having no effect with module version warnings The --panicOnWarning flag was not wired into the root command's logger, so warnings emitted during config loading (such as module version incompatibility warnings) would not trigger a panic. Bind the panicOnWarning flag to the rootCommand struct and set the PanicOnWarningHook on the logger created in createLogger. Fixes #14524 Co-Authored-By: Claude Opus 4.6 --- AGENTS.md | 1 + commands/commandeer.go | 9 ++++++++- hugolib/integrationtest_builder.go | 9 +++++++++ testscripts/commands/hugo__paniconwarning_mod.txt | 13 +++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 testscripts/commands/hugo__paniconwarning_mod.txt diff --git a/AGENTS.md b/AGENTS.md index 8cdf9fdb1..22febd9db 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -7,6 +7,7 @@ * Never export symbols that's not needed outside of the package. * Avoid global state at (almost) all cost. * This is a project with a long history; assume that a similiar problem has been solved before, look hard for helper functions before creating new ones. +* In tests, use `qt` matchers (e.g. `b.Assert(err, qt.ErrorMatches, ...)`) instead of raw `if`/`t.Fatal` checks. * Use `./check.sh ./somepackage/...` when iterating. * Use `./check.sh` when you're done. diff --git a/commands/commandeer.go b/commands/commandeer.go index f233ce2c3..dc7bd7f33 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -131,6 +131,7 @@ type rootCommand struct { gc bool poll string forceSyncStatic bool + panicOnWarning bool // Profile flags (for debugging of performance problems) cpuprofile string @@ -488,12 +489,18 @@ func (r *rootCommand) createLogger(running bool) (loggers.Logger, error) { } } + var logHookLast func(e *logg.Entry) error + if r.panicOnWarning { + logHookLast = loggers.PanicOnWarningHook + } + optsLogger := loggers.Options{ DistinctLevel: logg.LevelWarn, Level: level, StdOut: r.StdOut, StdErr: r.StdErr, StoreErrors: running, + HandlerPost: logHookLast, } return loggers.New(optsLogger), nil @@ -591,7 +598,7 @@ func applyLocalFlagsBuild(cmd *cobra.Command, r *rootCommand) { cmd.Flags().BoolVar(&r.gc, "gc", false, "enable to run some cleanup tasks (remove unused cache files) after the build") cmd.Flags().StringVar(&r.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.RegisterFlagCompletionFunc("poll", cobra.NoFileCompletions) - cmd.Flags().Bool("panicOnWarning", false, "panic on first WARNING log") + cmd.Flags().BoolVar(&r.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().BoolVar(&r.forceSyncStatic, "forceSyncStatic", false, "copy all files when static is changed.") diff --git a/hugolib/integrationtest_builder.go b/hugolib/integrationtest_builder.go index ebc458e07..4628d5f6f 100644 --- a/hugolib/integrationtest_builder.go +++ b/hugolib/integrationtest_builder.go @@ -879,12 +879,18 @@ func (s *IntegrationTestBuilder) initBuilder() error { w = &s.logBuff } + var logHookLast func(e *logg.Entry) error + if s.Cfg.PanicOnWarning { + logHookLast = loggers.PanicOnWarningHook + } + logger := loggers.New( loggers.Options{ StdOut: w, StdErr: w, Level: s.Cfg.LogLevel, DistinctLevel: logg.LevelWarn, + HandlerPost: logHookLast, }, ) @@ -1143,6 +1149,9 @@ type IntegrationTestConfig struct { // The log level to use. LogLevel logg.Level + // Whether to panic on warnings. + PanicOnWarning bool + // Whether it needs the real file system (e.g. for js.Build tests). NeedsOsFS bool diff --git a/testscripts/commands/hugo__paniconwarning_mod.txt b/testscripts/commands/hugo__paniconwarning_mod.txt new file mode 100644 index 000000000..c37c6e590 --- /dev/null +++ b/testscripts/commands/hugo__paniconwarning_mod.txt @@ -0,0 +1,13 @@ +! hugo --panicOnWarning + +stderr 'is not compatible with this Hugo' + +-- hugo.toml -- +baseURL="https://example.org" + +[module] +[module.hugoVersion] +min = "0.148.0" +max = "0.148.0" +-- layouts/all.html -- +All. \ No newline at end of file -- 2.39.5