]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl: Warn and skip non-hook templates inside /layouts/_markup
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 10 Apr 2025 13:57:29 +0000 (15:57 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 10 Apr 2025 14:54:05 +0000 (16:54 +0200)
Fixes #13577

hugolib/site.go
tpl/tplimpl/templatestore.go
tpl/tplimpl/templatestore_integration_test.go

index ebf06eee3925fc41b0333b4b0801431695d51b1f..728b036d20e13ead63d721d792b2ef78c771b200 100644 (file)
@@ -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(),
index f6a119e9b5a1c50334466fdc603304bb5b80a4f3..df4ea649f83d316b7761016156df09610cb16cb7 100644 (file)
@@ -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 {
index 2f38495621ee75177f322c97e390335584ac6043..8e62df1d0770f0a7392eb53c0920c6920007bb88 100644 (file)
@@ -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 --