From: Joe Mooring Date: Fri, 17 Apr 2026 02:38:24 +0000 (-0700) Subject: commands: Fix environment isolation for configuration settings X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=1eea9fba0b7ff174d1e30b2307bef54842f422fc;p=brevno-suite%2Fhugo commands: Fix environment isolation for configuration settings Closes #14763 Co-authored-by: Claude Sonnet 4.6 --- diff --git a/commands/commandeer.go b/commands/commandeer.go index 44ecda9fc..ce86d1082 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -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 != "" { diff --git a/commands/hugobuilder.go b/commands/hugobuilder.go index 4037f6611..563ec98bc 100644 --- a/commands/hugobuilder.go +++ b/commands/hugobuilder.go @@ -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 index 000000000..3e07d1d23 --- /dev/null +++ b/testscripts/commands/config_environment_isolation_issue14763.txt @@ -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 }}|