Make hugo.toml the new config.toml

Both will of course work, but hugo.toml will win if both are set.

We should have done this a long time ago, of course, but the reason I'm picking this up now is that my VS Code setup by default picks up some
JSON config schema from some random other software which also names its config files config.toml.

Fixes #8979
This commit is contained in:
Bjørn Erik Pedersen 2023-01-15 18:45:51 +01:00
parent 6a579ebac3
commit f38a2fbd2e
7 changed files with 111 additions and 24 deletions

View file

@ -177,7 +177,7 @@ Complete documentation is available at https://gohugo.io/.`,
}, },
}) })
cc.cmd.PersistentFlags().StringVar(&cc.cfgFile, "config", "", "config file (default is path/config.yaml|json|toml)") cc.cmd.PersistentFlags().StringVar(&cc.cfgFile, "config", "", "config file (default is hugo.yaml|json|toml)")
cc.cmd.PersistentFlags().StringVar(&cc.cfgDir, "configDir", "config", "config dir") cc.cmd.PersistentFlags().StringVar(&cc.cfgDir, "configDir", "config", "config dir")
cc.cmd.PersistentFlags().BoolVar(&cc.quiet, "quiet", false, "build in quiet mode") cc.cmd.PersistentFlags().BoolVar(&cc.quiet, "quiet", false, "build in quiet mode")

View file

