Backtestshugorun-20260930-140149#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.
In the issue, `img` has a template that uses `.Inner`, so the parser expects `img` to be closed. It never is. The parser nests `figcaption` inside `img`, then accepts `{{< /figure >}}` as the closer for `img` without checking the name. `figure` then reaches EOF and is reported as the unclosed shortcode. The root cause is that missing name check on closing tags.
Patch A adds exactly that check: when a closing tag's name differs from `sc.name`, it returns "shortcode %q must be closed or self-closed" for the inner shortcode. It also wraps nested errors with the nested item's position. Its test reproduces the issue's figure/img/figcaption structure. It asserts the new message names `img` and does not mention `figure`, and it checks the error position. That test would fail before the change.
Patch B only wraps nested extraction errors with the nested shortcode's position. That helps when the inner error is something like a missing template. It does nothing for the reported case: the mismatched close still silently closes `img`, and the error still says unclosed shortcode "figure". Its test covers a different scenario (inner template not found) rather than the issue's.
A fixes the root cause and tests the reported scenario. B addresses an adjacent symptom.
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
9m 38s13 turns
Wall-clock time in the container
Full test suite
No new failures
3,482 passed · 85 skipped · 3,579 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
48vs 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..073fd24ed 100644@@ -421,6 +421,43 @@ line 4 b.Assert(errors[3].ErrorContext().Lines, qt.DeepEquals, []string{"line 1", "line 2", "123{{ .ThisDoesNotExist }}", "line 4"}) } +func TestErrorNestedUnclosedShortcodeShouldReferenceInner(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+-- content/_index.md --+---+title: "Home"+---+{{< figure >}}+{{< img src="x.jpg" >}}+{{% figcaption %}}+Snazzy+{{% /figcaption %}}+{{< /figure >}}+-- layouts/home.html --+{{ .Content }}+-- layouts/_shortcodes/figure.html --+<figure>{{ .Inner }}</figure>+-- layouts/_shortcodes/img.html --+<img>{{ .Inner }}+-- layouts/_shortcodes/figcaption.html --+<figcaption>{{ .Inner }}</figcaption>+`++ b, err := TestE(t, files)++ 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), `shortcode "figure"`)++ errors := herrors.UnwrapFileErrorsWithErrorContext(err)+ b.Assert(errors, qt.Not(qt.HasLen), 0)+ b.Assert(errors[0].Position().LineNumber, qt.Equals, 5)+ b.Assert(errors[0].Position().ColumnNumber, qt.Equals, 1)+}+ func TestErrorRenderHookHeading(t *testing.T) { t.Parallel() hugolib/shortcode.goindex c680620cf..a4652f101 100644@@ -618,11 +618,13 @@ Loop: s.addName(nested.name) } - if err == nil {- sc.inner = append(sc.inner, nested)- } else {+ if err != nil {+ if _, ok := err.(herrors.FileError); !ok {+ err = herrors.NewFileErrorFromPos(err, posFromInput("", source, currItem.Pos()))+ } return sc, err }+ sc.inner = append(sc.inner, nested) } else { sc.doMarkup = currItem.IsShortcodeMarkupDelimiter()@@ -659,6 +661,9 @@ Loop: // self-closing pt.Consume(1) } else {+ if !next.IsError() && next.ValStr(source) != sc.name {+ return sc, fmt.Errorf("%s: shortcode %q must be closed or self-closed", errorPrefix, sc.name)+ } sc.isClosing = true pt.Consume(2) }
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 {
What the agent reported
The error now names the inner shortcode that is actually unclosed (img) and points at its line. The new regression test fails without the fix and passes with it. ./bt-test could not run, so all test evidence comes from running go test directly.
What was wrong: In extractShortcode (hugolib/shortcode.go), a closing tag's name was never checked against the shortcode it was closing. In the reported content, img's template uses .Inner, so Hugo expects a closing tag for it. img took {{< /figure >}} as its own closing tag, which left figure reaching end of file unclosed. The error therefore blamed figure, at figure's line (4:1). Also, errors from a nested shortcode were reported at the outer shortcode's position.
What I changed (hugolib/shortcode.go, two small edits):
- Name check: if a shortcode that needs
.Innermeets a closing tag with a different name, it now fails withshortcode "img" must be closed or self-closed. Self-closing tags, the existing "does not evaluate .Inner … yet a closing tag was provided" error, and errors the parser itself reports behave as before. - Position: errors from a nested shortcode now carry that shortcode's own start position, unless they already have one. This is the same pattern
fail()inpage__content.gouses for top-level shortcodes.
Test: I added TestErrorNestedUnclosedShortcodeShouldReferenceInner in hugolib/hugo_sites_build_errors_test.go, next to TestErrorNestedShortcode. It builds the exact figure / img / figcaption content from the report and checks that the error names "img", does not mention "figure", and is reported at line 5, column 1.
Test evidence:
- The gate couldn't run:
./bt-testexits 1 without running any tests. Its report shows the build failing because it can't download Go modules (lookup proxy.golang.org ... server misbehaving). So I rango testdirectly withGOMODCACHE=/bt/cache/go-mod GOPROXY=off. - Without the fix: the new test fails with
"/content/_index.md:4:1": failed to extract shortcode: shortcode "figure" must be closed or self-closed. - With the fix: the new test passes, as do the other error and shortcode tests (
-run 'TestErrorNested|TestSiteBuildErrors|Shortcode|Inner') and./parser/.... - Full
./hugolib/run (done by the implementer subagent): everything passes except three tests that try to download modules (TestModulePrivateRepoand twoTestGitInfoFromGitModule…tests). They fail withpermission deniedon the module cache, and the implementer reported they fail the same way without the fix.
Blast radius: only shortcode extraction is affected. Content that was already malformed but used to build will now error. For example, in {{< a >}}{{< b >}}text{{< /a >}}, where a doesn't use .Inner, b used to quietly accept /a as its closing tag. The reviewer checked path names ({{< /a/b >}}), inline shortcodes, {{% %}} closing tags and self-closing tags, and none of them change.
Requirements:
- The error names the inner shortcode, not the outer one: implemented; shown by the new test.
- The "is closed" check misses shortcodes nested inside another shortcode's
.Inner: fixed by the closing-name check; same test. - "Before v0.111.0 this compiled fine": not restored, on purpose. The content really does leave
imgunclosed, and the report only asks for a correct error. To build, the user needs to self-close it ({{< img ... />}}) or remove.Innerfrom theimgtemplate.
The changes are uncommitted. AGENTS.md and CLAUDE.md were already deleted in the working tree before this session, and I left them alone.