Skip to content

Commit

Permalink
Throw error if resources.PostProcess is used in a deferred template
Browse files Browse the repository at this point in the history
That just doesn't work.

See gohugoio#12655
  • Loading branch information
bep committed Jul 17, 2024
1 parent 7be0377 commit 3b93ef6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
20 changes: 20 additions & 0 deletions tpl/templates/defer_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,23 @@ func TestDeferFromContentAdapterShouldFail(t *testing.T) {
b.Assert(err, qt.Not(qt.IsNil))
b.Assert(err.Error(), qt.Contains, "error calling Defer: this method cannot be called before the site is fully initialized")
}

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

files := `
-- hugo.toml --
-- assets/mytext.txt --
ABCD.
-- layouts/index.html --
Home
{{ with (templates.Defer (dict "key" "foo")) }}
{{ $mytext := resources.Get "mytext.txt" | minify | resources.PostProcess }}
{{ end }}
`
b, err := hugolib.TestE(t, files)

b.Assert(err, qt.Not(qt.IsNil))
b.Assert(err.Error(), qt.Contains, "resources.PostProcess cannot be used in a deferred template")
}
8 changes: 7 additions & 1 deletion tpl/tplimpl/template_ast_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package tplimpl
import (
"errors"
"fmt"
"strings"

"github.com/gohugoio/hugo/helpers"
htmltemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
Expand Down Expand Up @@ -248,7 +249,12 @@ func (c *templateContext) handleDefer(withNode *parse.WithNode) {
n := l.Nodes[0].(*parse.ActionNode)

inner := withNode.List.CopyList()
innerHash := helpers.MD5String(inner.String())
s := inner.String()
if strings.Contains(s, "resources.PostProcess") {
c.err = errors.New("resources.PostProcess cannot be used in a deferred template")
return
}
innerHash := helpers.MD5String(s)
deferredID := tpl.HugoDeferredTemplatePrefix + innerHash

c.deferNodes[deferredID] = inner
Expand Down

0 comments on commit 3b93ef6

Please sign in to comment.