Repository: gohugoio/hugo (language: go)

Issue:
Page resource not available when containing directory name partially matches another directory
Reference: <https://discourse.gohugo.io/t/page-resources-of-index-pages-missing/53053>

This is somewhat similar to: <https://github.com/gohugoio/hugo/issues/12465>

```text
content/
├── s1/
│   └── _index.md
├── s1x/
│   └── a.txt
└── _index.md
```

In the above, content/s1x/a.txt should be a page resource for content/_index.md. But it isn't because the `s1x` directory name starts with `s1`.

Failing test

```go
func TestFoo(t *testing.T) {
	t.Parallel()

	files := `
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
-- content/_index.md --
---
title: home
---
-- content/s1/_index.md --
---
title: s1
---
-- content/s1x/a.txt --
a.txt
-- layouts/index.html --
{{ range .Resources.Match "**" }}
  {{ .Name }}|
{{ end }}
`

	b := hugolib.Test(t, files)

	b.AssertFileContent("public/index.html", "s1x/a.txt|")
}
```



Discussion / hints:

Fix patch:
diff --git a/hugolib/content_map_page.go b/hugolib/content_map_page.go
index b369dfe13fd..b48504203d7 100644
--- a/hugolib/content_map_page.go
+++ b/hugolib/content_map_page.go
@@ -509,8 +509,23 @@ func (m *pageMap) forEachResourceInPage(
 			// A page key points to the logical path of a page, which when sourced from the filesystem
 			// may represent a directory (bundles) or a single content file (e.g. p1.md).
 			// So, to avoid any overlapping ambiguity, we start looking from the owning directory.
-			ownerKey, _ := m.treePages.LongestPrefixAll(path.Dir(resourceKey))
-			if ownerKey != keyPage {
+			s := resourceKey
+
+			for {
+				s = path.Dir(s)
+				ownerKey, found := m.treePages.LongestPrefixAll(s)
+				if !found {
+					return true, nil
+				}
+				if ownerKey == keyPage {
+					break
+				}
+
+				if s != ownerKey && strings.HasPrefix(s, ownerKey) {
+					// Keep looking
+					continue
+				}
+
 				// Stop walking downwards, someone else owns this resource.
 				rw.SkipPrefix(ownerKey + "/")
 				return false, nil


Test patch:
diff --git a/hugolib/content_map_test.go b/hugolib/content_map_test.go
index a9f719f4a83..6a12458462f 100644
--- a/hugolib/content_map_test.go
+++ b/hugolib/content_map_test.go
@@ -361,6 +361,35 @@ p1-foo.txt
 	b.AssertFileExists("public/s1/p1/index.html", true)
 }
 
+// Issue 13228.
+func TestBranchResourceOverlap(t *testing.T) {
+	t.Parallel()
+
+	files := `
+-- hugo.toml --
+disableKinds = ['page','rss','section','sitemap','taxonomy','term']
+-- content/_index.md --
+---
+title: home
+---
+-- content/s1/_index.md --
+---
+title: s1
+---
+-- content/s1x/a.txt --
+a.txt
+-- layouts/index.html --
+Home.
+{{ range .Resources.Match "**" }}
+  {{ .Name }}|
+{{ end }}
+`
+
+	b := Test(t, files)
+
+	b.AssertFileContent("public/index.html", "s1x/a.txt|")
+}
+
 func TestSitemapOverrideFilename(t *testing.T) {
 	t.Parallel()