]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix panic with debug.Dump with Page when running the server
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 26 Mar 2024 09:43:08 +0000 (10:43 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 26 Mar 2024 19:41:30 +0000 (20:41 +0100)
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

tpl/debug/debug.go
tpl/debug/debug_integration_test.go
tpl/debug/init.go

index f603dd94d9018e7e4c786def1e8a71a6bd8acc43..027454b53b360b0460425e0a1b62657c1854b2f5 100644 (file)
 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.
index 9a36e2d12ac68992e58192805f5842a2f057233c..52f0a427c84807988d57c7027769a80059679d1f 100644 (file)
@@ -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\"")
+}
index 796a34bfcfcd28873c32dbfc9a264794c07a5084..1232a4166da29fcbe2eb96ccbd490dde9fb9f5e9 100644 (file)
@@ -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}"},
                        },
                )