]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
commands: Fix --panicOnWarning flag having no effect with module version warnings
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 15 Feb 2026 23:09:17 +0000 (00:09 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 16 Feb 2026 21:34:29 +0000 (22:34 +0100)
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 <noreply@anthropic.com>
AGENTS.md
commands/commandeer.go
hugolib/integrationtest_builder.go
testscripts/commands/hugo__paniconwarning_mod.txt [new file with mode: 0644]

index 8cdf9fdb1b6ccaedb258ed84468d7deb728eb6c1..22febd9db3c8657f6948cd40e86259c0983b6439 100644 (file)
--- 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.
 
index f233ce2c3a7c2e0d856cc6ae3eb88b28336948d1..dc7bd7f33943f1f60ec0e0017b74c996177ed630 100644 (file)
@@ -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.")
index ebc458e079f3edf416efee6774c1450342d88b1e..4628d5f6f32ab3fc0e62e760a54f626463db6888 100644 (file)
@@ -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 (file)
index 0000000..c37c6e5
--- /dev/null
@@ -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