Backtestshugorun-20261002-200313#15052
hugolib: Throw error when Render method is unable to find specified template
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches fix the same root cause. They replace the silent `return "", nil` in `pageContentOutput.Render` with an error when the named layout cannot be found. Both also correct the same malformed `-- layouts/myview.html` txtar header in `tpl/page`. That header fix matters: once missing views raise an error, the broken header would make the existing test fail. Correctness is equivalent at the core. Patch B's error is more useful, though. It goes through `pco.po.p.wrapError`, the page's existing error-wrapping helper used elsewhere in hugolib, so the message carries page/file context. The text (`failed to resolve layout "view_bar" for .Render: template not found`) also says which call failed. Patch A gives a bare `template "view_bar" not found` with no page context, which is less helpful on a site with many pages. Tests: Patch A adds only the issue's reproduction and checks the message with a regex. Patch B adds that same case, plus `.Render` on regular pages in a range loop (a different call path), plus a positive check that an existing view still renders correctly. B's tests cover more and would catch regressions in both directions. Scope: both are minimal, with no unrelated churn. Overall B is better on craft (contextual error, consistent with hugolib conventions) and on test thoroughness.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
1h 26m
Wall-clock time in the container
Full test suite
No new failures
3,661 passed · 92 skipped · 3,762 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
53vs 21 merged
Changed lines, ours vs merged
The issue
View on GitHubCurrently, when you invoke .Page.Render and pass it the name of a non-existent template, the method returns an empty string without warning or error. This isn't anything new; Hugo has behaved this way since 0.54.0, and probably earlier.
There may be a good reason for the existing behavior, but it was surprising to me.
Relevant code:
Integration test:
// See issue 15052.
func TestRenderViewNotFound(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
-- layouts/home.html --
{{ .Render "view_foo" }}
{{ .Render "view_bar" }}
-- layouts/view_foo.html --
foo
`
b, err := hugolib.TestE(t, files)
b.Assert(err, qt.IsNotNil) // fails (no error is thrown)
}
Reference: <https://discourse.gohugo.io/t/silent-error-with-render-function-in-v0-163-2-extended-withdeploy/57290/7>
The two patches
hugolib/page__per_output.goindex 9009165ff..39daa0c40 100644@@ -114,7 +114,7 @@ func (pco *pageContentOutput) Render(ctx context.Context, layout ...string) (tem } if !found {- return "", nil+ return "", pco.po.p.wrapError(fmt.Errorf("failed to resolve layout %q for .Render: template not found", layout[0])) } // Make sure to send the *pageState and not the *pageContentOutput to the template.hugolib/page_test.goindex f6225cac5..e58162703 100644@@ -1900,6 +1900,56 @@ func TestRenderWithoutArgument(t *testing.T) { b.Assert(err, qt.IsNotNil) } +// Issue #15052+func TestRenderViewNotFound(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- layouts/home.html --+{{ .Render "view_foo" }}+{{ .Render "view_bar" }}+-- layouts/view_foo.html --+foo+`++ b, err := TestE(t, files)+ b.Assert(err, qt.IsNotNil)+ b.Assert(err.Error(), qt.Contains, `failed to resolve layout "view_bar"`)++ // Range over pages (Render via the lazy content provider on page collections).+ files = `+-- hugo.toml --+disableKinds = ['rss','section','sitemap','taxonomy','term']+-- content/p1.md --+---+title: p1+---+-- layouts/home.html --+{{ range site.RegularPages }}{{ .Render "view_bar" }}{{ end }}+-- layouts/page.html --+{{ .Title }}+`++ b, err = TestE(t, files)+ b.Assert(err, qt.IsNotNil)+ b.Assert(err.Error(), qt.Contains, `failed to resolve layout "view_bar"`)++ // Existing view renders fine.+ files = `+-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- layouts/home.html --+{{ .Render "view_foo" }}+-- layouts/view_foo.html --+foo+`++ b = Test(t, files)+ b.AssertFileContent("public/index.html", "foo")+}+ // Issue #13021 func TestAllStores(t *testing.T) { t.Parallel()tpl/page/page_integration_test.goindex 817033b40..da18f553e 100644@@ -77,7 +77,7 @@ Image OK. {{ if page.IsHome }} Link OK. {{ end }}+-- layouts/myview.html -- {{ if page.IsHome }} Render OK. {{ end }}
hugolib/page__per_output.goindex 9009165ff..e8dd38ce7 100644@@ -114,7 +114,7 @@ func (pco *pageContentOutput) Render(ctx context.Context, layout ...string) (tem } if !found {- return "", nil+ return "", fmt.Errorf("template %q not found", layout[0]) } // Make sure to send the *pageState and not the *pageContentOutput to the template.hugolib/page_test.goindex f6225cac5..803dddc01 100644@@ -2055,3 +2055,21 @@ disableKinds = ["taxonomy", "term"] } } }++// See issue 15052.+func TestRenderViewNotFound(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- layouts/home.html --+{{ .Render "view_foo" }}+{{ .Render "view_bar" }}+-- layouts/view_foo.html --+foo+`++ b, err := TestE(t, files)+ b.Assert(err, qt.ErrorMatches, `.*template "view_bar" not found.*`)+}tpl/page/page_integration_test.goindex 817033b40..da18f553e 100644@@ -77,7 +77,7 @@ Image OK. {{ if page.IsHome }} Link OK. {{ end }}+-- layouts/myview.html -- {{ if page.IsHome }} Render OK. {{ end }}