Backtestshugorun-20261005-133023#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 A = our agent, Patch B = the merged fix.
Both patches change the same line in `pageContentOutput.Render` so a missing layout returns an error instead of `"", nil`. That fixes the root cause, and both also fix the malformed `-- layouts/myview.html` header in tpl/page, which would otherwise break under the new behavior. On correctness the two are equivalent. Patch A's error is more useful. It goes through `pco.po.p.wrapError`, which adds page and file context in line with how page errors are reported elsewhere in hugolib, and it names the layout, the page kind and the output format. This matters because layout lookup depends on the output format: a view that exists only as .html is legitimately missing for JSON output. Patch B's bare `template %q not found` gives no page or format context. Patch A's tests are also stronger. It reproduces the issue's case and checks the message, verifies the success path still renders once the missing call is removed, and covers the per-output-format case (view_foo exists for html but is rendered from home.json). Patch B tests only the issue's single scenario. One small uncertainty: the diff does not show whether hugolib_integration_test.go already imports `strings`, so Patch A's test could need an import. Weighing better diagnostics, convention-consistent error wrapping and broader regression coverage, Patch A is the better fix.
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 31m
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
54vs 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/hugolib_integration_test.goindex c5570469a..22c2d1cf1 100644@@ -127,3 +127,54 @@ title: Films b := hugolib.Test(t, files) b.AssertFileContent("public/index.html", "tags\nfiction\nbooks\nFilms") }++// 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+`++ _, err := hugolib.TestE(t, files)+ if err == nil {+ t.Fatal("expected error")+ }+ want := `failed to find layout "view_bar" for page kind "home" and output format "html"`+ if !strings.Contains(err.Error(), want) {+ t.Errorf("error msg must contain %q, error msg actually contains %q", want, err.Error())+ }++ files = strings.Replace(files, `{{ .Render "view_bar" }}`, "", 1)+ b := hugolib.Test(t, files)+ b.AssertFileContent("public/index.html", "foo")++ // The view exists for html only, so rendering it from the json output must fail.+ files = `+-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+[outputs]+home = ['html', 'json']+-- layouts/home.html --+{{ .Render "view_foo" }}+-- layouts/home.json --+{{ .Render "view_foo" }}+-- layouts/view_foo.html --+foo+`++ _, err = hugolib.TestE(t, files)+ if err == nil {+ t.Fatal("expected error")+ }+ want = `failed to find layout "view_foo" for page kind "home" and output format "json"`+ if !strings.Contains(err.Error(), want) {+ t.Errorf("error msg must contain %q, error msg actually contains %q", want, err.Error())+ }+}hugolib/page__per_output.goindex 9009165ff..700ced874 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 find layout %q for page kind %q and output format %q", layout[0], pco.po.p.Kind(), pco.po.p.pageOutput.f.Name)) } // Make sure to send the *pageState and not the *pageContentOutput to the template.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 }}