Backtestshugorun-20261024-162305#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 A = our agent, Patch B = the merged fix.
The issue's actual scenario is an unclosed nested shortcode (img, which evaluates .Inner) inside figure: the parser consumes `{{< /figure >}}` as img's closing tag, then reports figure as unclosed. Patch A addresses this root cause in extractShortcode: when a closing tag's name does not match the current shortcode, it reports that the inner shortcode must be closed (or, if the named shortcode takes no inner, reports the stray closing tag at its position), and it additionally wraps nested errors with the nested shortcode's position. Patch B only does the latter: it re-positions errors that bubble up from nested extraction (e.g. missing template) but does nothing for the mismatched-close case, so the user's exact content still yields `unclosed shortcode "figure"` at the outer position. The independent tests confirm this: both tests built from the issue's own content and error line (naming the inner shortcode, and reporting its line) pass on A and still fail on B, while B only fixes the missing-template-position case that A also fixes. Tests: A adds a build test for the valid nested case plus five error-case assertions covering message and position at multiple nesting depths and delimiter styles; B adds one test for the missing-template position only, which does not exercise the issue's reported scenario. Scope/craft: A's change is larger but targeted and uses the existing error prefix, herrors.NewFileErrorFromPos and posFromInput conventions; the extra inner/no-inner distinction on the close tag is justified by the ambiguity in the issue. B is clean but incomplete.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
1h 10m
Wall-clock time in the container
Full test suite
No new failures
2,046 passed · 2 skipped · 2,084 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
125vs 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..3b7fbfe98 100644@@ -593,6 +593,7 @@ func (s *shortcodeParseInfo) extractShortcode(ordinal, level int, source []byte, nestedOrdinal := 0 nextLevel := level + 1 closed := false+ closeDelimPos := -1 const errorPrefix = "failed to extract shortcode" Loop:@@ -606,6 +607,7 @@ Loop: return sc, errors.New("shortcode has no name") } if next.IsShortcodeClose() {+ closeDelimPos = currItem.Pos() continue } @@ -621,6 +623,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 +647,21 @@ Loop: } case currItem.IsShortcodeClose():- closed = true next := pt.Peek()+ if sc.name != "" && (next.IsShortcodeName() || next.IsInlineShortcodeName()) && next.ValStr(source) != sc.name {+ closeName := next.ValStr(source)+ if next.IsShortcodeName() {+ if templ := s.firstTemplateStore.LookupShortcodeByName(closeName); templ != nil && !templ.ParseInfo.IsInner {+ // A closing tag for a nested shortcode that does not take inner content.+ err := fmt.Errorf("%s: shortcode %q does not evaluate .Inner or .InnerDeindent, yet a closing tag was provided", errorPrefix, closeName)+ return sc, herrors.NewFileErrorFromPos(err, posFromInput("", source, closeDelimPos))+ }+ }+ // This closing tag belongs to an enclosing 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() {hugolib/shortcode_test.goindex dceb93be4..b737e0a67 100644@@ -13,7 +13,12 @@ package hugolib -import "testing"+import (+ "strings"+ "testing"++ qt "github.com/frankban/quicktest"+) func BenchmarkCreateShortcodePlaceholders(b *testing.B) { var ordinal int = 42@@ -21,3 +26,99 @@ func BenchmarkCreateShortcodePlaceholders(b *testing.B) { createShortcodePlaceholder("shortcodeName", 32, uint64(ordinal)) } }++func TestShortcodeNestedNoInnerIssue10819(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ["taxonomy", "term", "home", "section", "rss", "sitemap"]+-- layouts/all.html --+{{ .Content }}+-- layouts/_shortcodes/myfigure.html --+<figure>{{ .Inner }}</figure>+-- layouts/_shortcodes/myimg.html --+<img src="{{ .Get "src" }}">+-- layouts/_shortcodes/myfigcaption.html --+<figcaption>{{ .Inner }}</figcaption>+-- content/p1.md --+---+title: p1+---++{{< myfigure >}}+{{< myimg src="a.jpg" >}}+{{% myfigcaption %}}+Snazzy+{{% /myfigcaption %}}+{{< /myfigure >}}+`+ b := Test(t, files)+ b.AssertFileContent("public/p1/index.html", "<figure>", "<img src=\"a.jpg\">", "<figcaption><p>Snazzy</p>\n</figcaption>", "</figure>")+}++func TestShortcodeErrorNestedIssue10819(t *testing.T) {+ t.Parallel()++ filesTemplate := `+-- hugo.toml --+disableKinds = ["taxonomy", "term", "home", "section", "rss", "sitemap"]+-- layouts/all.html --+{{ .Content }}+-- layouts/_shortcodes/myfigure.html --+<figure>{{ .Inner }}</figure>+-- layouts/_shortcodes/mydiv.html --+<div>{{ .Inner }}</div>+-- layouts/_shortcodes/myimg.html --+<img src="{{ .Get "src" }}">{{ .Inner }}+-- layouts/_shortcodes/myicon.html --+<i></i>+-- layouts/_shortcodes/myfigcaption.html --+<figcaption>{{ .Inner }}</figcaption>+-- content/p1.md --+---+title: p1+---++CONTENT+`++ test := func(content, expect string) {+ t.Helper()+ files := strings.Replace(filesTemplate, "CONTENT", content, 1)+ b, err := TestE(t, files)+ b.Assert(err, qt.IsNotNil)+ b.Assert(err.Error(), qt.Contains, expect)+ }++ // Unclosed inner shortcode taking .Inner.+ test(`{{< myfigure >}}+{{< myimg src="a.jpg" >}}+{{% myfigcaption %}}+Snazzy+{{% /myfigcaption %}}+{{< /myfigure >}}`, `p1.md:6:1": failed to extract shortcode: shortcode "myimg" must be closed or self-closed`)++ // Same, but with markdown delimiters on the unclosed shortcode.+ test(`{{< myfigure >}}+{{% myimg src="a.jpg" %}}+{{< /myfigure >}}`, `p1.md:6:1": failed to extract shortcode: shortcode "myimg" must be closed or self-closed`)++ // Deeper nesting.+ test(`{{< mydiv >}}+{{< myfigure >}}+text {{< myimg src="a.jpg" >}}+{{< /myfigure >}}+{{< /mydiv >}}`, `p1.md:7:6": failed to extract shortcode: shortcode "myimg" must be closed or self-closed`)++ // Closing tag for a nested shortcode that does not take inner content.+ test(`{{< myfigure >}}+{{< myfigcaption >}}{{< /myfigcaption >}}+{{< myicon >}}x{{< /myicon >}}+{{< /myfigure >}}`, `p1.md:7:16": failed to extract shortcode: shortcode "myicon" does not evaluate .Inner or .InnerDeindent, yet a closing tag was provided`)++ // Other errors in nested shortcodes are reported at the nested shortcode.+ test(`{{< myfigure >}}+{{< nosuchshortcode >}}+{{< /myfigure >}}`, `p1.md:6:1": failed to extract shortcode: template for shortcode "nosuchshortcode" not found`)+}
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 {