Skip to content

Commit

Permalink
Handle transient errors in config loading etc.
Browse files Browse the repository at this point in the history
As in: Get the Kubernetes site to build with the new Hugo version.

Updates gohugoio#10947
  • Loading branch information
bep committed May 17, 2023
1 parent e48c3ae commit 6e2cadb
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 3 deletions.
20 changes: 19 additions & 1 deletion config/allconfig/allconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func (c Config) cloneForLang() *Config {
}

func (c *Config) CompileConfig() error {
var transientErr error
s := c.Timeout
if _, err := strconv.Atoi(s); err == nil {
// A number, assume seconds.
Expand Down Expand Up @@ -209,7 +210,8 @@ func (c *Config) CompileConfig() error {
}
f, found := outputFormats.GetByName(format)
if !found {
return fmt.Errorf("unknown output format %q for kind %q", format, kind)
transientErr = fmt.Errorf("unknown output format %q for kind %q", format, kind)
continue
}
kindOutputFormats[kind] = append(kindOutputFormats[kind], f)
}
Expand Down Expand Up @@ -288,6 +290,7 @@ func (c *Config) CompileConfig() error {
IgnoreFile: ignoreFile,
MainSections: c.MainSections,
Clock: clock,
transientErr: transientErr,
}

for _, s := range allDecoderSetups {
Expand Down Expand Up @@ -323,6 +326,11 @@ type ConfigCompiled struct {
IgnoreFile func(filename string) bool
MainSections []string
Clock time.Time

// This is set to the last transient error found during config compilation.
// With themes/modules we compule the configuration in multiple passes, and
// errors with missing output format definitions may resolve itself.
transientErr error
}

// This may be set after the config is compiled.
Expand Down Expand Up @@ -565,6 +573,16 @@ type Configs struct {
configLangs []config.AllProvider
}

// transientErr returns the last transient error found during config compilation.
func (c *Configs) transientErr() error {
for _, l := range c.LanguageConfigSlice {
if l.C.transientErr != nil {
return l.C.transientErr
}
}
return nil
}

func (c *Configs) IsZero() bool {
// A config always has at least one language.
return c == nil || len(c.Languages) == 0
Expand Down
8 changes: 7 additions & 1 deletion config/allconfig/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,19 @@ func LoadConfig(d ConfigSourceDescriptor) (*Configs, error) {
if err != nil {
return nil, fmt.Errorf("failed to load modules: %w", err)
}

if len(l.ModulesConfigFiles) > 0 {
// Config merged in from modules.
// Re-read the config.
configs, err = FromLoadConfigResult(d.Fs, res)
if err != nil {
return nil, fmt.Errorf("failed to create config: %w", err)
return nil, fmt.Errorf("failed to create config from modules config: %w", err)
}
if err := configs.transientErr(); err != nil {
return nil, fmt.Errorf("failed to create config from modules config: %w", err)
}
} else if err := configs.transientErr(); err != nil {
return nil, fmt.Errorf("failed to create config: %w", err)
}

configs.Modules = moduleConfig.ActiveModules
Expand Down
36 changes: 36 additions & 0 deletions hugolib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,42 @@ themeconfigdirparam: {{ site.Params.themeconfigdirparam }}

}

func TestConfigOutputFormatDefinedInTheme(t *testing.T) {
t.Parallel()

files := `
-- hugo.toml --
theme = "mytheme"
[outputFormats]
[outputFormats.myotherformat]
baseName = 'myotherindex'
mediaType = 'text/html'
[outputs]
home = ['myformat']
-- themes/mytheme/hugo.toml --
[outputFormats]
[outputFormats.myformat]
baseName = 'myindex'
mediaType = 'text/html'
-- layouts/index.html --
Home.
`

b, err := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
},
).BuildE()

b.Assert(err, qt.IsNil)
b.AssertFileContent("public/myindex.html", "Home.")

}

func TestReproCommentsIn10947(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion hugolib/site_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func (s *Site) GoogleAnalytics() string {
return s.Config().Services.GoogleAnalytics.ID
}

func (s *Site) Param(key string) (any, error) {
func (s *Site) Param(key any) (any, error) {
return resource.Param(s, nil, key)
}

Expand Down
4 changes: 4 additions & 0 deletions langs/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (l *Language) loadLocation(tzStr string) error {
return nil
}

func (l *Language) String() string {
return l.Lang
}

// Languages is a sortable list of languages.
type Languages []*Language

Expand Down
11 changes: 11 additions & 0 deletions resources/page/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ type Site interface {
// Returns the Params configured for this site.
Params() maps.Params

// Param is a convenience method to do lookups in Params.
Param(key any) (any, error)

// Returns a map of all the data inside /data.
Data() map[string]any

Expand Down Expand Up @@ -254,6 +257,10 @@ func (s *siteWrapper) Params() maps.Params {
return s.s.Params()
}

func (s *siteWrapper) Param(key any) (any, error) {
return s.s.Param(key)
}

func (s *siteWrapper) Data() map[string]any {
return s.s.Data()
}
Expand Down Expand Up @@ -410,6 +417,10 @@ func (s testSite) IsMultiLingual() bool {
return false
}

func (s testSite) Param(key any) (any, error) {
return nil, nil
}

// NewDummyHugoSite creates a new minimal test site.
func NewDummyHugoSite(cfg config.Provider) Site {
return testSite{
Expand Down

0 comments on commit 6e2cadb

Please sign in to comment.