Skip to content

Commit f797f84

Browse files
bepclaude
andcommitted
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>
1 parent e857777 commit f797f84

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

hugolib/integrationtest_builder.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,15 @@ func (s *IntegrationTestBuilder) AddFiles(filenameContent ...string) *Integratio
751751
return s
752752
}
753753

754+
func (s *IntegrationTestBuilder) CreateDirs(dirnames ...string) *IntegrationTestBuilder {
755+
for _, dirname := range dirnames {
756+
absDir := s.absFilename(filepath.FromSlash(dirname))
757+
s.Assert(s.fs.Source.MkdirAll(absDir, 0o777), qt.IsNil)
758+
s.createdFiles = append(s.createdFiles, absDir)
759+
}
760+
return s
761+
}
762+
754763
func (s *IntegrationTestBuilder) RemoveFiles(filenames ...string) *IntegrationTestBuilder {
755764
for _, filename := range filenames {
756765
absFilename := s.absFilename(filename)

hugolib/rebuild_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,33 @@ func TestRebuilErrorRecovery(t *testing.T) {
399399
b.EditFileReplaceAll("content/mysection/mysectionbundle/index.md", "{{< foo }}", "{{< foo >}}").Build()
400400
}
401401

402+
// Issue 14573
403+
func TestRebuildAddContentWithMultipleDirCreations(t *testing.T) {
404+
t.Parallel()
405+
files := `
406+
-- hugo.toml --
407+
baseURL = "https://example.com"
408+
disableLiveReload = true
409+
-- content/p1.md --
410+
---
411+
title: "P1"
412+
---
413+
-- layouts/page.html --
414+
Single: {{ .Title }}|{{ .Content }}|
415+
-- layouts/list.html --
416+
Pages: {{ range .RegularPages }}{{ .RelPermalink }}|{{ end }}$
417+
`
418+
b := TestRunning(t, files)
419+
b.AssertFileContent("public/index.html", "Pages: /p1/|$")
420+
b.AddFiles(
421+
"content/nesteddir/dir1/post.md", "---\ntitle: Post\n---",
422+
).CreateDirs(
423+
"content/dir1",
424+
"content/dir2",
425+
).Build()
426+
b.AssertFileContent("public/nesteddir/dir1/post/index.html", "Single: Post|")
427+
}
428+
402429
func TestRebuildAddPageListPagesInHome(t *testing.T) {
403430
files := `
404431
-- hugo.toml --

hugolib/site.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,15 +1316,18 @@ func (h *HugoSites) fileEventsContentPaths(p []pathChange) []pathChange {
13161316
// Remove all files below dir.
13171317
if len(dirs) > 0 {
13181318
n := 0
1319-
for _, d := range dirs {
1320-
dir := d.p.Path() + "/"
1321-
for _, o := range others {
1322-
if !strings.HasPrefix(o.p.Path(), dir) {
1323-
others[n] = o
1324-
n++
1319+
for _, o := range others {
1320+
keep := true
1321+
for _, d := range dirs {
1322+
if strings.HasPrefix(o.p.Path(), d.p.Path()+"/") {
1323+
keep = false
1324+
break
13251325
}
13261326
}
1327-
1327+
if keep {
1328+
others[n] = o
1329+
n++
1330+
}
13281331
}
13291332
others = others[:n]
13301333
}

0 commit comments

Comments
 (0)