]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
commands: Fix environment isolation for configuration settings
authorJoe Mooring <joe.mooring@veriphor.com>
Fri, 17 Apr 2026 02:38:24 +0000 (19:38 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 17 Apr 2026 07:40:23 +0000 (09:40 +0200)
Closes #14763

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
commands/commandeer.go
commands/hugobuilder.go
testscripts/commands/config_environment_isolation_issue14763.txt [new file with mode: 0644]

index 44ecda9fcb1477ff9452e576987b4993b2009c9e..ce86d10824b2162abfb55556633e3418be81d79f 100644 (file)
@@ -153,6 +153,23 @@ type rootCommand struct {
        cfgDir  string
 }
 
+// resolveEnvironment sets r.environment if not already set.
+// server indicates whether the server command is running (defaults to development).
+func (r *rootCommand) resolveEnvironment(server bool) {
+       if r.environment != "" {
+               return
+       }
+       if env := os.Getenv("HUGO_ENVIRONMENT"); env != "" {
+               r.environment = env
+       } else if env := os.Getenv("HUGO_ENV"); env != "" {
+               r.environment = env
+       } else if server {
+               r.environment = hugo.EnvironmentDevelopment
+       } else {
+               r.environment = hugo.EnvironmentProduction
+       }
+}
+
 func (r *rootCommand) isVerbose() bool {
        return r.logger.Level() <= logg.LevelInfo
 }
@@ -224,6 +241,7 @@ func (r *rootCommand) ConfigFromProvider(key configKey, cfg config.Provider) (*c
        if cfg == nil {
                panic("cfg must be set")
        }
+       r.resolveEnvironment(false)
        cc, _, err := r.commonConfigs.GetOrCreate(key, func(key configKey) (*commonConfig, error) {
                var dir string
                if r.source != "" {
index 4037f66117d836add66053503df18a6aee3f2a5e..563ec98bc5efa77a6c33fcd9c38753111889c399 100644 (file)
@@ -1082,22 +1082,8 @@ func (c *hugoBuilder) loadConfig(cd *simplecobra.Commandeer, running bool) error
        cfg := config.New()
        cfg.Set("renderToMemory", c.r.renderToMemory)
        watch := c.r.buildWatch || (c.s != nil && c.s.serverWatch)
-       if c.r.environment == "" {
-               // We need to set the environment as early as possible because we need it to load the correct config.
-               // Check if the user has set it in env.
-               if env := os.Getenv("HUGO_ENVIRONMENT"); env != "" {
-                       c.r.environment = env
-               } else if env := os.Getenv("HUGO_ENV"); env != "" {
-                       c.r.environment = env
-               } else {
-                       if c.s != nil {
-                               // The server defaults to development.
-                               c.r.environment = hugo.EnvironmentDevelopment
-                       } else {
-                               c.r.environment = hugo.EnvironmentProduction
-                       }
-               }
-       }
+       // We need to set the environment as early as possible because we need it to load the correct config.
+       c.r.resolveEnvironment(c.s != nil)
        cfg.Set("environment", c.r.environment)
 
        cfg.Set("internal", hmaps.Params{
diff --git a/testscripts/commands/config_environment_isolation_issue14763.txt b/testscripts/commands/config_environment_isolation_issue14763.txt
new file mode 100644 (file)
index 0000000..3e07d1d
--- /dev/null
@@ -0,0 +1,23 @@
+# Issue 14763
+
+# The staging config should not apply.
+hugo config
+! stdout 'myparam'
+mkdir content
+hugo new content foo.md
+hugo -DF
+! grep 'foo' public/foo/index.html
+
+# The staging config should apply.
+hugo config -e staging
+stdout 'myparam'
+hugo new content bar.md -e staging
+hugo -DF -e staging
+grep 'myparam: foo|' public/bar/index.html
+
+-- config/_default/hugo.toml --
+baseURL = "https://example.com"
+-- config/staging/params.toml --
+myparam = "foo"
+-- layouts/single.html --
+myparam: {{ site.Params.myparam }}|