From c3d435acfae17c8619451337e4f09348b05a32fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 19 Feb 2025 08:44:37 +0100 Subject: [PATCH] Fix --printPathWarnings when site calls templates.Defer This issue was introduced recently in eb7a5aabaaa025b6633b4aff3267b21ebdffb8e9. Fixes #13420 --- hugolib/hugo_sites.go | 3 ++- hugolib/hugo_sites_build.go | 23 ++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index 83e8caa7f..b17c761aa 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -85,7 +85,8 @@ type HugoSites struct { pageTrees *pageTrees - postRenderInit sync.Once + printUnusedTemplatesInit sync.Once + printPathWarningsInit sync.Once // File change events with filename stored in this map will be skipped. skipRebuildForFilenamesMu sync.Mutex diff --git a/hugolib/hugo_sites_build.go b/hugolib/hugo_sites_build.go index 44598526c..729c17c75 100644 --- a/hugolib/hugo_sites_build.go +++ b/hugolib/hugo_sites_build.go @@ -176,12 +176,18 @@ func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error { return err } + // We need to do this before render deferred. + if err := h.printPathWarningsOnce(); err != nil { + h.SendError(fmt.Errorf("printPathWarnings: %w", err)) + } + if err := h.renderDeferred(infol); err != nil { h.SendError(fmt.Errorf("renderDeferred: %w", err)) } - if err := h.postRenderOnce(); err != nil { - h.SendError(fmt.Errorf("postRenderOnce: %w", err)) + // This needs to be done after the deferred rendering to get complete template usage coverage. + if err := h.printUnusedTemplatesOnce(); err != nil { + h.SendError(fmt.Errorf("printPathWarnings: %w", err)) } if err := h.postProcess(infol); err != nil { @@ -545,9 +551,9 @@ func (s *Site) executeDeferredTemplates(de *deps.DeferredExecutions) error { return g.Wait() } -// / postRenderOnce runs some post processing that only needs to be done once, e.g. printing of unused templates. -func (h *HugoSites) postRenderOnce() error { - h.postRenderInit.Do(func() { +// printPathWarningsOnce prints path warnings if enabled. +func (h *HugoSites) printPathWarningsOnce() error { + h.printPathWarningsInit.Do(func() { conf := h.Configs.Base if conf.PrintPathWarnings { // We need to do this before any post processing, as that may write to the same files twice @@ -562,7 +568,14 @@ func (h *HugoSites) postRenderOnce() error { return false }) } + }) + return nil +} +// / printUnusedTemplatesOnce prints unused templates if enabled. +func (h *HugoSites) printUnusedTemplatesOnce() error { + h.printUnusedTemplatesInit.Do(func() { + conf := h.Configs.Base if conf.PrintUnusedTemplates { unusedTemplates := h.Tmpl().(tpl.UnusedTemplatesProvider).UnusedTemplates() for _, unusedTemplate := range unusedTemplates {