Backtestshugorun-20261005-133023#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 scenario is a nested shortcode (`figcaption`) that is given a closing tag but does not evaluate .Inner. The nested extraction returns without error. The outer shortcode then meets the stray `{{% /figcaption %}}` closing tag and ends up reporting a misleading "unclosed shortcode figure" against the outer shortcode. Patch A only re-wraps errors returned from nested extraction with the nested position. That helps cases like a missing inner template, but it does nothing for the reported case, where the nested call returns no error and the misattribution happens in the outer's handling of a mismatched closing tag. Patch A's test covers only the template-not-found case, not the issue's scenario. Patch B adds the same position wrapping and also detects a closing tag whose name does not match the current shortcode. It reports either "does not evaluate .Inner ... yet a closing tag was provided" for the inner shortcode or a "closing tag does not match start tag" error, positioned at the closing tag. This addresses the root cause of the confusing error. Patch B's table-driven tests cover the issue's markdown-notation case, mismatched closing tags, deep nesting, template not found, and a valid nesting regression check. Patch B is more correct and better tested.
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 40m
Wall-clock time in the container
Full test suite
No new failures
3,490 passed · 85 skipped · 3,585 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
120vs 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/hugo_sites_build_errors_test.goindex 3209f664d..a1a2f7b81 100644@@ -637,3 +637,91 @@ line3: 'value3' b.Assert(pos.LineNumber, qt.Equals, 8) b.Assert(pos.ColumnNumber, qt.Equals, 1) }++// Errors when extracting a nested shortcode should be reported+// for and at the position of the nested shortcode, not the outer one.+func TestErrorExtractNestedShortcode(t *testing.T) {+ t.Parallel()++ filesTemplate := `+-- hugo.toml --+disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]+-- layouts/_shortcodes/figure.html --+<figure>{{ .Inner }}</figure>+-- layouts/_shortcodes/img.html --+<img src="{{ .Get "src" }}">+-- layouts/_shortcodes/figcaption.html --+<figcaption>{{ .Inner }}</figcaption>+-- layouts/_shortcodes/caption.html --+<figcaption>{{ .Get "text" }}</figcaption>+-- layouts/single.html --+{{ .Content }}+-- content/p1.md --+---+title: "p1"+---++{{< figure >}}+{{< img src="a.jpg" >}}+CONTENT+{{< /figure >}}+`++ for _, test := range []struct {+ name string+ content string+ expected string+ }{+ {+ name: "template not found",+ content: ` {{< nono >}}`,+ expected: `"/content/p1.md:7:3": failed to extract shortcode: template for shortcode "nono" not found`,+ },+ {+ name: "closing tag without inner",+ content: ` {{< img src="b.jpg" >}}{{< /img >}}`,+ expected: `"/content/p1.md:7:26": failed to extract shortcode: shortcode "img" does not evaluate .Inner or .InnerDeindent, yet a closing tag was provided`,+ },+ {+ // Issue 10672.+ name: "closing tag without inner, markdown notation",+ content: `{{% caption %}}+Snazzy 💡+{{% /caption %}}`,+ expected: `"/content/p1.md:9:1": failed to extract shortcode: shortcode "caption" does not evaluate .Inner or .InnerDeindent, yet a closing tag was provided`,+ },+ {+ name: "closing tag does not match start tag",+ content: `{{% figcaption %}}+{{< img src="b.jpg" >}}+ {{% /figure %}}+{{% /figcaption %}}`,+ expected: `"/content/p1.md:9:3": failed to extract shortcode: closing tag for shortcode "figure" does not match start tag "figcaption"`,+ },+ {+ name: "deeply nested",+ content: `{{% figcaption %}}+Snazzy+ {{< nono >}}+{{% /figcaption %}}`,+ expected: `"/content/p1.md:9:3": failed to extract shortcode: template for shortcode "nono" not found`,+ },+ } {+ t.Run(test.name, func(t *testing.T) {+ t.Parallel()+ files := strings.Replace(filesTemplate, "CONTENT", test.content, 1)+ b, err := TestE(t, files)+ b.Assert(err, qt.IsNotNil)+ b.Assert(err.Error(), qt.Contains, filepath.FromSlash(test.expected))+ })+ }++ t.Run("valid nesting", func(t *testing.T) {+ t.Parallel()+ files := strings.Replace(filesTemplate, "CONTENT", `{{% figcaption %}}+Snazzy+{{% /figcaption %}}`, 1)+ b := Test(t, files)+ b.AssertFileContent("public/p1/index.html", `<figure>`, `<img src="a.jpg">`, `<figcaption>`, `Snazzy`)+ })+}hugolib/shortcode.goindex c680620cf..7ddc5fde0 100644@@ -593,8 +593,19 @@ func (s *shortcodeParseInfo) extractShortcode(ordinal, level int, source []byte, nestedOrdinal := 0 nextLevel := level + 1 closed := false+ // Position of the left delimiter of the closing tag currently being processed.+ closingTagPos := -1 const errorPrefix = "failed to extract shortcode" + // failAt returns err as a FileError positioned at pos in source,+ // unless it's already a FileError.+ failAt := func(err error, pos int) error {+ if _, ok := err.(herrors.FileError); ok {+ return err+ }+ return herrors.NewFileErrorFromPos(err, posFromInput("", source, pos))+ }+ Loop: for { currItem := pt.Next()@@ -606,6 +617,7 @@ Loop: return sc, errors.New("shortcode has no name") } if next.IsShortcodeClose() {+ closingTagPos = currItem.Pos() continue } @@ -621,7 +633,9 @@ Loop: if err == nil { sc.inner = append(sc.inner, nested) } else {- return sc, err+ // Report the error at the position of the nested shortcode,+ // not the outer one.+ return sc, failAt(err, currItem.Pos()) } } else {@@ -655,6 +669,22 @@ 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()) {+ if closingName := next.ValStr(source); closingName != sc.name {+ // The closing tag belongs to another shortcode, e.g. a nested+ // shortcode that does not evaluate .Inner.+ var err error+ if templ := s.firstTemplateStore.LookupShortcodeByName(closingName); templ != nil && !templ.ParseInfo.IsInner {+ err = fmt.Errorf("%s: shortcode %q does not evaluate .Inner or .InnerDeindent, yet a closing tag was provided", errorPrefix, closingName)+ } else {+ err = fmt.Errorf("%s: closing tag for shortcode %q does not match start tag %q", errorPrefix, closingName, sc.name)+ }+ if closingTagPos >= 0 {+ err = failAt(err, closingTagPos)+ }+ return nil, err+ }+ } if next.IsRightShortcodeDelim() { // self-closing pt.Consume(1)
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 {