@ -83,6 +83,7 @@ func (n *newSiteCmd) doNewSite(fs *hugofs.Fs, basepath string, force bool) error
return errors.New(basepath + " already exists and is not empty. See --force.") return errors.New(basepath + " already exists and is not empty. See --force.")
case !isEmpty && force: case !isEmpty && force:
// TODO(bep) eventually rename this to hugo.
all := append(dirs, filepath.Join(basepath, "config."+n.configFormat)) all := append(dirs, filepath.Join(basepath, "config."+n.configFormat))
for _, path := range all { for _, path := range all {
if exists, _ := helpers.Exists(path, fs.Source); exists { if exists, _ := helpers.Exists(path, fs.Source); exists {

View file

@ -29,11 +29,22 @@ import (
) )
var ( var (
// See issue #8979 for context.
// Hugo has always used config.toml etc. as the default config file name.
// But hugo.toml is a more descriptive name, but we need to check for both.
DefaultConfigNames = []string{"hugo", "config"}
DefaultConfigNamesSet = make(map[string]bool)
ValidConfigFileExtensions = []string{"toml", "yaml", "yml", "json"} ValidConfigFileExtensions = []string{"toml", "yaml", "yml", "json"}
validConfigFileExtensionsMap map[string]bool = make(map[string]bool) validConfigFileExtensionsMap map[string]bool = make(map[string]bool)
) )
func init() { func init() {
for _, name := range DefaultConfigNames {
DefaultConfigNamesSet[name] = true
}
for _, ext := range ValidConfigFileExtensions { for _, ext := range ValidConfigFileExtensions {
validConfigFileExtensionsMap[ext] = true validConfigFileExtensionsMap[ext] = true
} }
@ -142,8 +153,7 @@ func LoadConfigFromDir(sourceFs afero.Fs, configDir, environment string) (Provid
} }
var keyPath []string var keyPath []string
if !DefaultConfigNamesSet[name] {
if name != "config" {
// Can be params.jp, menus.en etc. // Can be params.jp, menus.en etc.
name, lang := paths.FileAndExtNoDelimiter(name) name, lang := paths.FileAndExtNoDelimiter(name)

View file

@ -69,7 +69,10 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
// use a partial configuration to do its job. // use a partial configuration to do its job.
defer l.deleteMergeStrategies() defer l.deleteMergeStrategies()
for _, name := range d.configFilenames() { names := d.configFilenames()
if names != nil {
for _, name := range names {
var filename string var filename string
filename, err := l.loadConfig(name) filename, err := l.loadConfig(name)
if err == nil { if err == nil {
@ -78,9 +81,23 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
return nil, nil, l.wrapFileError(err, filename) return nil, nil, l.wrapFileError(err, filename)
} }
} }
} else {
for _, name := range config.DefaultConfigNames {
var filename string
filename, err := l.loadConfig(name)
if err == nil {
configFiles = append(configFiles, filename)
break
} else if err != ErrNoConfigFile {
return nil, nil, l.wrapFileError(err, filename)
}
}
}
if d.AbsConfigDir != "" { if d.AbsConfigDir != "" {
dcfg, dirnames, err := config.LoadConfigFromDir(l.Fs, d.AbsConfigDir, l.Environment) dcfg, dirnames, err := config.LoadConfigFromDir(l.Fs, d.AbsConfigDir, l.Environment)
if err == nil { if err == nil {
if len(dirnames) > 0 { if len(dirnames) > 0 {
l.cfg.Set("", dcfg.Get("")) l.cfg.Set("", dcfg.Get(""))
@ -162,9 +179,9 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
return l.cfg, configFiles, err return l.cfg, configFiles, err
} }
// LoadConfigDefault is a convenience method to load the default "config.toml" config. // LoadConfigDefault is a convenience method to load the default "hugo.toml" config.
func LoadConfigDefault(fs afero.Fs) (config.Provider, error) { func LoadConfigDefault(fs afero.Fs) (config.Provider, error) {
v, _, err := LoadConfig(ConfigSourceDescriptor{Fs: fs, Filename: "config.toml"}) v, _, err := LoadConfig(ConfigSourceDescriptor{Fs: fs})
return v, err return v, err
} }
@ -202,7 +219,7 @@ func (d ConfigSourceDescriptor) configFileDir() string {
func (d ConfigSourceDescriptor) configFilenames() []string { func (d ConfigSourceDescriptor) configFilenames() []string {
if d.Filename == "" { if d.Filename == "" {
return []string{"config"} return nil
} }
return strings.Split(d.Filename, ",") return strings.Split(d.Filename, ",")
} }

View file

@ -782,3 +782,55 @@ defaultMarkdownHandler = 'blackfriday'
b.Assert(err.Error(), qt.Contains, "Configured defaultMarkdownHandler \"blackfriday\" not found. Did you mean to use goldmark? Blackfriday was removed in Hugo v0.100.0.") b.Assert(err.Error(), qt.Contains, "Configured defaultMarkdownHandler \"blackfriday\" not found. Did you mean to use goldmark? Blackfriday was removed in Hugo v0.100.0.")
} }
// Issue 8979
func TestHugoConfig(t *testing.T) {
filesTemplate := `
-- hugo.toml --
theme = "mytheme"
[params]
rootparam = "rootvalue"
-- config/_default/hugo.toml --
[params]
rootconfigparam = "rootconfigvalue"
-- themes/mytheme/config/_default/hugo.toml --
[params]
themeconfigdirparam = "themeconfigdirvalue"
-- themes/mytheme/hugo.toml --
[params]
themeparam = "themevalue"
-- layouts/index.html --
rootparam: {{ site.Params.rootparam }}
rootconfigparam: {{ site.Params.rootconfigparam }}
themeparam: {{ site.Params.themeparam }}
themeconfigdirparam: {{ site.Params.themeconfigdirparam }}
`
for _, configName := range []string{"hugo.toml", "config.toml"} {
configName := configName
t.Run(configName, func(t *testing.T) {
t.Parallel()
files := strings.ReplaceAll(filesTemplate, "hugo.toml", configName)
b, err := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
},
).BuildE()
b.Assert(err, qt.IsNil)
b.AssertFileContent("public/index.html",
"rootparam: rootvalue",
"rootconfigparam: rootconfigvalue",
"themeparam: themevalue",
"themeconfigdirparam: themeconfigdirvalue",
)
})
}
}

View file

@ -301,13 +301,18 @@ func (s *IntegrationTestBuilder) initBuilder() error {
s.Assert(afero.WriteFile(afs, filename, data, 0666), qt.IsNil) s.Assert(afero.WriteFile(afs, filename, data, 0666), qt.IsNil)
} }
configDirFilename := filepath.Join(s.Cfg.WorkingDir, "config")
if _, err := afs.Stat(configDirFilename); err != nil {
configDirFilename = ""
}
cfg, _, err := LoadConfig( cfg, _, err := LoadConfig(
ConfigSourceDescriptor{ ConfigSourceDescriptor{
WorkingDir: s.Cfg.WorkingDir, WorkingDir: s.Cfg.WorkingDir,
AbsConfigDir: configDirFilename,
Fs: afs, Fs: afs,
Logger: logger, Logger: logger,
Environ: []string{}, Environ: []string{},
Filename: "config.toml",
}, },
func(cfg config.Provider) error { func(cfg config.Provider) error {
return nil return nil

View file

@ -423,12 +423,14 @@ func (c *collector) applyThemeConfig(tc *moduleAdapter) error {
err error err error
) )
// Viper supports more, but this is the sub-set supported by Hugo. LOOP:
for _, configBaseName := range config.DefaultConfigNames {
for _, configFormats := range config.ValidConfigFileExtensions { for _, configFormats := range config.ValidConfigFileExtensions {
configFilename = filepath.Join(tc.Dir(), "config."+configFormats) configFilename = filepath.Join(tc.Dir(), configBaseName+"."+configFormats)
hasConfigFile, _ = afero.Exists(c.fs, configFilename) hasConfigFile, _ = afero.Exists(c.fs, configFilename)
if hasConfigFile { if hasConfigFile {
break break LOOP
}
} }
} }