Backtestshugorun-20261013-011256#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.
Patch B fixes the issue; Patch A does not.
**Root cause.** In the issue's input, the inner `img` shortcode's template uses `.Inner`, so extraction of `img` keeps reading. When it reaches the `{{< /figure >}}` closing tag, the existing `IsShortcodeClose` branch consumes it as `img`'s own closing tag. `figure` then never sees its close and reports `unclosed shortcode "figure"`.
**Patch B** adds a check at that point: if the closing tag's name differs from the current shortcode's name, it returns an error saying the current (inner) shortcode must be closed or self-closed. The check does not affect:
- self-closing tags (the next token is the right delimiter, not a name);
- correctly matched closes;
- inline shortcodes.
This addresses the actual mismatch, and the error now names `img` instead of `figure`. Its tests reproduce the issue's exact scenario plus several variants:
- `%` delimiters;
- deeper nesting;
- an unclosed shortcode at top level;
- a positive case confirming that correctly closed, self-closed, no-inner and inline nested shortcodes still build.
The issue-scenario test and its nested variants would fail before the change and pass after it.
**Patch A** only wraps errors returned from nested extraction with the nested shortcode's position. That does not touch the mis-consumed closing tag. In the reported scenario, nested extraction returns no error; the `unclosed shortcode "figure"` error is raised later at the outer level. So the issue's output would be unchanged. Its test covers a different situation (an inner shortcode whose template is missing) and asserts on position, so it does not exercise the reported bug.
Patch B resolves the issue at its root and has relevant, thorough tests; Patch A misses the issue entirely.
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 32m
Wall-clock time in the container
Full test suite
No new failures
1,940 passed · 77 skipped · 2,031 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
101vs 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..417e0fb88 100644@@ -655,6 +655,10 @@ Loop: return nil, fmt.Errorf("%s: shortcode %q does not evaluate .Inner or .InnerDeindent, yet a closing tag was provided", errorPrefix, name) } }+ if sc.name != "" && (next.IsShortcodeName() || next.IsInlineShortcodeName()) && next.ValStr(source) != sc.name {+ // The closing tag belongs to an enclosing shortcode, so this one was never closed.+ return sc, fmt.Errorf("%s: shortcode %q must be closed or self-closed", errorPrefix, sc.name)+ } if next.IsRightShortcodeDelim() { // self-closing pt.Consume(1)hugolib/shortcode_test.goindex dceb93be4..f94e3c6e8 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,93 @@ func BenchmarkCreateShortcodePlaceholders(b *testing.B) { createShortcodePlaceholder("shortcodeName", 32, uint64(ordinal)) } }++func TestShortcodeErrorWhenNestedShortcodeShouldBeClosedIssue10819(t *testing.T) {+ t.Parallel()++ const filesTemplate = `+-- hugo.toml --+disableKinds = ["home", "taxonomy", "term", "rss", "sitemap", "robotsTXT", "404", "section"]+enableInlineShortcodes = true+-- 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 --+NoInner+-- layouts/single.html --+{{ .Content }}+-- content/p1.md --+---+title: "p1"+---++CONTENT+`++ build := func(t *testing.T, content string) (*IntegrationTestBuilder, error) {+ return NewIntegrationTestBuilder(+ IntegrationTestConfig{+ T: t,+ TxtarString: strings.Replace(filesTemplate, "CONTENT", content, 1),+ },+ ).BuildE()+ }++ t.Run("Nested unclosed", func(t *testing.T) {+ t.Parallel()+ _, err := build(t, `{{< figure >}}+{{< img src="a.jpg" >}}+{{% figcaption %}}+Snazzy+{{% /figcaption %}}+{{< /figure >}}`)+ qt.Assert(t, err, qt.IsNotNil)+ qt.Assert(t, err.Error(), qt.Contains, `failed to extract shortcode: shortcode "img" must be closed or self-closed`)+ qt.Assert(t, err.Error(), qt.Not(qt.Contains), `"figure"`)+ })++ t.Run("Nested unclosed, markdown delimiters", func(t *testing.T) {+ t.Parallel()+ _, err := build(t, `{{% figure %}}+{{% img src="a.jpg" %}}+{{% /figure %}}`)+ qt.Assert(t, err, qt.IsNotNil)+ qt.Assert(t, err.Error(), qt.Contains, `failed to extract shortcode: shortcode "img" must be closed or self-closed`)+ })++ t.Run("Deeply nested unclosed", func(t *testing.T) {+ t.Parallel()+ _, err := build(t, `{{< figure >}}+{{% figcaption %}}+{{< img src="a.jpg" >}}+{{% /figcaption %}}+{{< /figure >}}`)+ qt.Assert(t, err, qt.IsNotNil)+ qt.Assert(t, err.Error(), qt.Contains, `failed to extract shortcode: shortcode "img" must be closed or self-closed`)+ })++ t.Run("Top level unclosed", func(t *testing.T) {+ t.Parallel()+ _, err := build(t, `{{< img src="a.jpg" >}}`)+ qt.Assert(t, err, qt.IsNotNil)+ qt.Assert(t, err.Error(), qt.Contains, `failed to extract shortcode: shortcode "img" must be closed or self-closed`)+ })++ t.Run("Nested closed and self-closed", func(t *testing.T) {+ t.Parallel()+ b, err := build(t, `{{< figure >}}+{{< img src="a.jpg" />}}+{{< img src="b.jpg" >}}B{{< /img >}}+{{< noinner >}}+{{< myinline.inline >}}Inline{{< /myinline.inline >}}+{{% figcaption %}}+Snazzy+{{% /figcaption %}}+{{< /figure >}}`)+ qt.Assert(t, err, qt.IsNil)+ b.AssertFileContent("public/p1/index.html", `<figure>`, `<img src="a.jpg">`, `<img src="b.jpg">B`, "NoInner", "Inline", `<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 {