Backtestshugorun-20261009-153657#11302
The "failed to extract" error refers to outer shortcode instead of inner shortcode
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
The issue's case is an inner shortcode (`img`) whose template uses `.Inner` but which is never closed. The parser lets `img` consume `{{< /figure >}}` as its own closing tag, so the error is then reported as an unclosed `figure`. Patch B fixes this at the root: when a closing tag's name does not match the current shortcode's name, it reports that the current (inner) shortcode "must be closed or self-closed". Patch B also wraps errors from nested shortcodes with the nested shortcode's position, so the reported line and column point at the inner shortcode. Its tests reproduce the issue's figure/img/figcaption structure, cover the unclosed case at one and two nesting levels plus a template-not-found case, and include a positive case confirming that properly closed and self-closed nesting still renders. Patch A only adds position wrapping for errors from nested extraction, and its test covers only a missing inner template. It does nothing about the closing-tag mismatch, so the issue's exact scenario would still produce `unclosed shortcode "figure"`. Patch A therefore addresses only an adjacent symptom, not the reported bug.
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
40m 33s
Wall-clock time in the container
Full test suite
No new failures
1,946 passed · 77 skipped · 2,031 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
64vs 43 merged
Changed lines, ours vs merged
The issue
View on GitHub<!-- 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
hugolib/shortcode.goindex c680620cf..16800355a 100644@@ -621,6 +621,11 @@ Loop: if err == nil { sc.inner = append(sc.inner, nested) } else {+ if _, ok := err.(herrors.FileError); !ok {+ // Report the error at the position of the nested shortcode,+ // not the outermost one.+ err = herrors.NewFileErrorFromPos(err, posFromInput("", source, currItem.Pos()))+ } return sc, err } @@ -640,8 +645,13 @@ Loop: } case currItem.IsShortcodeClose():- closed = true next := pt.Peek()+ if sc.name != "" && next.IsShortcodeName() && next.ValStr(source) != sc.name {+ // This closing tag belongs to an outer shortcode,+ // so this shortcode was never closed.+ return sc, fmt.Errorf("%s: shortcode %q must be closed or self-closed", errorPrefix, sc.name)+ }+ closed = true if !sc.isInline { if !sc.needsInner() { if next.IsError() {tpl/tplimpl/shortcodes_integration_test.goindex 32edbe52d..9140b4b21 100644@@ -891,3 +891,55 @@ title: home b.Assert(err, qt.IsNotNil) b.AssertLogContains(`ERROR The "gist" shortcode was deprecated in v0.143.0 and removed in v0.156.0.`) }++func TestShortcodeNestedUnclosedErrorIssue10819(t *testing.T) {+ t.Parallel()++ filesTemplate := `+-- hugo.toml --+-- layouts/all.html --+{{ .Content }}+-- layouts/_shortcodes/figure.html --+<figure>{{ .Inner }}</figure>+-- layouts/_shortcodes/img.html --+{{ with .Inner }}{{ . }}{{ end }}<img src="{{ .Get "src" }}">+-- layouts/_shortcodes/figcaption.html --+<figcaption>{{ .Inner }}</figcaption>+-- content/p1.md --+---+title: p1+---++{{< figure >}}+INNER+{{% figcaption %}}+Snazzy+{{% /figcaption %}}+{{< /figure >}}+`++ for _, test := range []struct {+ name string+ inner string+ expect string+ }{+ {"unclosed", `{{< img src="a.jpg" >}}`, `"/content/p1.md:6:1": failed to extract shortcode: shortcode "img" must be closed or self-closed`},+ {"unclosed in deeper level", `{{< figure >}}{{< img src="a.jpg" >}}{{< /figure >}}`, `"/content/p1.md:6:15": failed to extract shortcode: shortcode "img" must be closed or self-closed`},+ {"template not found", `{{< nosuchshortcode >}}`, `"/content/p1.md:6:1": failed to extract shortcode: template for shortcode "nosuchshortcode" not found`},+ } {+ t.Run(test.name, func(t *testing.T) {+ t.Parallel()+ files := strings.ReplaceAll(filesTemplate, "INNER", test.inner)+ b, err := hugolib.TestE(t, files)+ b.Assert(err, qt.IsNotNil)+ b.Assert(err.Error(), qt.Contains, test.expect)+ })+ }++ t.Run("closed", func(t *testing.T) {+ t.Parallel()+ files := strings.ReplaceAll(filesTemplate, "INNER", `{{< img src="a.jpg" />}}{{< img src="b.jpg" >}}B{{< /img >}}`)+ b := hugolib.Test(t, files)+ b.AssertFileContent("public/p1/index.html", `<figure>`, `<img src="a.jpg">`, `B<img src="b.jpg">`, `<figcaption>`, `Snazzy`)+ })+}
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 {