Skip to content

Commit

Permalink
Also warn about duplicate content paths with --printPathWarnings
Browse files Browse the repository at this point in the history
Closes #12511
  • Loading branch information
bep committed May 17, 2024
1 parent 3d40aba commit e3bf264
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
22 changes: 20 additions & 2 deletions hugolib/content_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ func (cfg contentMapConfig) getTaxonomyConfig(s string) (v viewName) {
return
}

func (m *pageMap) insertPage(s string, p *pageState) (contentNodeI, contentNodeI, bool) {
u, n, replaced := m.treePages.InsertIntoValuesDimensionWithLock(s, p)

if replaced && !m.s.h.isRebuild() && m.s.conf.PrintPathWarnings {
var messageDetail string
if p1, ok := n.(*pageState); ok && p1.File() != nil {
messageDetail = fmt.Sprintf(" file: %q", p1.File().Filename())
}
if p2, ok := u.(*pageState); ok && p2.File() != nil {
messageDetail += fmt.Sprintf(" file: %q", p2.File().Filename())
}

m.s.Log.Warnf("Duplicate content path: %q%s", s, messageDetail)
}

return u, n, replaced
}

func (m *pageMap) AddFi(fi hugofs.FileMetaInfo, buildConfig *BuildCfg) (pageCount uint64, resourceCount uint64, addErr error) {
if fi.IsDir() {
return
Expand Down Expand Up @@ -261,7 +279,7 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo, buildConfig *BuildCfg) (pageCoun
return
}

m.treePages.InsertIntoValuesDimensionWithLock(pi.Base(), p)
m.insertPage(pi.Base(), p)

}
return
Expand Down Expand Up @@ -336,7 +354,7 @@ func (m *pageMap) addPagesFromGoTmplFi(fi hugofs.FileMetaInfo, buildConfig *Buil
return nil
}

u, n, replaced := s.pageMap.treePages.InsertIntoValuesDimensionWithLock(pi.Base(), ps)
u, n, replaced := s.pageMap.insertPage(pi.Base(), ps)

if h.isRebuild() {
if replaced {
Expand Down
32 changes: 32 additions & 0 deletions hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,35 @@ disableKinds = ['home','section','rss','sitemap','taxonomy','term']
"data1.yaml|param1v",
)
}

func TestPagesFromGoTmplPathWarningsIssue12511(t *testing.T) {
t.Parallel()

files := `
-- hugo.toml --
baseURL = "https://example.com"
disableKinds = ['home','section','rss','sitemap','taxonomy','term']
printPathWarnings = true
-- content/_content.gotmpl --
{{ .AddPage (dict "path" "p1" "title" "p1" ) }}
{{ .AddPage (dict "path" "p2" "title" "p2" ) }}
-- content/p1.md --
---
title: "p1"
---
-- layouts/_default/single.html --
{{ .Title }}|
`

b := hugolib.Test(t, files, hugolib.TestOptWarn())

b.AssertFileContent("public/p1/index.html", "p1|")

b.AssertLogContains("Duplicate content path")

files = strings.ReplaceAll(files, `"path" "p1"`, `"path" "p1new"`)

b = hugolib.Test(t, files, hugolib.TestOptWarn())

b.AssertLogNotContains("WARN")
}

0 comments on commit e3bf264

Please sign in to comment.