Backtestshugorun-20261009-153657#14352
Support slug in section, taxonomy, and term pages
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Patch A does not address slug support for sections, taxonomies, or terms. It renames an interface parameter and swaps HasAnyVector for HasVector in MatchSiteVector. That may fix a separate type or semantics bug, but it has nothing to do with deriving URLs from branch-page slugs. It also adds no tests. Patch B appears to build on existing slug-in-branch-page support. The test file already contains assertions for es/ayuda paths. B adds what that support needs on rebuild: it records the ancestor slugs of branch pages in the walk context and detects slug changes between builds. When a slug changes, it forces descendant pages to be recreated from source and marks them as changed. It also clones the term sitesMatrixBase map, so a mutated map does not corrupt pages recreated on rebuild. B adds a thorough rebuild test (TestSlugInBranchPagesRebuild). The test covers changing a root section slug, adding a slug to a nested section, removing a slug, and changing a taxonomy slug. It checks descendants, bundles, resources and term back-references. B also corrects an RSS link expectation to an absolute URL. B is directly relevant to the issue's behaviour (slugs propagating to descendants) and well tested. A is unrelated to the feature.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
1h 32m
Wall-clock time in the container
Full test suite
No new failures
2,290 passed · 95 skipped · 2,385 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
157vs 4 merged
Changed lines, ours vs merged
The issue
View on GitHub<!-- Describe this new feature. Think about if it really belongs in the Hugo core module; you may want to discuss it on https://discourse.gohugo.io/ first. -->
Currently with multilanguage sites you can localize the URL for pages by setting slug in the frontmatter.
However there is no slug support for sections. This means the only way to localize a section URL is to use url together with [[cascade]]. This is annoying since you have to "redo from scratch" the entire URL format, with : tokens, duplicating names for parent sections, etc.
Proposal: add support for slug in section pages. It would behave the obvious way, basically being equivalent to changing the directory name for the section. It would affect the URL for the section and all descendant pages.
Example:
help/
_index.en.md # contains no slug
_index.es.md # contains slug="ayuda"
how-to-frob/
index.en.md # contains no slug
index.es.md # contains slug="como-frobear"
would result in the following paths:
help/
help/how-to-frob/
es/ayuda/
es/ayuda/como-frobear/The two patches
hugolib/content_map_page_assembler.goindex 8fb0fbd34..3601b8c2e 100644@@ -16,6 +16,7 @@ package hugolib import ( "context" "fmt"+ "maps" "path" "strings" "time"@@ -223,6 +224,43 @@ func (a *allPagesAssembler) doCreatePages(prefix string, depth int) error { return cascades } + // getAncestorSlugs returns the slugs set on the branch pages above s+ // for the site vector vec. These are used to build the target paths+ // of s, see targetPathDir.+ getAncestorSlugs := func(wctx *doctree.WalkContext[contentNode], vec sitesmatrix.Vector, s string) string {+ _, v := wctx.DataRaw(vec).LongestPrefix(s)+ if v == nil {+ return ""+ }+ return v.(string)+ }++ // ancestorSlugsChanged reports whether any slug set on the branch pages+ // above s has changed since the previous build.+ // The pages affected are registered as changed so any page+ // depending on them (e.g. via their RelPermalink) gets rebuilt.+ ancestorSlugsChanged := func(s string, n contentNode) bool {+ if s == "" {+ return false+ }+ var changed bool+ n.forEeachContentNode(+ func(vec sitesmatrix.Vector, nn contentNode) bool {+ ps, ok := nn.(*pageState)+ if !ok {+ // Not created yet.+ return true+ }+ if getAncestorSlugs(h.previousPageTreesWalkContext, vec, s) != getAncestorSlugs(rw.WalkContext, vec, s) {+ changed = true+ a.assembleChanges.Add(ps)+ }+ return true+ },+ )+ return changed+ }+ doTransformPages := func(s string, n contentNode, cascades *page.PageMatcherParamsConfigs) (n2 contentNode, ns doctree.NodeTransformState, err error) { defer func() { cascadesLen := cascades.Len()@@ -245,6 +283,24 @@ func (a *allPagesAssembler) doCreatePages(prefix string, depth int) error { // New cascade values added, pass them downwards. rw.WalkContext.Data().Insert(paths.AddTrailingSlash(s), cascades) }++ if n2 != nil && s != "" && a.h.Conf.Watching() {+ // Record any slugs set on branch pages so we can detect+ // changes to them on rebuilds, see ancestorSlugsChanged.+ n2.forEeachContentNode(+ func(vec sitesmatrix.Vector, nn contentNode) bool {+ ps, ok := nn.(*pageState)+ if !ok || !ps.IsBranch() {+ return true+ }+ if slug := ps.Slug(); slug != "" {+ slugs := getAncestorSlugs(rw.WalkContext, vec, s) + "/" + s + ":" + slug+ rw.WalkContext.DataRaw(vec).Insert(paths.AddTrailingSlash(s), slugs)+ }+ return true+ },+ )+ } }() var handlePageMetaSource func(v contentNode, n contentNodesMap, replaceVector bool) (n2 contentNode, err error)@@ -447,7 +503,10 @@ func (a *allPagesAssembler) doCreatePages(prefix string, depth int) error { if a.h.isRebuild() { cascadesPrevious := getCascades(h.previousPageTreesWalkContext, s) h1, h2 := cascadesPrevious.SourceHash(), cascades.SourceHash()- if h1 != h2 {+ // Always check the slugs, as this also registers the+ // affected pages as changed.+ slugsChanged := ancestorSlugsChanged(s, n)+ if h1 != h2 || slugsChanged { // Force rebuild from the source. n = unpackPageMetaSources(n) }@@ -858,8 +917,11 @@ func (a *allPagesAssembler) doCreatePages(prefix string, depth int) error { if len(v) > 0 { p := &pageMetaSource{- pathInfo: pi,- sitesMatrixBase: v,+ pathInfo: pi,+ // Clone the vectors, as v gets modified when merging+ // with existing term pages above, and the page+ // may be recreated from this source on rebuilds.+ sitesMatrixBase: maps.Clone(v), pageConfigSource: &pagemeta.PageConfigEarly{ Kind: kinds.KindTerm, },hugolib/page_permalink_test.goindex 48dc7d430..4bfdca2cb 100644@@ -277,7 +277,7 @@ slug: rojo b.AssertFileContent("public/tags/red/index.html", "term|/tags/red|/tags/red/|/help/how-to-frob/|") b.AssertFileContent("public/es/ayuda/index.html", "section|/help|/es/ayuda/|/es/ayuda/avanzado/|/es/ayuda/como-frobear/|")- b.AssertFileContent("public/es/ayuda/index.xml", "<link>/es/ayuda/</link>")+ b.AssertFileContent("public/es/ayuda/index.xml", "<link>https://example.org/es/ayuda/</link>") b.AssertFileContent("public/es/ayuda/como-frobear/index.html", "page|/help/how-to-frob|/es/ayuda/como-frobear/|") b.AssertFileContent("public/es/ayuda/avanzado/index.html", "section|/help/advanced|/es/ayuda/avanzado/|/es/ayuda/avanzado/p1/|") b.AssertFileContent("public/es/ayuda/avanzado/p1/index.html", "page|/help/advanced/p1|/es/ayuda/avanzado/p1/|")@@ -312,6 +312,93 @@ title: P1 b.AssertFileContent("public/Ayuda/p1.html", "page|/Ayuda/p1.html|") } +func TestSlugInBranchPagesRebuild(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ["rss", "sitemap"]+defaultContentLanguage = "en"+[languages.en]+weight = 1+[languages.es]+weight = 2+[taxonomies]+tag = "tags"+-- content/help/_index.en.md --+---+title: Help+---+-- content/help/_index.es.md --+---+title: Ayuda+slug: ayuda+---+-- content/help/p1.es.md --+---+title: P1+---+-- content/help/advanced/_index.es.md --+---+title: Avanzado+---+-- content/help/advanced/bundle/index.es.md --+---+title: Bundle+tags: [red]+---+-- content/help/advanced/bundle/data.es.txt --+data+-- content/help/deep/dir/p2.es.md --+---+title: P2+---+-- content/tags/_index.es.md --+---+title: Etiquetas+slug: etiquetas+---+-- layouts/all.html --+{{ .Kind }}|{{ .RelPermalink }}|{{ range .Pages }}{{ .RelPermalink }}|{{ end }}{{ range .Resources }}{{ .RelPermalink }}|{{ end }}+`++ b := TestRunning(t, files)++ b.AssertFileContent("public/es/ayuda/advanced/bundle/index.html", "page|/es/ayuda/advanced/bundle/|/es/ayuda/advanced/bundle/data.es.txt|")+ b.AssertFileContent("public/es/etiquetas/red/index.html", "term|/es/etiquetas/red/|/es/ayuda/advanced/bundle/|")++ // Change the slug of the root section.+ b.EditFileReplaceAll("content/help/_index.es.md", "slug: ayuda", "slug: socorro").Build()++ b.AssertFileContent("public/es/socorro/index.html", "section|/es/socorro/|/es/socorro/advanced/|/es/socorro/p1/|")+ b.AssertFileContent("public/es/socorro/p1/index.html", "page|/es/socorro/p1/|")+ b.AssertFileContent("public/es/socorro/advanced/index.html", "section|/es/socorro/advanced/|/es/socorro/advanced/bundle/|")+ b.AssertFileContent("public/es/socorro/advanced/bundle/index.html", "page|/es/socorro/advanced/bundle/|/es/socorro/advanced/bundle/data.es.txt|")+ b.AssertFileContent("public/es/socorro/advanced/bundle/data.es.txt", "data")+ b.AssertFileContent("public/es/socorro/deep/dir/p2/index.html", "page|/es/socorro/deep/dir/p2/|")+ b.AssertFileContent("public/es/etiquetas/red/index.html", "term|/es/etiquetas/red/|/es/socorro/advanced/bundle/|")+ b.AssertFileContent("public/help/index.html", "section|/help/|")++ // Add a slug to a nested section.+ b.EditFileReplaceAll("content/help/advanced/_index.es.md", "title: Avanzado", "title: Avanzado\nslug: avanzado").Build()++ b.AssertFileContent("public/es/socorro/index.html", "section|/es/socorro/|/es/socorro/avanzado/|/es/socorro/p1/|")+ b.AssertFileContent("public/es/socorro/avanzado/bundle/index.html", "page|/es/socorro/avanzado/bundle/|/es/socorro/avanzado/bundle/data.es.txt|")++ // Remove the slug from the root section.+ b.EditFileReplaceAll("content/help/_index.es.md", "slug: socorro", "").Build()++ b.AssertFileContent("public/es/help/index.html", "section|/es/help/|/es/help/avanzado/|/es/help/p1/|")+ b.AssertFileContent("public/es/help/avanzado/bundle/index.html", "page|/es/help/avanzado/bundle/|/es/help/avanzado/bundle/data.es.txt|")+ b.AssertFileContent("public/es/help/deep/dir/p2/index.html", "page|/es/help/deep/dir/p2/|")++ // Change the slug of the taxonomy.+ b.EditFileReplaceAll("content/tags/_index.es.md", "slug: etiquetas", "slug: marcas").Build()++ b.AssertFileContent("public/es/marcas/index.html", "taxonomy|/es/marcas/|/es/marcas/red/|")+ b.AssertFileContent("public/es/marcas/red/index.html", "term|/es/marcas/red/|/es/help/avanzado/bundle/|")+}+ func BenchmarkTargetPathDir(b *testing.B) { files := ` -- content/help/_index.md --
hugolib/sitesmatrix/dimensions.goindex aefa6f5e0..ea07284ef 100644@@ -202,7 +202,7 @@ type VectorProvider interface { VectorIterator // HasVector returns true if the given vector is contained in the provider. // Used for membership testing of files, resources and pages.- HasVector(HasAnyVectorv Vector) bool+ HasVector(v Vector) bool // HasAnyVector returns true if any of the vectors in the provider matches any of the vectors in v. HasAnyVector(v VectorProvider) boolresources/page/pagemeta/page_frontmatter.goindex d2f3d2793..6077fcc3c 100644@@ -191,7 +191,7 @@ type SitesMatrixAndComplements struct { // MatchSiteVector checks whether the site vector matches the sites matrix. func (p *SitesMatrixAndComplements) MatchSiteVector(siteVector sitesmatrix.Vector) bool {- return p.SitesMatrix.HasAnyVector(siteVector)+ return p.SitesMatrix.HasVector(siteVector) } // MatchLanguageCoarse checks whether the language dimension matches either