Repository: gohugoio/hugo (language: go)

Issue:
Wrong .Parent with pages with overlapping names (e.g. sdk and sdk_exporters)
The issue title is a big vague for now.

Reference: <https://discourse.gohugo.io/t/subpage-missing-from-sections-page-list-when-using-0-123-8/48789>

Minimal reproducible example:

```text
git clone --single-branch -b hugo-forum-topic-48789 https://github.com/jmooring/hugo-testing hugo-forum-topic-48789
cd hugo-forum-topic-48789
npm ci
hugo server
```

Then visit <http://localhost:1313/docs/logs/> to see the problem.

You will see "Stdout" in the menu on the left, but that page is not shown in the content listing with v0.123.0 or later.


Discussion / hints:

Fix patch:
diff --git a/hugolib/page__tree.go b/hugolib/page__tree.go
index e54d596bc95..cccfb89042a 100644
--- a/hugolib/page__tree.go
+++ b/hugolib/page__tree.go
@@ -124,11 +124,16 @@ func (pt pageTree) Parent() page.Page {
 		return pt.p.s.home
 	}
 
-	_, n := pt.p.s.pageMap.treePages.LongestPrefix(dir, true, nil)
-	if n != nil {
-		return n.(page.Page)
+	for {
+		_, n := pt.p.s.pageMap.treePages.LongestPrefix(dir, true, nil)
+		if n == nil {
+			return pt.p.s.home
+		}
+		if pt.p.m.bundled || n.isContentNodeBranch() {
+			return n.(page.Page)
+		}
+		dir = paths.Dir(dir)
 	}
-	return nil
 }
 
 func (pt pageTree) Ancestors() page.Pages {


Test patch:
diff --git a/hugolib/site_sections_test.go b/hugolib/site_sections_test.go
index 4d4ff965b90..7fa15fb668d 100644
--- a/hugolib/site_sections_test.go
+++ b/hugolib/site_sections_test.go
@@ -398,3 +398,26 @@ Kind: {{ .Kind }}|RelPermalink: {{ .RelPermalink }}|SectionsPath: {{ .SectionsPa
 	b.AssertFileContent("public/a/b/c/mybundle/index.html", "Kind: page|RelPermalink: /a/b/c/mybundle/|SectionsPath: /a/b/c|SectionsEntries: [a b c]|Len: 3")
 	b.AssertFileContent("public/index.html", "Kind: home|RelPermalink: /|SectionsPath: /|SectionsEntries: []|Len: 0")
 }
+
+func TestParentWithPageOverlap(t *testing.T) {
+	files := `
+-- hugo.toml --
+baseURL = "https://example.com/"
+-- content/docs/_index.md --
+-- content/docs/logs/_index.md --
+-- content/docs/logs/sdk.md --
+-- content/docs/logs/sdk_exporters/stdout.md --
+-- layouts/_default/list.html --
+{{ .RelPermalink }}|{{ with .Parent}}{{ .RelPermalink }}{{ end }}|
+-- layouts/_default/single.html --
+{{ .RelPermalink }}|{{ with .Parent}}{{ .RelPermalink }}{{ end }}|
+
+`
+	b := Test(t, files)
+
+	b.AssertFileContent("public/index.html", "/||")
+	b.AssertFileContent("public/docs/index.html", "/docs/|/|")
+	b.AssertFileContent("public/docs/logs/index.html", "/docs/logs/|/docs/|")
+	b.AssertFileContent("public/docs/logs/sdk/index.html", "/docs/logs/sdk/|/docs/logs/|")
+	b.AssertFileContent("public/docs/logs/sdk_exporters/stdout/index.html", "/docs/logs/sdk_exporters/stdout/|/docs/logs/|")
+}