Fix OS env override for nested config param only available in theme
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 19 Mar 2021 10:05:17 +0000 (11:05 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 20 Mar 2021 12:34:51 +0000 (13:34 +0100)
Fixes #8346

hugolib/config.go
hugolib/config_test.go

index 5ef78acf409fb02c0c3fcaa3cff5256d10c2b302..fe138bf2037a2533904984a0cd0b87ac52819a1c 100644 (file)
@@ -18,6 +18,7 @@ import (
        "path/filepath"
        "strings"
 
+       "github.com/gohugoio/hugo/common/maps"
        "github.com/gohugoio/hugo/common/types"
 
        "github.com/gobwas/glob"
@@ -27,8 +28,6 @@ import (
 
        "github.com/gohugoio/hugo/cache/filecache"
 
-       "github.com/gohugoio/hugo/common/maps"
-
        "github.com/gohugoio/hugo/parser/metadecoders"
 
        "github.com/gohugoio/hugo/common/herrors"
@@ -167,6 +166,40 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
                }
        }
 
+       // We made this a Glob pattern in Hugo 0.75, we don't need both.
+       if v.GetBool("ignoreVendor") {
+               helpers.Deprecated("--ignoreVendor", "--ignoreVendorPaths **", false)
+               v.Set("ignoreVendorPaths", "**")
+       }
+
+       modulesConfig, err := l.loadModulesConfig(v)
+       if err != nil {
+               return v, configFiles, err
+       }
+
+       // Need to run these after the modules are loaded, but before
+       // they are finalized.
+       collectHook := func(m *modules.ModulesConfig) error {
+               if err := loadLanguageSettings(v, nil); err != nil {
+                       return err
+               }
+
+               mods := m.ActiveModules
+
+               // Apply default project mounts.
+               if err := modules.ApplyProjectConfigDefaults(v, mods[0]); err != nil {
+                       return err
+               }
+
+               return nil
+       }
+
+       _, modulesConfigFiles, err := l.collectModules(modulesConfig, v, collectHook)
+
+       if err == nil && len(modulesConfigFiles) > 0 {
+               configFiles = append(configFiles, modulesConfigFiles...)
+       }
+
        const delim = "__env__delim"
 
        // Apply environment overrides
@@ -222,40 +255,6 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
 
        }
 
-       // We made this a Glob pattern in Hugo 0.75, we don't need both.
-       if v.GetBool("ignoreVendor") {
-               helpers.Deprecated("--ignoreVendor", "--ignoreVendorPaths **", false)
-               v.Set("ignoreVendorPaths", "**")
-       }
-
-       modulesConfig, err := l.loadModulesConfig(v)
-       if err != nil {
-               return v, configFiles, err
-       }
-
-       // Need to run these after the modules are loaded, but before
-       // they are finalized.
-       collectHook := func(m *modules.ModulesConfig) error {
-               if err := loadLanguageSettings(v, nil); err != nil {
-                       return err
-               }
-
-               mods := m.ActiveModules
-
-               // Apply default project mounts.
-               if err := modules.ApplyProjectConfigDefaults(v, mods[0]); err != nil {
-                       return err
-               }
-
-               return nil
-       }
-
-       _, modulesConfigFiles, err := l.collectModules(modulesConfig, v, collectHook)
-
-       if err == nil && len(modulesConfigFiles) > 0 {
-               configFiles = append(configFiles, modulesConfigFiles...)
-       }
-
        return v, configFiles, err
 }
 
index ecb45006701ef053229d453650c7944d813aeb8a..75f5b9786a6bec2c8ccf1e7010bda6a7eb404257 100644 (file)
@@ -483,6 +483,7 @@ func TestLoadConfigWithOsEnvOverrides(t *testing.T) {
 
        baseConfig := `
 
+theme = "mytheme"
 environment = "production"
 enableGitInfo = true
 intSlice = [5,7,9]
@@ -501,6 +502,16 @@ quality = 75
 
        b := newTestSitesBuilder(t).WithConfigFile("toml", baseConfig)
 
+       b.WithSourceFile("themes/mytheme/config.toml", `
+
+[params]
+[params.mytheme_section]
+theme_param="themevalue"
+[params.mytheme_section2]
+theme_param="themevalue2"
+
+`)
+
        b.WithEnviron(
                "HUGO_ENVIRONMENT", "test",
                "HUGO_NEW", "new", // key not in config.toml
@@ -510,10 +521,13 @@ quality = 75
                "HUGO_STRINGSLICE", `["c", "d"]`,
                "HUGO_INTSLICE", `[5, 8, 9]`,
                "HUGO_FLOATSLICE", `[5.32]`,
-               // https://github.com/gohugoio/hugo/issues/7829
+               // Issue #7829
                "HUGOxPARAMSxAPI_CONFIGxAPI_KEY", "new_key",
                // Delimiters are case sensitive.
                "HUGOxPARAMSxAPI_CONFIGXANOTHER_KEY", "another_key",
+               // Issue #8346
+               "HUGOxPARAMSxMYTHEME_SECTIONxTHEME_PARAM", "themevalue_changed",
+               "HUGOxPARAMSxMYTHEME_SECTION2xTHEME_PARAM", "themevalue2_changed",
        )
 
        b.Build(BuildCfg{})
@@ -531,4 +545,6 @@ quality = 75
        c.Assert(cfg.Get("intSlice"), qt.DeepEquals, []interface{}{5, 8, 9})
        c.Assert(cfg.Get("params.api_config.api_key"), qt.Equals, "new_key")
        c.Assert(cfg.Get("params.api_config.another_key"), qt.Equals, "default another_key")
+       c.Assert(cfg.Get("params.mytheme_section.theme_param"), qt.Equals, "themevalue_changed")
+       c.Assert(cfg.Get("params.mytheme_section2.theme_param"), qt.Equals, "themevalue2_changed")
 }