Skip to content

Commit 383dd82

Browse files
committed
tpl: Warn and skip non-hook templates inside /layouts/_markup
Fixes #13577
1 parent 653f1c1 commit 383dd82

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

Diff for: hugolib/site.go

+1
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ func newHugoSites(cfg deps.DepsCfg, d *deps.Deps, pageTrees *pageTrees, sites []
396396
templateStore, err := tplimpl.NewStore(
397397
tplimpl.StoreOptions{
398398
Fs: s.BaseFs.Layouts.Fs,
399+
Log: s.Log,
399400
DefaultContentLanguage: s.Conf.DefaultContentLanguage(),
400401
Watching: s.Conf.Watching(),
401402
PathParser: s.Conf.PathParser(),

Diff for: tpl/tplimpl/templatestore.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"time"
2121

2222
"github.com/gohugoio/hugo/common/herrors"
23+
"github.com/gohugoio/hugo/common/loggers"
2324
"github.com/gohugoio/hugo/common/maps"
2425
"github.com/gohugoio/hugo/common/paths"
2526
"github.com/gohugoio/hugo/helpers"
@@ -156,6 +157,9 @@ type StoreOptions struct {
156157
// The filesystem to use.
157158
Fs afero.Fs
158159

160+
// The logger to use.
161+
Log loggers.Logger
162+
159163
// The path parser to use.
160164
PathParser *paths.PathParser
161165

@@ -986,7 +990,10 @@ func (s *TemplateStore) setTemplateByPath(p string, ti *TemplInfo) {
986990
}
987991

988992
func (s *TemplateStore) insertShortcode(pi *paths.Path, fi hugofs.FileMetaInfo, replace bool, tree doctree.Tree[map[string]map[TemplateDescriptor]*TemplInfo]) (*TemplInfo, error) {
989-
k1, k2, _, d := s.toKeyCategoryAndDescriptor(pi)
993+
k1, k2, _, d, err := s.toKeyCategoryAndDescriptor(pi)
994+
if err != nil {
995+
return nil, err
996+
}
990997
m := tree.Get(k1)
991998
if m == nil {
992999
m = make(map[string]map[TemplateDescriptor]*TemplInfo)
@@ -1027,7 +1034,18 @@ func (s *TemplateStore) insertShortcode(pi *paths.Path, fi hugofs.FileMetaInfo,
10271034
}
10281035

10291036
func (s *TemplateStore) insertTemplate(pi *paths.Path, fi hugofs.FileMetaInfo, replace bool, tree doctree.Tree[map[nodeKey]*TemplInfo]) (*TemplInfo, error) {
1030-
key, _, category, d := s.toKeyCategoryAndDescriptor(pi)
1037+
key, _, category, d, err := s.toKeyCategoryAndDescriptor(pi)
1038+
// See #13577. Warn for now.
1039+
if err != nil {
1040+
var loc string
1041+
if fi != nil {
1042+
loc = fmt.Sprintf("file %q", fi.Meta().Filename)
1043+
} else {
1044+
loc = fmt.Sprintf("path %q", pi.Path())
1045+
}
1046+
s.opts.Log.Warnf("skipping template %s: %s", loc, err)
1047+
return nil, nil
1048+
}
10311049

10321050
return s.insertTemplate2(pi, fi, key, category, d, replace, false, tree)
10331051
}
@@ -1481,7 +1499,7 @@ func (s *TemplateStore) templates() iter.Seq[*TemplInfo] {
14811499
}
14821500
}
14831501

1484-
func (s *TemplateStore) toKeyCategoryAndDescriptor(p *paths.Path) (string, string, Category, TemplateDescriptor) {
1502+
func (s *TemplateStore) toKeyCategoryAndDescriptor(p *paths.Path) (string, string, Category, TemplateDescriptor, error) {
14851503
k1 := p.Dir()
14861504
k2 := ""
14871505

@@ -1575,7 +1593,7 @@ func (s *TemplateStore) toKeyCategoryAndDescriptor(p *paths.Path) (string, strin
15751593
k1 = strings.TrimSuffix(k1, "/_markup")
15761594
parts := strings.Split(d.Layout, "-")
15771595
if len(parts) < 2 {
1578-
panic("markup template must have at least 2 parts")
1596+
return "", "", 0, TemplateDescriptor{}, fmt.Errorf("unrecognized render hook template")
15791597
}
15801598
// Either 2 or 3 parts, e.g. render-codeblock-go.
15811599
d.Variant1 = parts[1]
@@ -1585,7 +1603,7 @@ func (s *TemplateStore) toKeyCategoryAndDescriptor(p *paths.Path) (string, strin
15851603
d.Layout = "" // This allows using page layout as part of the key for lookups.
15861604
}
15871605

1588-
return k1, k2, category, d
1606+
return k1, k2, category, d, nil
15891607
}
15901608

15911609
func (s *TemplateStore) transformTemplates() error {

Diff for: tpl/tplimpl/templatestore_integration_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,21 @@ All.
844844
b.AssertLogContains("Duplicate content path")
845845
}
846846

847+
// Issue #13577.
848+
func TestPrintPathWarningOnInvalidMarkupFilename(t *testing.T) {
849+
t.Parallel()
850+
851+
files := `
852+
-- hugo.toml --
853+
-- layouts/all.html --
854+
All.
855+
-- layouts/_markup/sitemap.xml --
856+
`
857+
b := hugolib.Test(t, files, hugolib.TestOptWarn())
858+
859+
b.AssertLogContains("unrecognized render hook")
860+
}
861+
847862
func BenchmarkExecuteWithContext(b *testing.B) {
848863
files := `
849864
-- hugo.toml --

0 commit comments

Comments
 (0)