Skip to content

Commit 60a2cdf

Browse files
authoredJun 13, 2023
Fix config merge regression with root slices (e.g. disableKinds)
Fixes #11089
1 parent e08cfc8 commit 60a2cdf

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
 

‎config/allconfig/allconfig.go

+18
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,22 @@ type configCompiler interface {
191191
func (c Config) cloneForLang() *Config {
192192
x := c
193193
x.C = nil
194+
copyStringSlice := func(in []string) []string {
195+
if in == nil {
196+
return nil
197+
}
198+
out := make([]string, len(in))
199+
copy(out, in)
200+
return out
201+
}
202+
203+
// Copy all the slices to avoid sharing.
204+
x.DisableKinds = copyStringSlice(x.DisableKinds)
205+
x.DisableLanguages = copyStringSlice(x.DisableLanguages)
206+
x.MainSections = copyStringSlice(x.MainSections)
207+
x.IgnoreErrors = copyStringSlice(x.IgnoreErrors)
208+
x.IgnoreFiles = copyStringSlice(x.IgnoreFiles)
209+
x.Theme = copyStringSlice(x.Theme)
194210

195211
// Collapse all static dirs to one.
196212
x.StaticDir = x.staticDirs()
@@ -787,12 +803,14 @@ func fromLoadConfigResult(fs afero.Fs, logger loggers.Logger, res config.LoadCon
787803

788804
// Create a copy of the complete config and replace the root keys with the language specific ones.
789805
clone := all.cloneForLang()
806+
790807
if err := decodeConfigFromParams(fs, bcfg, mergedConfig, clone, differentRootKeys); err != nil {
791808
return nil, fmt.Errorf("failed to decode config for language %q: %w", k, err)
792809
}
793810
if err := clone.CompileConfig(logger); err != nil {
794811
return nil, err
795812
}
813+
796814
langConfigMap[k] = clone
797815
case maps.ParamsMergeStrategy:
798816
default:

‎hugolib/config_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,74 @@ author: {{ site.Author }}
750750

751751
}
752752

753+
// Issue #11089
754+
func TestHugoConfigSliceOverrides(t *testing.T) {
755+
t.Parallel()
756+
757+
filesTemplate := `
758+
-- hugo.toml --
759+
disableKinds = ["section"]
760+
[languages]
761+
[languages.en]
762+
disableKinds = []
763+
title = "English"
764+
weigHt = WEIGHT_EN
765+
[languages.sv]
766+
title = "Swedish"
767+
wEight = WEIGHT_SV
768+
disableKinds = ["page"]
769+
-- layouts/index.html --
770+
Home: {{ .Lang}}|{{ len site.RegularPages }}|
771+
-- layouts/_default/single.html --
772+
Single.
773+
-- content/p1.en.md --
774+
-- content/p2.en.md --
775+
-- content/p1.sv.md --
776+
-- content/p2.sv.md --
777+
778+
`
779+
780+
t.Run("En first", func(t *testing.T) {
781+
files := strings.ReplaceAll(filesTemplate, "WEIGHT_EN", "1")
782+
files = strings.ReplaceAll(files, "WEIGHT_SV", "2")
783+
784+
cfg := config.New()
785+
b, err := NewIntegrationTestBuilder(
786+
IntegrationTestConfig{
787+
T: t,
788+
TxtarString: files,
789+
BaseCfg: cfg,
790+
},
791+
).BuildE()
792+
793+
b.Assert(err, qt.IsNil)
794+
b.AssertFileContent("public/index.html", "Home: en|2|")
795+
b.AssertFileContent("public/sv/index.html", "Home: sv|0|")
796+
797+
})
798+
799+
t.Run("Sv first", func(t *testing.T) {
800+
files := strings.ReplaceAll(filesTemplate, "WEIGHT_EN", "2")
801+
files = strings.ReplaceAll(files, "WEIGHT_SV", "1")
802+
803+
for i := 0; i < 20; i++ {
804+
cfg := config.New()
805+
b, err := NewIntegrationTestBuilder(
806+
IntegrationTestConfig{
807+
T: t,
808+
TxtarString: files,
809+
BaseCfg: cfg,
810+
},
811+
).BuildE()
812+
813+
b.Assert(err, qt.IsNil)
814+
b.AssertFileContent("public/index.html", "Home: en|2|")
815+
b.AssertFileContent("public/sv/index.html", "Home: sv|0|")
816+
}
817+
})
818+
819+
}
820+
753821
func TestConfigOutputFormatDefinedInTheme(t *testing.T) {
754822
t.Parallel()
755823

0 commit comments

Comments
 (0)
Please sign in to comment.