Fix index out of range panic in fileEventsContentPaths

The nested loop had dirs as the outer loop and others as the inner loop
with a single counter, causing n to exceed len(others) when multiple
dirs existed. Swap the loop order so each file in others is checked
against all dirs exactly once.

Fixes #14573

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Bjørn Erik Pedersen
2026-02-27 10:21:12 +01:00
parent e8577771c3
commit f797f84902
3 changed files with 46 additions and 7 deletions
+9
View File
@@ -751,6 +751,15 @@ func (s *IntegrationTestBuilder) AddFiles(filenameContent ...string) *Integratio
return s
}
func (s *IntegrationTestBuilder) CreateDirs(dirnames ...string) *IntegrationTestBuilder {
for _, dirname := range dirnames {
absDir := s.absFilename(filepath.FromSlash(dirname))
s.Assert(s.fs.Source.MkdirAll(absDir, 0o777), qt.IsNil)
s.createdFiles = append(s.createdFiles, absDir)
}
return s
}
func (s *IntegrationTestBuilder) RemoveFiles(filenames ...string) *IntegrationTestBuilder {
for _, filename := range filenames {
absFilename := s.absFilename(filename)
+27
View File
@@ -399,6 +399,33 @@ func TestRebuilErrorRecovery(t *testing.T) {
b.EditFileReplaceAll("content/mysection/mysectionbundle/index.md", "{{< foo }}", "{{< foo >}}").Build()
}
// Issue 14573
func TestRebuildAddContentWithMultipleDirCreations(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
baseURL = "https://example.com"
disableLiveReload = true
-- content/p1.md --
---
title: "P1"
---
-- layouts/page.html --
Single: {{ .Title }}|{{ .Content }}|
-- layouts/list.html --
Pages: {{ range .RegularPages }}{{ .RelPermalink }}|{{ end }}$
`
b := TestRunning(t, files)
b.AssertFileContent("public/index.html", "Pages: /p1/|$")
b.AddFiles(
"content/nesteddir/dir1/post.md", "---\ntitle: Post\n---",
).CreateDirs(
"content/dir1",
"content/dir2",
).Build()
b.AssertFileContent("public/nesteddir/dir1/post/index.html", "Single: Post|")
}
func TestRebuildAddPageListPagesInHome(t *testing.T) {
files := `
-- hugo.toml --
+10 -7
View File
@@ -1316,15 +1316,18 @@ func (h *HugoSites) fileEventsContentPaths(p []pathChange) []pathChange {
// Remove all files below dir.
if len(dirs) > 0 {
n := 0
for _, d := range dirs {
dir := d.p.Path() + "/"
for _, o := range others {
if !strings.HasPrefix(o.p.Path(), dir) {
others[n] = o
n++
for _, o := range others {
keep := true
for _, d := range dirs {
if strings.HasPrefix(o.p.Path(), d.p.Path()+"/") {
keep = false
break
}
}
if keep {
others[n] = o
n++
}
}
others = others[:n]
}