commands: Fix environment isolation for configuration settings

Closes #14763

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Joe Mooring
2026-04-16 19:38:24 -07:00
committed by Bjørn Erik Pedersen
parent 9a5c7e0d24
commit 1eea9fba0b
3 changed files with 43 additions and 16 deletions
+18
View 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 != "" {
+2 -16
View 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{
@@ -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 }}|