Backtestshugorun-20260922-141813#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 fix the root cause identically (replacing the silent `return "", nil` at the `!found` branch) and both fix the same unrelated `myview.html` fence typo, so scope is equal. The deciding differences are error handling and test rigor, which cut in opposite directions. Patch A returns `pco.po.p.wrapError(...)`, using Hugo's idiomatic wrapError helper that decorates the error with page/file context — exactly the diagnostic information a user needs to locate which page's `.Render` call failed; this is the convention used throughout hugolib for render-time errors. Patch B returns a bare `fmt.Errorf`, so the surfaced error has no page context. Patch A also uses the wording 'layout' matching the method's own `layout` parameter, which is marginally more accurate than B's 'template'. On tests, Patch B is stronger: it asserts the specific message via `qt.ErrorMatches`, verifying the error content, while Patch A only asserts `err != nil`. Both tests fail before and pass after, so both are valid. Because correctness and craft (criteria 1 and 4) rank above test rigor (criterion 2), Patch A's contextual, idiomatic wrapped error outweighs Patch B's tighter assertion. It's a close call, not a tie — Patch A is the better-engineered 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
27m 4s13 turns
Wall-clock time in the container
Full test suite
No new failures
3,657 passed · 92 skipped · 3,762 total
Contamination audit
Unknown
See the audit notes for this run
Patch size
21vs 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..a49942bf8 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("layout %q not found", layout[0])) } // Make sure to send the *pageState and not the *pageContentOutput to the template.hugolib/page_test.goindex f6225cac5..ec3c9ef3d 100644@@ -1900,6 +1900,24 @@ func TestRenderWithoutArgument(t *testing.T) { b.Assert(err, qt.IsNotNil) } +// 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.IsNotNil)+}+ // 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 }}
What the agent reported
The diff is clean and minimal. All work is complete.
Summary
What was wrongPage.Render "name" silently returned an empty string with no error when the named template/view did not exist. In hugolib/page__per_output.go, the Render method's if !found { return "", nil } branch swallowed the "layout not found" signal from resolveTemplate, so typos in view names failed silently (issue 15052).
What I changed
hugolib/page__per_output.go(production fix): the!foundbranch now returns a wrapped error naming the missing layout, mirroring the method's existing idiom:go return "", pco.po.p.wrapError(fmt.Errorf("layout %q not found", layout[0]))This path is only reached when a layout argument was supplied (the no-argument case already errors earlier), solayout[0]is safe.hugolib/page_test.go(regression test): addedTestRenderViewNotFound— the exact scenario from the report (.Render "view_bar"with noview_bar.html), asserting a build error viaTestE+qt.IsNotNil.tpl/page/page_integration_test.go(blast-radius repair): fixed a pre-existing malformed txtar marker (-- layouts/myview.html→-- layouts/myview.html --). The missing trailing--meantmyview.htmlwas never created as a fixture; the old silent-empty behavior masked this, but the intended{{ .Render "myview" }}inTestThatPageIsAvailableEverywherewould now error. Repairing the marker creates the fixture the test always intended, so.Render "myview"outputs "Render OK." directly — strengthening the test, not weakening it.
Test evidence
- Before the fix:
TestRenderViewNotFoundFAILED (got nil value but want non-nil), proving the bug. - After the fix (gate run via
./bt-test ./hugolib/ -run TestRenderViewNotFound|TestRenderWithoutArgument|TestErrorNestedRender): all three PASS — the regression test passes and the adjacent Render/error tests still pass. TestThatPageIsAvailableEverywhere(./bt-test ./tpl/page/ -run ...): PASS after the marker repair.
Requirements checklist
- Throw an error when
.Renderis given a non-existent template — Implemented, verified byTestRenderViewNotFound(fails before, passes after) and the production diff. - The provided integration test (
TestRenderViewNotFound) should now produce a non-nil error — Implemented; added verbatim and confirmed passing. - Preserve existing behavior for the no-layout case and legitimate views — Verified:
TestRenderWithoutArgumentandTestErrorNestedRenderstill pass; blast-radius scan found and repaired the only affected test.
Note: unrelated AGENTS.md/CLAUDE.md deletions were present in the working tree at session start; they are pre-existing and untouched by this fix. All changes are left uncommitted in the working tree.