Backtestshugorun-20260922-141813#11302

Issue #11302 Merged fix PR # Run 22 Sep 2026 · run-20260922-141813 gohugoio/hugo

The "failed to extract" error refers to outer shortcode instead of inner shortcode

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Patch A fixes the reported bug at its root. The issue is that when an inner shortcode that uses .Inner (img) is left unclosed, the parser lets img greedily consume the outer's closing tag ({{< /figure >}}), so figure later hits EOF and reports 'unclosed shortcode figure' — blaming the wrong shortcode. Patch A adds a name-match check at the point a closing token is encountered in extractShortcode: if the close token's name differs from the current shortcode's name, the current (inner) shortcode's own close is missing, and it errors against that shortcode ('shortcode "img" must be closed or self-closed'). This correctly identifies the actually-unclosed shortcode. Its test reproduces the exact issue scenario (figure/img/figcaption) and asserts the error names img, not figure — it fails before and passes after. Patch B misidentifies the problem: its test uses a different scenario ({{< outer >}}{{< inner >}}{{< /outer >}}) producing a 'template for shortcode "inner" not found' error, not the reported 'unclosed shortcode' error. Its change only wraps errors *returned* from the nested extractShortcode call with a position; but in the reported repro the inner shortcode consumes the outer's closing tag and returns successfully, so the 'unclosed figure' error is generated by the outer's own loop at EOF and is never routed through the nested-error path Patch B touches. Thus Patch B likely does not fix the reported bug and only adjusts error position/wrapping for an unrelated case. Both patches are similar in scope and craft, but correctness — the dominant criterion — decisively favors A.

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

29m 55s43 turns

Wall-clock time in the container

Full test suite

No new failures

3,480 passed · 85 skipped · 3,579 total

Contamination audit

Unknown

See the audit notes for this run

Patch size

45vs 43 merged

Changed lines, ours vs merged

<!-- Please answer these questions before submitting your issue. Thanks! -->

What version of Hugo are you using (hugo version)?

<pre>
$ hugo version
hugo v0.111.1-39a4a3cf676533859217805c36181c7a3dfa321c darwin/arm64 BuildDate=2023-03-02T10:08:26Z VendorInfo=gohugoio
</pre>

Does this issue reproduce with the latest release?

yes

When evaluating this file:

+++
Type = "note"
Date = "2020-03-30T01:19:59-05:00"

+++

{{< figure >}}
{{< img src="lighting-in-pantry.jpg" alt="a pantry with led lights shining bright light out of it" >}}
{{% figcaption %}}
Snazzy 💡
{{% /figcaption %}}
{{< /figure >}}

i get this error:

Start building sites … 
hugo v0.111.1-39a4a3cf676533859217805c36181c7a3dfa321c darwin/arm64 BuildDate=2023-03-02T10:08:26Z VendorInfo=gohugoio
Error: Error building site: "/Users/calebjasik/Git/jasik-xyz/content/2020-03-30-0119/index.md:7:1": failed to extract shortcode: unclosed shortcode "figure"
Total in 88 ms

Before v0.111.0, this compiled fine, but I'm not sure the "is closed" detector is effective if there is additional shortcodes in the .Inner of the shortcode?

The two patches

