Repository: gohugoio/hugo (language: go)

Issue:
Panic when using debug.Dump .Page and others in server
Reference: <https://discourse.gohugo.io/t/49029>

This began with v0.123.0.

These trigger a panic:

- `debug.Dump .Page`
- `debug.Dump site.Taxonomies`

This does not trigger a panic:

```
debug.Dump "foo"
```


Discussion / hints:

Fix patch:
diff --git a/tpl/debug/debug.go b/tpl/debug/debug.go
index f603dd94d90..027454b53b3 100644
--- a/tpl/debug/debug.go
+++ b/tpl/debug/debug.go
@@ -15,12 +15,12 @@
 package debug
 
 import (
+	"encoding/json"
 	"sort"
 	"sync"
 	"time"
 
 	"github.com/bep/logg"
-	"github.com/sanity-io/litter"
 	"github.com/spf13/cast"
 	"github.com/yuin/goldmark/util"
 
@@ -108,7 +108,11 @@ type Namespace struct {
 // Also note that the output from Dump may change from Hugo version to the next,
 // so don't depend on a specific output.
 func (ns *Namespace) Dump(val any) string {
-	return litter.Sdump(val)
+	b, err := json.MarshalIndent(val, "", "  ")
+	if err != nil {
+		return ""
+	}
+	return string(b)
 }
 
 // VisualizeSpaces returns a string with spaces replaced by a visible string.
diff --git a/tpl/debug/init.go b/tpl/debug/init.go
index 796a34bfcfc..1232a4166da 100644
--- a/tpl/debug/init.go
+++ b/tpl/debug/init.go
@@ -36,7 +36,7 @@ func init() {
 			[][2]string{
 				{`{{ $m := newScratch }}
 {{ $m.Set "Hugo" "Rocks!" }}
-{{ $m.Values | debug.Dump | safeHTML }}`, "map[string]interface {}{\n  \"Hugo\": \"Rocks!\",\n}"},
+{{ $m.Values | debug.Dump | safeHTML }}`, "{\n  \"Hugo\": \"Rocks!\"\n}"},
 			},
 		)
 


Test patch:
diff --git a/tpl/debug/debug_integration_test.go b/tpl/debug/debug_integration_test.go
index 9a36e2d12ac..52f0a427c84 100644
--- a/tpl/debug/debug_integration_test.go
+++ b/tpl/debug/debug_integration_test.go
@@ -43,3 +43,33 @@ disableKinds = ["taxonomy", "term"]
 
 	b.AssertLogContains("timer:  name foo count 5 duration")
 }
+
+func TestDebugDumpPage(t *testing.T) {
+	files := `
+-- hugo.toml --
+baseURL = "https://example.org/"
+disableLiveReload = true
+[taxonomies]
+tag = "tags"
+-- content/_index.md --
+---
+title: "The Index"
+date: 2012-03-15
+---
+-- content/p1.md --
+---
+title: "The First"
+tags: ["a", "b"]
+---
+-- layouts/_default/list.html --
+Dump: {{ debug.Dump . | safeHTML }}
+Dump Site: {{ debug.Dump site }}
+Dum site.Taxonomies: {{ debug.Dump site.Taxonomies | safeHTML }}
+-- layouts/_default/single.html --
+Dump: {{ debug.Dump . | safeHTML }}
+
+
+`
+	b := hugolib.TestRunning(t, files)
+	b.AssertFileContent("public/index.html", "Dump: {\n  \"Date\": \"2012-03-15T00:00:00Z\"")
+}