diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2025-04-10 15:57:29 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2025-04-10 16:54:05 +0200 |
commit | 383dd82f9532c0d95e51d8e10b99d622ba9c8a22 (patch) | |
tree | ee936efa487d6c4c18114474606bcc4c5c6f2c21 | |
parent | 653f1c1d462332bccf303a5040e5cd99c89a4378 (diff) | |
download | hugo-383dd82f9532c0d95e51d8e10b99d622ba9c8a22.tar.gz hugo-383dd82f9532c0d95e51d8e10b99d622ba9c8a22.zip |
tpl: Warn and skip non-hook templates inside /layouts/_markup
Fixes #13577
-rw-r--r-- | hugolib/site.go | 1 | ||||
-rw-r--r-- | tpl/tplimpl/templatestore.go | 28 | ||||
-rw-r--r-- | tpl/tplimpl/templatestore_integration_test.go | 15 |
3 files changed, 39 insertions, 5 deletions
diff --git a/hugolib/site.go b/hugolib/site.go index ebf06eee3..728b036d2 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -396,6 +396,7 @@ func newHugoSites(cfg deps.DepsCfg, d *deps.Deps, pageTrees *pageTrees, sites [] templateStore, err := tplimpl.NewStore( tplimpl.StoreOptions{ Fs: s.BaseFs.Layouts.Fs, + Log: s.Log, DefaultContentLanguage: s.Conf.DefaultContentLanguage(), Watching: s.Conf.Watching(), PathParser: s.Conf.PathParser(), diff --git a/tpl/tplimpl/templatestore.go b/tpl/tplimpl/templatestore.go index f6a119e9b..df4ea649f 100644 --- a/tpl/tplimpl/templatestore.go +++ b/tpl/tplimpl/templatestore.go @@ -20,6 +20,7 @@ import ( "time" "github.com/gohugoio/hugo/common/herrors" + "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/common/paths" "github.com/gohugoio/hugo/helpers" @@ -156,6 +157,9 @@ type StoreOptions struct { // The filesystem to use. Fs afero.Fs + // The logger to use. + Log loggers.Logger + // The path parser to use. PathParser *paths.PathParser @@ -986,7 +990,10 @@ func (s *TemplateStore) setTemplateByPath(p string, ti *TemplInfo) { } func (s *TemplateStore) insertShortcode(pi *paths.Path, fi hugofs.FileMetaInfo, replace bool, tree doctree.Tree[map[string]map[TemplateDescriptor]*TemplInfo]) (*TemplInfo, error) { - k1, k2, _, d := s.toKeyCategoryAndDescriptor(pi) + k1, k2, _, d, err := s.toKeyCategoryAndDescriptor(pi) + if err != nil { + return nil, err + } m := tree.Get(k1) if m == nil { m = make(map[string]map[TemplateDescriptor]*TemplInfo) @@ -1027,7 +1034,18 @@ func (s *TemplateStore) insertShortcode(pi *paths.Path, fi hugofs.FileMetaInfo, } func (s *TemplateStore) insertTemplate(pi *paths.Path, fi hugofs.FileMetaInfo, replace bool, tree doctree.Tree[map[nodeKey]*TemplInfo]) (*TemplInfo, error) { - key, _, category, d := s.toKeyCategoryAndDescriptor(pi) + key, _, category, d, err := s.toKeyCategoryAndDescriptor(pi) + // See #13577. Warn for now. + if err != nil { + var loc string + if fi != nil { + loc = fmt.Sprintf("file %q", fi.Meta().Filename) + } else { + loc = fmt.Sprintf("path %q", pi.Path()) + } + s.opts.Log.Warnf("skipping template %s: %s", loc, err) + return nil, nil + } return s.insertTemplate2(pi, fi, key, category, d, replace, false, tree) } @@ -1481,7 +1499,7 @@ func (s *TemplateStore) templates() iter.Seq[*TemplInfo] { } } -func (s *TemplateStore) toKeyCategoryAndDescriptor(p *paths.Path) (string, string, Category, TemplateDescriptor) { +func (s *TemplateStore) toKeyCategoryAndDescriptor(p *paths.Path) (string, string, Category, TemplateDescriptor, error) { k1 := p.Dir() k2 := "" @@ -1575,7 +1593,7 @@ func (s *TemplateStore) toKeyCategoryAndDescriptor(p *paths.Path) (string, strin k1 = strings.TrimSuffix(k1, "/_markup") parts := strings.Split(d.Layout, "-") if len(parts) < 2 { - panic("markup template must have at least 2 parts") + return "", "", 0, TemplateDescriptor{}, fmt.Errorf("unrecognized render hook template") } // Either 2 or 3 parts, e.g. render-codeblock-go. d.Variant1 = parts[1] @@ -1585,7 +1603,7 @@ func (s *TemplateStore) toKeyCategoryAndDescriptor(p *paths.Path) (string, strin d.Layout = "" // This allows using page layout as part of the key for lookups. } - return k1, k2, category, d + return k1, k2, category, d, nil } func (s *TemplateStore) transformTemplates() error { diff --git a/tpl/tplimpl/templatestore_integration_test.go b/tpl/tplimpl/templatestore_integration_test.go index 2f3849562..8e62df1d0 100644 --- a/tpl/tplimpl/templatestore_integration_test.go +++ b/tpl/tplimpl/templatestore_integration_test.go @@ -844,6 +844,21 @@ All. b.AssertLogContains("Duplicate content path") } +// Issue #13577. +func TestPrintPathWarningOnInvalidMarkupFilename(t *testing.T) { + t.Parallel() + + files := ` +-- hugo.toml -- +-- layouts/all.html -- +All. +-- layouts/_markup/sitemap.xml -- +` + b := hugolib.Test(t, files, hugolib.TestOptWarn()) + + b.AssertLogContains("unrecognized render hook") +} + func BenchmarkExecuteWithContext(b *testing.B) { files := ` -- hugo.toml -- |