Can Hugo fail on duplicate pages (files in the public dir)?

I have a section where page urls are mapped to the site root (using [permalinks]). Is it possible to make hugo fail if two or more files have identical paths (to prevent me from accidentally having duplicate titles or slugs)?

For now it looks like hugo silently ignores duplicate pages.

So far I’ve only found this issue https://github.com/gohugoio/hugo/issues/3113

No, but there is an open issue about creating some kind of warning in this case.

As it turned out, the template language and context variables are rich enough to implement this myself!

A snippet to put into baseof.html:

{{ range .Site.AllPages.GroupBy "Permalink" }}
  {{ if gt (len .Pages) 1 }}
    {{ errorf "Multiple pages with the same url %q:" .Key }}
    {{ range .Pages }}
      {{ errorf "\t %q" .File.Path }}
    {{ end }}
    {{ .DuplicatePageUrlsFound }}
  {{ end }}
{{ end }}

This fails nicely with the following message:

ERROR 2018/09/12 14:55:55 Multiple pages with the same url "/post-1/":
ERROR 2018/09/12 14:55:55 	 "articles/post2.md"
ERROR 2018/09/12 14:55:55 	 "articles/post1/index.md"
ERROR 2018/09/12 14:55:55 Error while rendering "page" in "articles/": template: _default/single.html:30:7: executing "_default/single.html" at <.DuplicatePageUrls>: can't evaluate field DuplicatePageUrls in type hugolib.PageGroup
2 Likes