Backtestshugorun-20261002-200313#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 root cause is in extractShortcode. When a nested shortcode that uses .Inner (`img`) is left unclosed, its parse loop treats the outer `{{< /figure >}}` close tag as its own. It sets closed=true and consumes that tag. The outer `figure` then reaches EOF and reports "unclosed shortcode figure", which is the misleading error in the report.
Patch A fixes this directly. In the IsShortcodeClose branch it peeks at the closing name. If that name differs from the current shortcode's name, it returns "shortcode \"img\" must be closed or self-closed" instead of swallowing the outer close. It keeps the existing "does not evaluate .Inner" error for a closing tag on a non-inner nested shortcode, so that check still happens before the mismatch error. It also wraps nested non-FileErrors with the nested shortcode's position, so the error points at line 6:1 (the `img` line) rather than at the outer shortcode.
Patch A's tests cover:
- the exact issue scenario, asserting the inner name and position and that "figure" is absent;
- an unclosed inner shortcode just before the outer close;
- a closing tag on a non-inner nested shortcode;
- a positive case where a non-inner `img` mixed with closed `figcaption` still renders correctly.
Patch B only adds position wrapping for errors that already come back from nested extraction. It does nothing about the close-tag swallowing. In the issue's scenario the error is produced by the outer shortcode at EOF, not by the nested call, so B would still report "unclosed shortcode figure". B's test uses a different scenario (inner template not found) and does not exercise the reported bug. Patch A resolves the issue; Patch B does not.
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
47m 48s
Wall-clock time in the container
Full test suite
No new failures
3,487 passed · 85 skipped · 3,582 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
143vs 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..9aa1666bf 100644@@ -621,6 +621,10 @@ 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.+ err = herrors.NewFileErrorFromPos(err, posFromInput("", source, currItem.Pos()))+ } return sc, err } @@ -640,8 +644,19 @@ Loop: } case currItem.IsShortcodeClose():- closed = true next := pt.Peek()+ if (next.IsShortcodeName() || next.IsInlineShortcodeName()) && sc.name != "" && next.ValStr(source) != sc.name {+ if next.IsShortcodeName() {+ if templ := s.firstTemplateStore.LookupShortcodeByName(next.ValStr(source)); templ != nil && !templ.ParseInfo.IsInner {+ // A closing tag for a shortcode that cannot have one.+ return nil, fmt.Errorf("%s: shortcode %q does not evaluate .Inner or .InnerDeindent, yet a closing tag was provided", errorPrefix, next.ValStr(source))+ }+ }+ // 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..c2452c930 100644@@ -13,7 +13,11 @@ package hugolib -import "testing"+import (+ "testing"++ qt "github.com/frankban/quicktest"+) func BenchmarkCreateShortcodePlaceholders(b *testing.B) { var ordinal int = 42@@ -21,3 +25,123 @@ func BenchmarkCreateShortcodePlaceholders(b *testing.B) { createShortcodePlaceholder("shortcodeName", 32, uint64(ordinal)) } }++// Issue #10819.+func TestShortcodeErrorUnclosedNestedShortcode(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ["home", "section", "taxonomy", "term", "rss", "sitemap", "robots.txt", "404"]+-- layouts/_shortcodes/figure.html --+<figure>{{ .Inner }}</figure>+-- layouts/_shortcodes/img.html --+<img src="{{ .Get "src" }}">{{ .Inner }}+-- layouts/_shortcodes/figcaption.html --+<figcaption>{{ .Inner }}</figcaption>+-- layouts/single.html --+{{ .Content }}+-- content/p1.md --+---+title: p1+---++{{< figure >}}+{{< img src="a.jpg" >}}+{{% figcaption %}}+Snazzy+{{% /figcaption %}}+{{< /figure >}}+`++ b, err := TestE(t, files)+ b.Assert(err, qt.IsNotNil)+ b.Assert(err.Error(), qt.Contains, `p1.md:6:1": failed to extract shortcode: shortcode "img" must be closed or self-closed`)+ b.Assert(err.Error(), qt.Not(qt.Contains), `"figure"`)+}++func TestShortcodeErrorUnclosedNestedShortcodeAtEnd(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ["home", "section", "taxonomy", "term", "rss", "sitemap", "robots.txt", "404"]+-- layouts/_shortcodes/outer.html --+<div>{{ .Inner }}</div>+-- layouts/_shortcodes/inner.html --+<span>{{ .Inner }}</span>+-- layouts/single.html --+{{ .Content }}+-- content/p1.md --+---+title: p1+---++{{< outer >}}+{{< inner >}}+{{< /outer >}}+`++ b, err := TestE(t, files)+ b.Assert(err, qt.IsNotNil)+ b.Assert(err.Error(), qt.Contains, `shortcode "inner" must be closed or self-closed`)+}++func TestShortcodeErrorNestedClosingTagForNonInnerShortcode(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ["home", "section", "taxonomy", "term", "rss", "sitemap", "robots.txt", "404"]+-- layouts/_shortcodes/outer.html --+<div>{{ .Inner }}</div>+-- layouts/_shortcodes/img.html --+<img>+-- layouts/single.html --+{{ .Content }}+-- content/p1.md --+---+title: p1+---++{{< outer >}}+{{< img >}}a{{< /img >}}+{{< /outer >}}+`++ b, err := TestE(t, files)+ b.Assert(err, qt.IsNotNil)+ b.Assert(err.Error(), qt.Contains, `shortcode "img" does not evaluate .Inner or .InnerDeindent, yet a closing tag was provided`)+ b.Assert(err.Error(), qt.Not(qt.Contains), `"outer"`)+}++func TestShortcodeNestedMixedClosedAndNonInner(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ["home", "section", "taxonomy", "term", "rss", "sitemap", "robots.txt", "404"]+-- layouts/_shortcodes/figure.html --+<figure>{{ .Inner }}</figure>+-- layouts/_shortcodes/img.html --+<img src="{{ .Get "src" }}">+-- layouts/_shortcodes/figcaption.html --+<figcaption>{{ .Inner }}</figcaption>+-- layouts/single.html --+{{ .Content }}+-- content/p1.md --+---+title: p1+---++{{< figure >}}+{{< img src="a.jpg" >}}+{{% figcaption %}}+Snazzy+{{% /figcaption %}}+{{< /figure >}}+`++ b := Test(t, files)+ b.AssertFileContent("public/p1/index.html", `<figure>`, `<img src="a.jpg">`, `<figcaption><p>Snazzy</p>`, `</figure>`)+}
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 {