Backtestshugorun-20261007-020926#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 bug: an unclosed nested shortcode (`img`) swallows content until it reaches the outer `{{< /figure >}}` closing tag. That tag gets treated as `img`'s closing, which leaves `figure` apparently unclosed, so the error names the wrong shortcode.
Patch B fixes this at the root. When the closing tag's name doesn't match the shortcode being extracted, it raises an error naming the inner shortcode ("shortcode \"img\" must be closed or self-closed"). It also gives a specific message for a stray closing tag on a shortcode that doesn't use `.Inner`. Its tests reproduce the exact scenario from the issue (figure/img/figcaption) and assert the error names `img` and not `figure`. They also cover the top-level unclosed case, the stray closing tag case, and a positive case with valid nesting. These tests would fail before the change and pass after.
Patch A only re-wraps errors from nested extraction with the nested shortcode's position. It does not change how a mismatched closing tag is matched, so the issue's scenario would still report `unclosed shortcode \"figure\"`. Its test covers a different situation (inner template not found) and never exercises the unclosed-nested case from the issue. Patch A therefore does not resolve the reported problem.
Patch B is correct and well tested; Patch A misses the root cause.
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
1h
Wall-clock time in the container
Full test suite
No new failures
3,488 passed · 85 skipped · 3,583 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
94vs 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..d6c30238d 100644@@ -659,6 +659,14 @@ Loop: // self-closing pt.Consume(1) } else {+ if closingName := next.ValStr(source); next.IsShortcodeName() && sc.name != "" && closingName != sc.name {+ if templ := s.firstTemplateStore.LookupShortcodeByName(closingName); templ != nil && !templ.ParseInfo.IsInner {+ // A stray closing tag for a nested shortcode that takes no inner content.+ return sc, fmt.Errorf("%s: shortcode %q does not evaluate .Inner or .InnerDeindent, yet a closing tag was provided", errorPrefix, closingName)+ }+ // The closing tag belongs to an outer shortcode, so this one was never closed.+ return sc, fmt.Errorf("%s: shortcode %q must be closed or self-closed", errorPrefix, sc.name)+ } sc.isClosing = true pt.Consume(2) }hugolib/shortcode_test.goindex dceb93be4..20d128aa2 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,82 @@ func BenchmarkCreateShortcodePlaceholders(b *testing.B) { createShortcodePlaceholder("shortcodeName", 32, uint64(ordinal)) } }++// Issue 10819.+func TestShortcodeUnclosedNestedErrorNamesInnerShortcode(t *testing.T) {+ t.Parallel()++ filesTemplate := `+-- hugo.toml --+disableKinds = ["home", "section", "taxonomy", "term", "rss", "sitemap", "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/_shortcodes/noinner.html --+<hr>+-- layouts/single.html --+{{ .Content }}+-- content/p1.md --+---+title: p1+---+CONTENT+`++ build := func(content string) (*IntegrationTestBuilder, error) {+ files := strings.Replace(filesTemplate, "CONTENT", content, 1)+ return NewIntegrationTestBuilder(+ IntegrationTestConfig{+ T: t,+ TxtarString: files,+ },+ ).BuildE()+ }++ t.Run("Unclosed nested shortcode", func(t *testing.T) {+ t.Parallel()+ b, err := build(`{{< figure >}}+{{< img src="a.jpg" >}}+{{% figcaption %}}+Snazzy+{{% /figcaption %}}+{{< /figure >}}`)+ b.Assert(err, qt.IsNotNil)+ b.Assert(err.Error(), qt.Contains, `failed to extract shortcode: shortcode "img" must be closed or self-closed`)+ b.Assert(err.Error(), qt.Not(qt.Contains), `"figure"`)+ })++ t.Run("Unclosed top level shortcode", func(t *testing.T) {+ t.Parallel()+ b, err := build(`{{< figure >}}+{{< img src="a.jpg" />}}`)+ b.Assert(err, qt.IsNotNil)+ b.Assert(err.Error(), qt.Contains, `failed to extract shortcode: shortcode "figure" must be closed or self-closed`)+ })++ t.Run("Nested shortcode without inner given a closing tag", func(t *testing.T) {+ t.Parallel()+ b, err := build(`{{< figure >}}+{{< noinner >}}{{< /noinner >}}+{{< /figure >}}`)+ b.Assert(err, qt.IsNotNil)+ b.Assert(err.Error(), qt.Contains, `failed to extract shortcode: shortcode "noinner" does not evaluate .Inner or .InnerDeindent, yet a closing tag was provided`)+ b.Assert(err.Error(), qt.Not(qt.Contains), `"figure"`)+ })++ t.Run("Closed and self-closed nested shortcodes", func(t *testing.T) {+ t.Parallel()+ b, err := build(`{{< figure >}}+{{< img src="a.jpg" />}}+{{< img src="b.jpg" >}}B{{< /img >}}+{{% figcaption %}}+Snazzy+{{% /figcaption %}}+{{< /figure >}}`)+ b.Assert(err, qt.IsNil)+ b.AssertFileContent("public/p1/index.html", `<img src="a.jpg">`, `<img src="b.jpg">B`, "<figcaption>")+ })+}
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 {