Skip to content

Commit 38e05bd

Browse files
committed
Fix panic with debug.Dump with Page when running the server
This replaces the current implementation with `json.MarshalIndent` which doesn't produce the same output, but at least it doesn't crash. There's a bug in the upstream `litter` library. This can probably be fixed, but that needs to wait. I have tested `go-spew` which does not crash, but it is very data racy in this context. FIxes #12309
1 parent ebfca61 commit 38e05bd

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

tpl/debug/debug.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
package debug
1616

1717
import (
18+
"encoding/json"
1819
"sort"
1920
"sync"
2021
"time"
2122

2223
"github.com/bep/logg"
23-
"github.com/sanity-io/litter"
2424
"github.com/spf13/cast"
2525
"github.com/yuin/goldmark/util"
2626

@@ -108,7 +108,11 @@ type Namespace struct {
108108
// Also note that the output from Dump may change from Hugo version to the next,
109109
// so don't depend on a specific output.
110110
func (ns *Namespace) Dump(val any) string {
111-
return litter.Sdump(val)
111+
b, err := json.MarshalIndent(val, "", " ")
112+
if err != nil {
113+
return ""
114+
}
115+
return string(b)
112116
}
113117

114118
// VisualizeSpaces returns a string with spaces replaced by a visible string.

tpl/debug/debug_integration_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,33 @@ disableKinds = ["taxonomy", "term"]
4343

4444
b.AssertLogContains("timer: name foo count 5 duration")
4545
}
46+
47+
func TestDebugDumpPage(t *testing.T) {
48+
files := `
49+
-- hugo.toml --
50+
baseURL = "https://example.org/"
51+
disableLiveReload = true
52+
[taxonomies]
53+
tag = "tags"
54+
-- content/_index.md --
55+
---
56+
title: "The Index"
57+
date: 2012-03-15
58+
---
59+
-- content/p1.md --
60+
---
61+
title: "The First"
62+
tags: ["a", "b"]
63+
---
64+
-- layouts/_default/list.html --
65+
Dump: {{ debug.Dump . | safeHTML }}
66+
Dump Site: {{ debug.Dump site }}
67+
Dum site.Taxonomies: {{ debug.Dump site.Taxonomies | safeHTML }}
68+
-- layouts/_default/single.html --
69+
Dump: {{ debug.Dump . | safeHTML }}
70+
71+
72+
`
73+
b := hugolib.TestRunning(t, files)
74+
b.AssertFileContent("public/index.html", "Dump: {\n \"Date\": \"2012-03-15T00:00:00Z\"")
75+
}

tpl/debug/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func init() {
3636
[][2]string{
3737
{`{{ $m := newScratch }}
3838
{{ $m.Set "Hugo" "Rocks!" }}
39-
{{ $m.Values | debug.Dump | safeHTML }}`, "map[string]interface {}{\n \"Hugo\": \"Rocks!\",\n}"},
39+
{{ $m.Values | debug.Dump | safeHTML }}`, "{\n \"Hugo\": \"Rocks!\"\n}"},
4040
},
4141
)
4242

0 commit comments

Comments
 (0)