Skip to content

Commit a5189ed

Browse files
committed
tpl/collections: Include template path in IsSet unsupported type warning
When isset is called on a value that's not an array, channel, slice, or map, Hugo logs a warning that says only "calling IsSet with unsupported type X (Y) will always return false." On any site with more than one template, there's no way to find the offending call site. Thread context through IsSet and surface the executing template's filename in the warning. Switch from Log.Warnf to Log.Warnidf so the warning is grouped under an ID that users can suppress via ignoreLogs. Fixes #11794
1 parent 4ed7600 commit a5189ed

5 files changed

Lines changed: 33 additions & 3 deletions

File tree

common/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
WarnGoldmarkRawHTML = "warning-goldmark-raw-html"
2121
WarnPartialSuperfluousPrefix = "warning-partial-superfluous-prefix"
2222
WarnHomePageIsLeafBundle = "warning-home-page-is-leaf-bundle"
23+
WarnIsSetUnsupportedType = "warning-isset-unsupported-type"
2324
)
2425

2526
// Field/method names with special meaning.

docs/data/docs.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,6 +2376,7 @@ tpl:
23762376
- isSet
23772377
- isset
23782378
Args:
2379+
- ctx
23792380
- c
23802381
- key
23812382
Description: |-

tpl/collections/collections.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@ import (
2020
"errors"
2121
"fmt"
2222
"math/rand/v2"
23+
"path/filepath"
2324
"reflect"
2425
"strings"
2526
"time"
2627

2728
"github.com/gohugoio/hugo/common/collections"
29+
"github.com/gohugoio/hugo/common/constants"
2830
"github.com/gohugoio/hugo/common/hmaps"
2931
"github.com/gohugoio/hugo/common/hreflect"
3032
"github.com/gohugoio/hugo/common/hstore"
3133
"github.com/gohugoio/hugo/common/types"
3234
"github.com/gohugoio/hugo/deps"
3335
"github.com/gohugoio/hugo/langs"
36+
"github.com/gohugoio/hugo/tpl"
3437
"github.com/gohugoio/hugo/tpl/compare"
3538
"github.com/spf13/cast"
3639
)
@@ -339,7 +342,7 @@ func (ns *Namespace) Group(key any, items any) (any, error) {
339342

340343
// IsSet returns whether a given array, channel, slice, or map in c has the given key
341344
// defined.
342-
func (ns *Namespace) IsSet(c any, key any) (bool, error) {
345+
func (ns *Namespace) IsSet(ctx context.Context, c any, key any) (bool, error) {
343346
av := reflect.ValueOf(c)
344347
kv := reflect.ValueOf(key)
345348

@@ -357,7 +360,15 @@ func (ns *Namespace) IsSet(c any, key any) (bool, error) {
357360
return av.MapIndex(kv).IsValid(), nil
358361
}
359362
default:
360-
ns.deps.Log.Warnf("calling IsSet with unsupported type %q (%T) will always return false.\n", av.Kind(), c)
363+
var where string
364+
if currentTpl := tpl.Context.CurrentTemplate.Get(ctx); currentTpl != nil {
365+
if f := currentTpl.Filename(); f != "" {
366+
where = fmt.Sprintf(" in %q", filepath.ToSlash(f))
367+
} else if n := currentTpl.Name(); n != "" {
368+
where = fmt.Sprintf(" in template %q", n)
369+
}
370+
}
371+
ns.deps.Log.Warnidf(constants.WarnIsSetUnsupportedType, "calling IsSet with unsupported type %q (%T)%s will always return false.", av.Kind(), c, where)
361372
}
362373

363374
return false, nil

tpl/collections/collections_integration_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,20 @@ index: {{ index $d "foo" }}|
671671

672672
hugolib.Test(t, files).AssertFileContent("public/index.html", "dict: map[string]interface {} true 0", "! FAIL", "index: |", "d3: map[foo:bar]|")
673673
}
674+
675+
// Issue 11794
676+
func TestIsSetUnsupportedTypeWarningIncludesTemplatePath(t *testing.T) {
677+
t.Parallel()
678+
679+
files := `
680+
-- hugo.toml --
681+
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
682+
-- layouts/home.html --
683+
{{ $s := "hello" }}
684+
{{ if isset $s "anyKey" }}A{{ else }}B{{ end }}
685+
`
686+
687+
b := hugolib.Test(t, files, hugolib.TestOptWarn())
688+
689+
b.AssertLogContains(`WARN calling IsSet with unsupported type "string" (string) in "/layouts/home.html" will always return false.`)
690+
}

tpl/collections/collections_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ func TestIsSet(t *testing.T) {
460460
} {
461461
errMsg := qt.Commentf("[%d] %v", i, test)
462462

463-
result, err := ns.IsSet(test.a, test.key)
463+
result, err := ns.IsSet(context.Background(), test.a, test.key)
464464
if test.isErr {
465465
continue
466466
}

0 commit comments

Comments
 (0)