Skip to content

Commit

Permalink
Fix config loading for "hugo mod init"
Browse files Browse the repository at this point in the history
We have some commands that will continue even if the config loading fails (e.g. because a module can not be found). In Hugo 0.84.0 we introduced a new `_merge` flag that we removed once the configuration was loaded. But we did not do that in error situations, leading to failures in some situations.

This commit fixes that by making sure the configuration is always cleaned before return, even in error situations.

Fixes #8697
  • Loading branch information
bep committed Jun 27, 2021
1 parent d9bdd37 commit 923dd9d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
var configFiles []string

l := configLoader{ConfigSourceDescriptor: d, cfg: config.New()}
// Make sure we always do this, even in error situations,
// as we have commands (e.g. "hugo mod init") that will
// use a partial configuration to do its job.
defer l.deleteMergeStrategies()

for _, name := range d.configFilenames() {
var filename string
Expand Down Expand Up @@ -125,10 +129,7 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
collectHook := func(m *modules.ModulesConfig) error {
// We don't need the merge strategy configuration anymore,
// remove it so it doesn't accidentaly show up in other settings.
l.cfg.WalkParams(func(params ...config.KeyParams) bool {
params[len(params)-1].Params.DeleteMergeStrategy()
return false
})
l.deleteMergeStrategies()

if err := l.loadLanguageSettings(nil); err != nil {
return err
Expand Down Expand Up @@ -461,6 +462,13 @@ func (l configLoader) loadConfig(configName string) (string, error) {
return filename, nil
}

func (l configLoader) deleteMergeStrategies() {
l.cfg.WalkParams(func(params ...config.KeyParams) bool {
params[len(params)-1].Params.DeleteMergeStrategy()
return false
})
}

func (l configLoader) loadLanguageSettings(oldLangs langs.Languages) error {
_, err := langs.LoadLanguageSettings(l.cfg, oldLangs)
return err
Expand Down

0 comments on commit 923dd9d

Please sign in to comment.