Backtestshugorun-20260922-141813#15207
panic: slice bounds out of range in contentNodeShifter.Delete when removing draft pages from a multilingual taxonomy
The merged fix was judged better than our patch.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches correctly diagnose the root cause: the `contentNodes` case of `Delete` mutates the slice with `v = append(v[:i], v[i+1:]...)` while ranging over the original header, so when two or more nodes match the delete vector the reslice indices run past the shrunken length and panic `[5:4]` — exactly the reported crash and the `0x35`/multi-page-in-one-taxonomy-term trigger. The difference is completeness. Patch B applies a minimal filter-in-place (write-pointer `n`, `v = v[:n]`) that stops the panic, but it (a) leaves the compacted slice unwritten to the tree — the doctree still holds the original-length header whose stale tail now contains the deleted nodes AND a duplicated *kept* node (e.g. `[keep0,keep2,keep5,del3,del4,keep5]`); the deleted ones are MarkStale'd but the duplicated kept node is not, so a surviving page can be double-referenced — and (b) ignores the identical mutate-while-ranging bug in `DeleteFunc`'s `contentNodes` case, leaving a second latent panic of the same class unfixed. Patch A restructures the loop to build fresh `remaining`/`deleted` slices (no shared-backing-array aliasing), extends the shifter interface so `Delete`/`DeleteFunc` return the updated node, and re-inserts that compacted node into the tree (`r.tree.Insert(key, updated)`) whenever something was deleted but the node isn't empty — so the tree ends up holding exactly the remaining nodes, and it fixes the twin `DeleteFunc` bug too. That is the true root-cause fix rather than a panic-suppression that relies on downstream staleness to mask a corrupt slice. On tests both are meaningful (interleaved and all-match cases that panic pre-fix); B's are tied more directly to the reported scenario, A's additionally assert the remaining-slice contents are correct, though A leaves its own changed `DeleteFunc` untested. Patch B wins clearly on scope — A's interface-signature change ripples through nodeshifttree.go, the interface definition, and the doctree test shifter, which is real churn. But correctness outranks scope, and A's larger footprint is the necessary cost of a complete fix: it eliminates the duplicate/stale-reference hazard B introduces and closes the second bug B leaves open. The scope cost is not enough to offset A's correctness advantage, so this is not a tie.
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
25m 9s21 turns
Wall-clock time in the container
Full test suite
No new failures
3,694 passed · 99 skipped · 3,806 total
Contamination audit
Unknown
See the audit notes for this run
Patch size
126vs 161 merged
Changed lines, ours vs merged
The issue
View on GitHubWhat version of Hugo are you using?
Reproduced on both:
hugo v0.159.1-86c7d3afacab79dc53325602d77ef884b7570268+extended darwin/arm64
hugo v0.164.0+extended+withdeploy darwin/arm64
Does this issue reproduce with the latest release?
Yes (0.164.0).
What did you expect to see?
A production build (no -D) of a multilingual site containing draft pages completes.
What did you see instead?
panic: runtime error: slice bounds out of range [5:4]
goroutine 37 [running]:
github.com/gohugoio/hugo/hugolib.(*contentNodeShifter).Delete(0x..., {0x..., 0x...}, {0x2, 0x0, 0x0})
github.com/gohugoio/hugo/hugolib/content_map_page_contentnodeshifter.go:48 +0x5f4
github.com/gohugoio/hugo/hugolib/doctree.(*NodeShiftTree[...]).delete(0x..., {0x..., 0x35})
github.com/gohugoio/hugo/hugolib/doctree/nodeshifttree.go:165 +0x84
github.com/gohugoio/hugo/hugolib/doctree.(*NodeShiftTree[...]).Delete(...)
github.com/gohugoio/hugo/hugolib/doctree/nodeshifttree.go:107
github.com/gohugoio/hugo/hugolib.(*allPagesAssembler).createAllPages.func1-range1(...)
github.com/gohugoio/hugo/hugolib/content_map_page_assembler.go:106 +0xa4
github.com/gohugoio/hugo/hugolib.(*allPagesAssembler).createAllPages(...)
github.com/gohugoio/hugo/hugolib/content_map_page_assembler.go:138 +0x168
github.com/gohugoio/hugo/hugolib.(*HugoSites).assemble(...)
github.com/gohugoio/hugo/hugolib/hugo_sites_build.go:345 +0x41c
The 0x35 in delete(..., {…, 0x35}) is 53, exactly the byte length of the key being removed (/blog/ plus a 47-character slug).
Only on the page-removal path
hugo -D (drafts included) builds fine. The panic appears only when a page is removed from the tree, which makes it invisible in local preview and fatal only in production.
A future publishDate panics identically. Setting [_build] render = "never" / list = "never" on the same pages avoids it entirely — consistent with those pages never being added to the tree, so nothing is deleted.
What I ruled out
Site: 8 languages, ~1,490 pages per language, ~11,455 content files, categories taxonomy.
- Not a regression. 0.159.1 and 0.164.0 behave identically.
- Not the number of drafts. 96 draft pages build fine; 16 panic.
- Not slug length. The panicking key was 47 chars, but renaming an unrelated post to exactly 47 chars builds fine.
- Not the slug. Renaming the offending page does not help.
- Not byte layout. Deleting a front-matter line, or adding a blank one, still panics.
Swapping page content at a fixed path isolates it to front matter. Changing categories = ["Airport Operations"] on the offending page to any other value clears the panic — and so does moving a different page out of that same category while leaving the offending one alone. So the trigger is combinatorial: some particular set of pages being deleted from one taxonomy term corrupts the shifter, rather than any single page being malformed.
Reproducer
I have not managed to minimise it, and I want to be upfront about that. Synthetic multilingual sites (8 languages, 12 blog + 200 glossary pages sharing one taxonomy term, varying draft counts) do not reproduce, so something else in the real tree is required. The affected repository is private.
I am happy to run patches or instrumented builds against the real site and report back, which may be faster than guessing at a minimal case.
The two patches
hugolib/content_map_page_contentnodeshifter.goindex 04f632f75..bc2c958e0 100644@@ -42,12 +42,16 @@ func (s *contentNodeShifter) Delete(n contentNode, vec sitesmatrix.Vector) (cont return v, true, true case contentNodes: var deleted contentNodes- for i, nn := range v {+ n := 0+ for _, nn := range v { if vv, ok, _ := s.Delete(nn, vec); ok { deleted = append(deleted, vv)- v = append(v[:i], v[i+1:]...)+ continue }+ v[n] = nn+ n++ }+ v = v[:n] if len(deleted) == 0 { return nil, false, false }hugolib/content_map_page_contentnodeshifter_test.gonew file mode 100644index 000000000..6a6fde9d0@@ -0,0 +1,118 @@+// Copyright 2025 The Hugo Authors. All rights reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package hugolib++import (+ "testing"++ qt "github.com/frankban/quicktest"+ "github.com/gohugoio/hugo/hugolib/sitesmatrix"+)++// testShifterNode is a minimal contentNodeForSite used to exercise+// contentNodeShifter.Delete's slice handling in isolation.+type testShifterNode struct {+ path string+ vec sitesmatrix.Vector+}++func (n *testShifterNode) Path() string { return n.path }++func (n *testShifterNode) siteVector() sitesmatrix.Vector { return n.vec }++func (n *testShifterNode) forEeachContentNode(f func(sitesmatrix.Vector, contentNode) bool) bool {+ return f(n.vec, n)+}++// Issue 15207: contentNodeShifter.Delete panicked with "slice bounds out of+// range" when more than one node in a contentNodes slice matched the site+// vector being deleted, e.g. draft or future-dated pages removed from a+// multilingual taxonomy. The loop mutated the slice while ranging over it.+func TestContentNodeShifterDeleteMultipleMatchesIssue15207(t *testing.T) {+ t.Parallel()++ c := qt.New(t)++ del := sitesmatrix.Vector{2, 0, 0}+ keep := sitesmatrix.Vector{1, 0, 0}++ // A contentNodes slice where several nodes share the vector being deleted,+ // interleaved with nodes that must be kept. Before the fix this panics in+ // the v = append(v[:i], v[i+1:]...) line.+ nodes := contentNodes{+ &testShifterNode{path: "/blog/a", vec: keep},+ &testShifterNode{path: "/blog/a", vec: del},+ &testShifterNode{path: "/blog/a", vec: keep},+ &testShifterNode{path: "/blog/a", vec: del},+ &testShifterNode{path: "/blog/a", vec: del},+ &testShifterNode{path: "/blog/a", vec: keep},+ }++ s := &contentNodeShifter{}++ deleted, wasDeleted, isEmpty := s.Delete(nodes, del)++ c.Assert(wasDeleted, qt.IsTrue)+ c.Assert(isEmpty, qt.IsFalse)++ dn, ok := deleted.(contentNodes)+ c.Assert(ok, qt.IsTrue)+ c.Assert(len(dn), qt.Equals, 3)+ for _, n := range dn {+ c.Assert(n.(*testShifterNode).siteVector(), qt.Equals, del)+ }+}++// Issue 15207: when every node in the slice matches the delete vector the+// mutate-while-ranging bug panics with "slice bounds out of range" (the+// crash reported for a production build removing draft pages from a+// multilingual taxonomy). Assert Delete completes and empties the slice.+func TestContentNodeShifterDeleteAllMatchesNoPanicIssue15207(t *testing.T) {+ t.Parallel()++ c := qt.New(t)++ del := sitesmatrix.Vector{2, 0, 0}++ nodes := contentNodes{+ &testShifterNode{path: "/blog/a", vec: del},+ &testShifterNode{path: "/blog/a", vec: del},+ &testShifterNode{path: "/blog/a", vec: del},+ &testShifterNode{path: "/blog/a", vec: del},+ }++ s := &contentNodeShifter{}++ var (+ deleted contentNode+ wasDeleted bool+ isEmpty bool+ )++ func() {+ defer func() {+ if r := recover(); r != nil {+ c.Fatalf("Delete panicked: %v", r)+ }+ }()+ deleted, wasDeleted, isEmpty = s.Delete(nodes, del)+ }()++ c.Assert(wasDeleted, qt.IsTrue)+ c.Assert(isEmpty, qt.IsTrue)++ dn, ok := deleted.(contentNodes)+ c.Assert(ok, qt.IsTrue)+ c.Assert(len(dn), qt.Equals, 4)+}
hugolib/content_map_page_contentnodeshifter.goindex 04f632f75..2d8bfa105 100644@@ -25,7 +25,7 @@ type contentNodeShifter struct { conf config.AllProvider // Used for logging/debugging. } -func (s *contentNodeShifter) Delete(n contentNode, vec sitesmatrix.Vector) (contentNode, bool, bool) {+func (s *contentNodeShifter) Delete(n contentNode, vec sitesmatrix.Vector) (contentNode, contentNode, bool, bool) { switch v := n.(type) { case contentNodesMap: deleted, wasDeleted := v[vec]@@ -33,49 +33,53 @@ func (s *contentNodeShifter) Delete(n contentNode, vec sitesmatrix.Vector) (cont delete(v, vec) resource.MarkStale(deleted) }- return deleted, wasDeleted, len(v) == 0+ return v, deleted, wasDeleted, len(v) == 0 case contentNodeForSite: if v.siteVector() != vec {- return nil, false, false+ return v, nil, false, false } resource.MarkStale(v)- return v, true, true+ return nil, v, true, true case contentNodes:- var deleted contentNodes- for i, nn := range v {- if vv, ok, _ := s.Delete(nn, vec); ok {+ var deleted, remaining contentNodes+ for _, nn := range v {+ updated, vv, ok, isEmpty := s.Delete(nn, vec)+ if ok { deleted = append(deleted, vv)- v = append(v[:i], v[i+1:]...)+ }+ if !isEmpty {+ remaining = append(remaining, updated) } } if len(deleted) == 0 {- return nil, false, false+ return v, nil, false, false }- return deleted, true, len(v) == 0+ return remaining, deleted, true, len(remaining) == 0 default:- v = v.(contentNodeSingle) // Ensure single node.- resource.MarkStale(v)- return v, true, true-+ vv := v.(contentNodeSingle) // Ensure single node.+ resource.MarkStale(vv)+ return nil, vv, true, true } } -func (s *contentNodeShifter) DeleteFunc(v contentNode, f func(n contentNode) bool) bool {+func (s *contentNodeShifter) DeleteFunc(v contentNode, f func(n contentNode) bool) (contentNode, bool) { switch ss := v.(type) { case contentNodeSingle: if f(ss) { resource.MarkStale(ss)- return true+ return nil, true }- return false+ return ss, false case contentNodes:- for i, n := range ss {+ var remaining contentNodes+ for _, n := range ss { if f(n) { resource.MarkStale(n)- ss = append(ss[:i], ss[i+1:]...)+ } else {+ remaining = append(remaining, n) } }- return len(ss) == 0+ return remaining, len(remaining) == 0 case contentNodesMap: for k, n := range ss { if f(n) {@@ -83,7 +87,7 @@ func (s *contentNodeShifter) DeleteFunc(v contentNode, f func(n contentNode) boo delete(ss, k) } }- return len(ss) == 0+ return ss, len(ss) == 0 default: panic(fmt.Sprintf("DeleteFunc: unknown type %T", v)) }hugolib/content_map_page_contentnodeshifter_test.gonew file mode 100644index 000000000..f8d2c07bc@@ -0,0 +1,79 @@+// Copyright 2025 The Hugo Authors. All rights reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package hugolib++import (+ "testing"++ qt "github.com/frankban/quicktest"+ "github.com/gohugoio/hugo/hugolib/sitesmatrix"+)++type testContentNodeForSite struct {+ vec sitesmatrix.Vector+}++func (n *testContentNodeForSite) Path() string { return "/test" }+func (n *testContentNodeForSite) nodeCategorySingle() {}+func (n *testContentNodeForSite) siteVector() sitesmatrix.Vector { return n.vec }+func (n *testContentNodeForSite) forEeachContentNode(f func(sitesmatrix.Vector, contentNode) bool) bool {+ return f(n.vec, n)+}++// See issue 15207.+func TestContentNodeShifterDeleteMultipleFromNodes(t *testing.T) {+ c := qt.New(t)++ s := &contentNodeShifter{}+ vec := sitesmatrix.Vector{2, 0, 0}+ other := sitesmatrix.Vector{1, 0, 0}++ newNodes := func() contentNodes {+ return contentNodes{+ &testContentNodeForSite{vec: other},+ &testContentNodeForSite{vec: vec},+ &testContentNodeForSite{vec: other},+ &testContentNodeForSite{vec: other},+ &testContentNodeForSite{vec: vec},+ }+ }++ updated, deleted, wasDeleted, isEmpty := s.Delete(newNodes(), vec)+ c.Assert(wasDeleted, qt.IsTrue)+ c.Assert(isEmpty, qt.IsFalse)+ c.Assert(deleted.(contentNodes), qt.HasLen, 2)+ remaining := updated.(contentNodes)+ c.Assert(remaining, qt.HasLen, 3)+ for _, n := range remaining {+ c.Assert(n.(contentNodeForSite).siteVector(), qt.Equals, other)+ }++ // Delete all.+ updated, deleted, wasDeleted, isEmpty = s.Delete(contentNodes{+ &testContentNodeForSite{vec: vec},+ &testContentNodeForSite{vec: vec},+ }, vec)+ c.Assert(wasDeleted, qt.IsTrue)+ c.Assert(isEmpty, qt.IsTrue)+ c.Assert(deleted.(contentNodes), qt.HasLen, 2)+ c.Assert(updated, qt.IsNil)++ // Delete none.+ nodes := contentNodes{&testContentNodeForSite{vec: other}}+ updated, deleted, wasDeleted, isEmpty = s.Delete(nodes, vec)+ c.Assert(wasDeleted, qt.IsFalse)+ c.Assert(isEmpty, qt.IsFalse)+ c.Assert(deleted, qt.IsNil)+ c.Assert(updated.(contentNodes), qt.HasLen, 1)+}hugolib/doctree/nodeshiftree_test.goindex 1d17a4129..a969b43fc 100644@@ -258,12 +258,15 @@ func (s *testShifter) Insert(old, new *testValue) (*testValue, *testValue, bool) return new, old, true } -func (s *testShifter) Delete(n *testValue, dimension sitesmatrix.Vector) (*testValue, bool, bool) {- return nil, true, true+func (s *testShifter) Delete(n *testValue, dimension sitesmatrix.Vector) (*testValue, *testValue, bool, bool) {+ return nil, n, true, true } -func (s *testShifter) DeleteFunc(v *testValue, f func(*testValue) bool) bool {- return f(v)+func (s *testShifter) DeleteFunc(v *testValue, f func(*testValue) bool) (*testValue, bool) {+ if f(v) {+ return nil, true+ }+ return v, false } func (s *testShifter) Shift(n *testValue, dimension sitesmatrix.Vector, fallback bool) (v *testValue, ok bool) {hugolib/doctree/nodeshifttree.goindex 0ccf3eba3..f2cec9f26 100644@@ -49,12 +49,14 @@ type ( // and a bool indicating if an existing record is updated. Insert(old, new T) (T, T, bool) - // Delete deletes T from the given dimension and returns the deleted T and whether the dimension was deleted and if it's empty after the delete.- Delete(v T, dimension sitesmatrix.Vector) (T, bool, bool)+ // Delete deletes T from the given dimension.+ // It returns the updated T, the deleted T, whether anything was deleted+ // and whether T is empty after the delete.+ Delete(v T, dimension sitesmatrix.Vector) (T, T, bool, bool) - // DeleteFunc deletes nodes in v from the tree where the given function returns true.- // It returns true if it's empty after the delete.- DeleteFunc(v T, f func(n T) bool) bool+ // DeleteFunc deletes nodes in v where the given function returns true.+ // It returns the updated T and whether it's empty after the delete.+ DeleteFunc(v T, f func(n T) bool) (T, bool) // Shift shifts v into the given dimension, // if fallback is true, it will fall back a fallback match if found.@@ -118,7 +120,7 @@ func (r *NodeShiftTree[T]) DeleteFuncRaw(key string, f func(T) bool) (T, int) { return lastDeleted, count } - isEmpty := r.shifter.DeleteFunc(v, func(n T) bool {+ updated, isEmpty := r.shifter.DeleteFunc(v, func(n T) bool { if f(n) { count++ lastDeleted = n@@ -129,6 +131,8 @@ func (r *NodeShiftTree[T]) DeleteFuncRaw(key string, f func(T) bool) (T, int) { if isEmpty { r.tree.Delete(key)+ } else if count > 0 {+ r.tree.Insert(key, updated) } return lastDeleted, count@@ -161,10 +165,15 @@ func (r *NodeShiftTree[T]) delete(key string) (T, bool) { var wasDeleted bool var deleted T if v, ok := r.tree.Get(key); ok {- var isEmpty bool- deleted, wasDeleted, isEmpty = r.shifter.Delete(v, r.siteVector)+ var (+ updated T+ isEmpty bool+ )+ updated, deleted, wasDeleted, isEmpty = r.shifter.Delete(v, r.siteVector) if isEmpty { r.tree.Delete(key)+ } else if wasDeleted {+ r.tree.Insert(key, updated) } } return deleted, wasDeleted
What the agent reported
All changes are in place and uncommitted, as required. The AGENTS.md/CLAUDE.md deletions were pre-existing in the starting snapshot, not part of this work.
Summary
What was wrong
contentNodeShifter.Delete (hugolib/content_map_page_contentnodeshifter.go) mutated a slice while ranging over it. In the case contentNodes: branch it did v = append(v[:i], v[i+1:]...) inside for i, nn := range v. The range fixes its bound to the original length, but each match shrinks v, so once i+1 exceeds the shrunken length the expression v[i+1:] panics with slice bounds out of range (the reported [5:4]). Other interleavings silently drop the wrong elements instead.
This path is hit from createAllPages's droppedPages cleanup (content_map_page_assembler.go:102-110) → treePages.Delete → NodeShiftTree.delete → shifter.Delete. It fires only when a page is removed from the tree — exactly the draft (no -D) and future-publishDate production-build cases in the report, and only when a single tree key holds a multi-element contentNodes slice with several nodes matching the site vector being deleted (the combinatorial multilingual-taxonomy condition the reporter observed). render/list = "never" avoids it because those pages are never added, so nothing is deleted.
What I changed
One branch in Delete: replaced the mutate-while-ranging loop with a filter-in-place using a separate write index (v[n] = nn; n++, then v = v[:n]). This deletes exactly the matching nodes, keeps the rest, and computes isEmpty correctly. No other branch or method was touched. 8 lines changed.
Test evidence (via ./bt-test ./hugolib/ ..., run in Docker)
New file hugolib/content_map_page_contentnodeshifter_test.go, two tests exercising the exact scenario (a contentNodes slice with multiple nodes matching one delete vector):
TestContentNodeShifterDeleteMultipleMatchesIssue15207(mixed keep/delete) — before fix: FAIL (silent data loss, got 2 want 3); after: PASS.TestContentNodeShifterDeleteAllMatchesNoPanicIssue15207(all match,recover-guarded) — before fix: FAIL with the literalruntime error: slice bounds out of range [3:2]; after: PASS.
Gate run (both regression tests + all adjacent taxonomy tests including the draft-specific and multilingual ones): 20/20 PASS, 0 fail — including TestTaxonomiesNotForDrafts, TestTaxonomiesIndexDraft, TestTaxonomiesHomeDraft, TestTaxonomiesRemoveOne, and TestTaxonomyNestedEmptySectionsIssue12188. No previously passing test regressed.
Blast radius: The only caller is NodeShiftTree.delete, which uses the returned isEmpty/wasDeleted (not the slice), and the fix preserves that contract exactly. The change is strictly a correctness fix on the page-removal path.
Requirements checklist
- Production build (no
-D) of a multilingual draft-in-taxonomy site must complete (the panic) — ✅ Implemented and covered. Root cause fixed; both regression tests failed before and pass after, one reproducing the literalslice bounds out of rangepanic. - Future
publishDatepanics identically — ✅ Same code path (both draft and future-date go throughshouldBuild→droppedPages→treePages.Delete→shifter.Delete); the single fix covers both. Verified by reading the drop path at content_map_page_assembler.go:276-289 (Kind-independent drop of non-Home/Section/Taxonomy pages). render/list = "never"avoids it — ✅ Consistent-with-diagnosis, not a code change: those pages are never inserted, soDeleteis never called; nothing in the fix alters that. No change needed.- Reproducer / minimisation — the reporter could not minimise a synthetic multilingual site, and neither is that required: the fix targets the precise faulting function with a deterministic unit-level reproduction of the exact triggering state (a
contentNodesslice with multiple vector matches), which is both the panic and the silent-corruption manifestation.
One noted-but-not-fixed item (out of scope): DeleteFunc at content_map_page_contentnodeshifter.go:75-82 has the identical mutate-while-ranging pattern (reachable via DeleteFuncRaw). It is a different code path, not implicated in this report, and I have no failing test for it, so per the minimal-change rule I left it untouched. It is a recommended follow-up: applying the same filter-in-place pattern would resolve it.