Our patch+45 −0
hugolib/shortcode.goindex c680620cf..62a71cad3 100644@@ -659,6 +659,13 @@ Loop: 				// self-closing 				pt.Consume(1) 			} else {+				if !sc.isInline && sc.name != "" && next.IsShortcodeName() && next.ValStr(source) != sc.name {+					// The closing tag is for an ancestor shortcode, which means+					// this shortcode's own closing tag is missing. Report the+					// error against this (inner) shortcode rather than the outer+					// shortcode whose closing tag was consumed. See #11302.+					return sc, fmt.Errorf("%s: shortcode %q must be closed or self-closed", errorPrefix, sc.name)+				} 				sc.isClosing = true 				pt.Consume(2) 			}tpl/tplimpl/shortcodes_integration_test.goindex 32edbe52d..4cafd7ddc 100644@@ -871,6 +871,44 @@ title: p1 	} } +// TestShortcodeNestedInnerIssue11302 verifies that when an inner shortcode that+// evaluates .Inner is left unclosed inside an outer shortcode, the error names+// the actually-unclosed inner shortcode ("img") rather than the outer one+// ("figure"). See https://github.com/gohugoio/hugo/issues/11302.+func TestShortcodeNestedInnerIssue11302(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- layouts/home.html --+{{ .Content }}+-- layouts/_shortcodes/figure.html --+<figure>{{ .Inner }}</figure>+-- layouts/_shortcodes/img.html --+<img src="{{ .Get "src" }}">{{ .Inner }}+-- layouts/_shortcodes/figcaption.html --+<figcaption>{{ .Inner }}</figcaption>+-- content/_index.md --+---+title: home+---+{{< figure >}}+{{< img src="lighting-in-pantry.jpg" alt="a pantry with led lights shining bright light out of it" >}}+{{% figcaption %}}+Snazzy+{{% /figcaption %}}+{{< /figure >}}+`++	_, err := hugolib.TestE(t, files)++	c := qt.New(t)+	c.Assert(err, qt.IsNotNil)+	c.Assert(err.Error(), qt.Contains, `shortcode "img" must be closed or self-closed`)+	c.Assert(err.Error(), qt.Not(qt.Contains), `shortcode "figure"`)+}+ // Issue 14491 func TestGistShortcodeDeprecationError14491(t *testing.T) { 	t.Parallel() 
Merged fix · PR #+42 −1
hugolib/hugo_sites_build_errors_test.goindex 3209f664d..10e9d123e 100644@@ -421,6 +421,39 @@ line 4 	b.Assert(errors[3].ErrorContext().Lines, qt.DeepEquals, []string{"line 1", "line 2", "123{{ .ThisDoesNotExist }}", "line 4"}) } +// Issue #11302: "failed to extract" error should refer to inner shortcode, not outer.+func TestErrorNestedShortcodeExtractInnerNotFound(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+-- content/_index.md --+---+title: "Home"+---++## Hello+{{< outer >}}{{< inner >}}{{< /outer >}}++-- layouts/home.html --+{{ .Content }}+-- layouts/_shortcodes/outer.html --+<div>{{ .Inner }}</div>+`++	b, err := TestE(t, files)++	b.Assert(err, qt.IsNotNil)+	fe := herrors.UnwrapFileError(err)+	b.Assert(fe, qt.Not(qt.IsNil))++	// The error should point to the inner shortcode's position (line 6, column 12),+	// not the outer shortcode's position (line 6, column 1).+	b.Assert(fe.Position().LineNumber, qt.Equals, 6)+	b.Assert(fe.Position().ColumnNumber, qt.Equals, 12)+	b.Assert(fe.Error(), qt.Contains, `failed to extract shortcode: template for shortcode "inner" not found`)+}+ func TestErrorRenderHookHeading(t *testing.T) { 	t.Parallel() hugolib/shortcode.goindex c680620cf..5275dc04b 100644@@ -612,6 +612,7 @@ Loop: 			if cnt > 0 { 				// nested shortcode; append it to inner content 				pt.Backup()+				nestedPos := currItem.Pos() 				nested, err := s.extractShortcode(nestedOrdinal, nextLevel, source, pt) 				nestedOrdinal++ 				if nested != nil && nested.name != "" {@@ -621,7 +622,14 @@ Loop: 				if err == nil { 					sc.inner = append(sc.inner, nested) 				} else {-					return sc, err+					// Wrap the error with the nested shortcode's position so that+					// the error points to the correct shortcode (the inner one)+					// rather than the outer shortcode.+					if fe, ok := err.(herrors.FileError); ok {+						return sc, fe+					}+					pos := posFromInput("", source, nestedPos)+					return sc, herrors.NewFileErrorFromPos(err, pos) 				}  			} else { 

What the agent reported

The fix is complete and verified. Here is my final report.

What was wrong

Hugo issue #11302: when an outer shortcode that renders .Inner (e.g. figure) contains a nested inner shortcode that also evaluates .Inner (e.g. img) but is left without its closing tag, the unclosed inner shortcode greedily consumes the following tokens — including the outer shortcode's {{< /figure >}} closing tag. Hugo then reported the error against the outer shortcode (shortcode "figure" must be closed or self-closed), which is misleading: the actually-unclosed shortcode is the inner img.

Root cause: in hugolib/shortcode.go, extractShortcode's case currItem.IsShortcodeClose(): handler consumed any closing tag as if it closed the current shortcode, never checking that the closing tag's name matched. So a recursing inner img swallowed figure's close, leaving figure unclosed and wrongly blamed.

What I changed

hugolib/shortcode.go — one guard in the close handler. When a closing tag's shortcode name does not match the current shortcode's sc.name (i.e. the tag belongs to an ancestor, so the current inner shortcode is the one missing its close), it now returns the error naming the current (inner) shortcode:

if !sc.isInline && sc.name != "" && next.IsShortcodeName() && next.ValStr(source) != sc.name {
    // The closing tag is for an ancestor shortcode ... See #11302.
    return sc, fmt.Errorf("%s: shortcode %q must be closed or self-closed", errorPrefix, sc.name)
}

tpl/tplimpl/shortcodes_integration_test.go — added TestShortcodeNestedInnerIssue11302, replicating the ticket's exact markdown (figure → unclosed inner img that evaluates .Inner, plus a properly closed figcaption sibling), asserting the error names "img" and not "figure".

Test evidence

  • Teeth verified: with the fix reverted, the regression test fails — the error reads shortcode "figure" must be closed or self-closed (exactly the #11302 bug). With the fix, it passes (error names img).
  • Gate (all with fix in place): hugolib shortcode/inner/nested tests — 28 pass / 0 fail; parser/pageparser — 103 pass / 0 fail; tpl/tplimpl shortcode integration (incl. the new test) — 25 pass / 1 pre-existing skip / 0 fail.
  • Reviewer confirmed no false positives in valid nesting (matched closes, self-closing, inline, empty-name, top-level self-close all fall through untouched) and no unrelated changes.

Requirements checklist

  1. Fix the misleading error that names the outer shortcode instead of the inner one (#11302) — Implemented. The error now correctly names the unclosed inner shortcode; regression test proves before/after behavior.
  2. "Compiled fine before v0.111.0" / the exact ticket markdown — Investigated with evidence. I first built the literal markdown treating img as a normal self-closing (non-.Inner) shortcode: it builds successfully on current HEAD (that variant was never the bug — the lexer and semantic parser at v0.111.1 are functionally identical to current, so valid input always compiled). The real failure requires the inner shortcode to evaluate .Inner and be left unclosed, which a probe confirmed still produced the outer-named error on current HEAD — that is the scenario the fix and regression test target.

Note: the AGENTS.md/CLAUDE.md deletions in git status are pre-existing environment artifacts, not part of this change. All changes are left uncommitted in the working tree.