]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
hugolib: Fix newly created shortcodes not found during server rebuild
authorRohan Hasabe <rohanhasabe8@gmail.com>
Sun, 11 Jan 2026 17:00:23 +0000 (12:00 -0500)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 11 Jan 2026 18:33:00 +0000 (19:33 +0100)
When a new shortcode file was added during `hugo server`, the shortcode
was detected but not properly registered in the template cache. This caused
subsequent content edits using the shortcode to fail with "template for
shortcode not found" until the server was restarted.

The issue was that when a shortcode was added, only a glob identity for
dependent content was added to the changes list, but not the shortcode
file's own pathInfo. This meant RefreshFiles filtered it out and never
inserted it into the shortcodesByName cache.

The fix adds the shortcode file's pathInfo to changes (in addition to the
glob for dependent content), and corrects the glob pattern from
`shortcodes/...` to `/_shortcodes/...` to match the actual path format.

Fixes #14207

hugolib/hugo_sites_build.go
hugolib/rebuild_test.go

index aa644d0edafc0b6701bfdad8dabb403fb57661cb..17f7c492e7d9d1d67e017feb5ed5a7e058e9cd20 100644 (file)
@@ -1034,7 +1034,9 @@ func (h *HugoSites) processPartialFileEvents(ctx context.Context, l logg.LevelLo
                                        changes = append(changes, identity.GenghisKhan)
                                }
                                if strings.Contains(base, "shortcodes") {
-                                       changes = append(changes, hglob.NewGlobIdentity(fmt.Sprintf("shortcodes/%s*", pathInfo.BaseNameNoIdentifier())))
+                                       // Add both the shortcode file itself (for template refresh) and a glob for dependent content
+                                       changes = append(changes, pathInfo)
+                                       changes = append(changes, hglob.NewGlobIdentity(fmt.Sprintf("/_shortcodes/%s*", pathInfo.BaseNameNoIdentifier())))
                                } else {
                                        changes = append(changes, pathInfo)
                                }
index 07ce1580d9cb0628e139effff25b528409473bf5..19c2b3098d723be985dc17af0879d056b0974ae9 100644 (file)
@@ -2093,3 +2093,39 @@ All.
        b.EditFileReplaceAll("content/mybundle/resource.txt", "This is a resource file.", "This is an edited resource file.").Build()
        b.AssertFileContent("public/mybundle/resource.txt", "This is an edited resource file.")
 }
+
+// Issue #14207
+func TestRebuildAddShortcodeThenEditContentUsingIt(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+baseURL = "https://example.com"
+disableLiveReload = true
+-- layouts/single.html --
+Single: {{ .Title }}|{{ .Content }}|
+-- content/p1.md --
+---
+title: "P1"
+---
+Content before shortcode.
+`
+       b := TestRunning(t, files)
+       b.AssertFileContent("public/p1/index.html", "Single: P1|", "Content before shortcode.")
+
+       // Add a new shortcode file
+       b.AddFiles(
+               "layouts/_shortcodes/year.html", `{{- now.Format "2006" -}}`,
+       ).Build()
+
+       // Verify shortcode was registered
+       ts := b.H.Sites[0].Deps.TemplateStore
+       if ti := ts.LookupShortcodeByName("year"); ti == nil {
+               t.Fatal("shortcode 'year' not found in cache after adding")
+       }
+
+       // Now edit content to use the shortcode - this should find the shortcode
+       currentYear := time.Now().Format("2006")
+       b.EditFileReplaceAll("content/p1.md", "Content before shortcode.", "This is {{< year >}} wow.").Build()
+       b.AssertFileContent("public/p1/index.html", "Single: P1|", fmt.Sprintf("This is %s wow.", currentYear))
+}