Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nil pointer outputformat shortcode bug #10391

Closed
davidejones opened this issue Oct 25, 2022 · 1 comment · Fixed by #10392
Closed

nil pointer outputformat shortcode bug #10391

davidejones opened this issue Oct 25, 2022 · 1 comment · Fixed by #10392

Comments

@davidejones
Copy link
Contributor

What version of Hugo are you using (hugo version)?

This is confirmed an issue with hugo 101.0 which we are currently using and also with the latest hugo 104.3.
(both extended)

Does this issue reproduce with the latest release?

Yes

What is the issue?

Under a specific set of circumstances a runtime error: invalid memory address or nil pointer dereference occurs

Full Error

View Stack Trace
ERROR 2022/10/25 09:49:02 [BUG] Got panic:
runtime error: invalid memory address or nil pointer dereference
goroutine 269 [running]:
runtime/debug.Stack()
        /usr/local/go/src/runtime/debug/stack.go:24 +0x65
github.com/gohugoio/hugo/hugolib.newPageContentOutput.func1.1()
        /root/project/hugo/hugolib/page__per_output.go:101 +0xaa
panic({0x25f2040, 0x23f09f0})
        /usr/local/go/src/runtime/panic.go:884 +0x212
github.com/gohugoio/hugo/hugolib.(*pageContentOutput).renderContent(0x0, {0xc000a83140, 0x11, 0x18}, 0x28?)
        /root/project/hugo/hugolib/page__per_output.go:636 +0x54
github.com/gohugoio/hugo/hugolib.renderShortcode(0x1, 0xc000194fc0, {{0xc00034253a, 0x2}, {{0xc0003432a0, 0x6}, {{0x2868128, 0xb}, {0x2841e13, 0x4}, ...}, ...}}, ...)
        /root/project/hugo/hugolib/shortcode.go:385 +0xd4e
github.com/gohugoio/hugo/hugolib.renderShortcode(0x0, 0xc000194fc0, {{0xc00034253a, 0x2}, {{0xc0003432a0, 0x6}, {{0x2868128, 0xb}, {0x2841e13, 0x4}, ...}, ...}}, ...)
        /root/project/hugo/hugolib/shortcode.go:368 +0xba5
github.com/gohugoio/hugo/hugolib.(*shortcodeHandler).renderShortcodesForPage(0xc00070bc70, 0xc000b57440, {{0xc0003432a0, 0x6}, {{0x2868128, 0xb}, {0x2841e13, 0x4}, {0x283d1ef, 0x1}, ...}, ...})
        /root/project/hugo/hugolib/shortcode.go:487 +0x18a
github.com/gohugoio/hugo/hugolib.newPageContentOutput.func1()
        /root/project/hugo/hugolib/page__per_output.go:112 +0x178
github.com/gohugoio/hugo/hugolib.newPageContentOutput.func2({0x837aa9?, 0x3?})
        /root/project/hugo/hugolib/page__per_output.go:201 +0x1b
github.com/gohugoio/hugo/lazy.(*Init).withTimeout.func1()
        /root/project/hugo/lazy/init.go:188 +0x3c
created by github.com/gohugoio/hugo/lazy.(*Init).withTimeout
        /root/project/hugo/lazy/init.go:187 +0x11e
Error: Error building site: "C:\Users\davidejones\Desktop\HUGOTEST\content\fr\tracing\visualization\trace.md:1:1": runtime error: invalid memory address or nil pointer dereference
Total in 402 ms

What specific conditions combine to lead to this error?

  • Looping over pages in a custom output format that accesses the page .Content or .Plain
  • Page markdown content containing a nested shortcode
  • Multilanguage site

I'm certain these conditions apply because doing any one of these individually causes the problem to be resolved

  • Removing the custom output format template or not calling .Content or .Plain in it causes it to work
  • Removing the nested shortcode causes it to work
  • disableLanguages = ['fr'] causes it to work

Recreated Example

TestNestedShortcodeCustomOutputFormat()

Add the following test to hugolib/shortcode_test.go to recreate

func TestNestedShortcodeCustomOutputFormat(t *testing.T) {
  t.Parallel()

  files := `
-- config.toml --

[outputFormats.Foobar]
baseName = "foobar"
isPlainText = true
mediaType = "application/json"
notAlternative = true

[languages.en]
languageName = "English"

[languages.en.outputs]
home = [ "HTML", "RSS", "Foobar" ]

[languages.fr]
languageName = "Français"

[[module.mounts]]
source = "content/en"
target = "content"
lang = "en"

[[module.mounts]]
source = "content/fr"
target = "content"
lang = "fr"

-- layouts/_default/list.foobar.json --
{{- $.Scratch.Add "data" slice -}}
{{- range (where .Site.AllPages "Kind" "!=" "home") -}}
  {{- $.Scratch.Add "data" (dict "content" (.Plain | truncate 10000) "type" .Type "full_url" .Permalink) -}}
{{- end -}}
{{- $.Scratch.Get "data" | jsonify -}}
-- content/en/p1.md --
---
title: "p1"
---

### More information

{{< tabs >}}
{{% tab "Test" %}}

It's a test

{{% /tab %}}
{{< /tabs >}}

-- content/fr/p2.md --
---
title: Test
---

### Plus d'informations

{{< tabs >}}
{{% tab "Test" %}}

C'est un test

{{% /tab %}}
{{< /tabs >}}

-- layouts/shortcodes/tabs.html --
<div>
<div class="tab-content">{{ .Inner }}</div>
</div>

-- layouts/shortcodes/tab.html --
<div>{{ .Inner }}</div>

-- layouts/_default/single.html --
{{ .Content }}
`

  b := NewIntegrationTestBuilder(
  	IntegrationTestConfig{
  		T:           t,
  		TxtarString: files,
  		Running:     true,
  		Verbose:     true,
  	},
  ).Build()

  b.AssertFileContent("public/fr/p2/index.html", `plus-dinformations`)

}

Error details

Debugging it seems to be that cp on p.pageOutput is nil at times

hugolib/shortcode.go

...

// Pre Hugo 0.55 this was the behaviour even for the outer-most
// shortcode.
if sc.doMarkup && (level > 0 || sc.configVersion() == 1) {
    var err error
    b, err := p.pageOutput.cp.renderContent([]byte(inner), false)
    if err != nil {
        return "", false, err
    }
    
...

For further context our site was working great and we discovered this bug when adding the output format code (which works successfully on other sites of ours)

@bep bep removed the NeedsTriage label Oct 26, 2022
@bep bep modified the milestones: v0.105.0, v0.106.0 Oct 26, 2022
bep pushed a commit to davidejones/hugo that referenced this issue Oct 26, 2022
bep added a commit to davidejones/hugo that referenced this issue Oct 26, 2022
We do lazy initialization and (potentially) reuse of an output format's rendered content. We do this evaluation when we
start a new rendering a new output format. There are, however, situation where these borders gets crossed (e.g.
accessing content from another output format). We have a check for this in place for most cases, but not the content
rendering of inner markdown blocks inside shortcodes. This patch applies that same logic to the newly introduced
RenderContent method (which is not available from the templates).

Fixes gohugoio#10391
bep added a commit to davidejones/hugo that referenced this issue Oct 26, 2022
We do lazy initialization and (potentially) reuse of an output format's rendered content. We do this evaluation when we
start a new rendering a new output format. There are, however, situation where these borders gets crossed (e.g.
accessing content from another output format). We have a check for this in place for most cases, but not the content
rendering of inner markdown blocks inside shortcodes. This patch applies that same logic to the newly introduced
RenderContent method (which is not available from the templates).

Fixes gohugoio#10391
bep added a commit that referenced this issue Oct 26, 2022
We do lazy initialization and (potentially) reuse of an output format's rendered content. We do this evaluation when we
start a new rendering a new output format. There are, however, situation where these borders gets crossed (e.g.
accessing content from another output format). We have a check for this in place for most cases, but not the content
rendering of inner markdown blocks inside shortcodes. This patch applies that same logic to the newly introduced
RenderContent method (which is not available from the templates).

Fixes #10391
@bep bep modified the milestones: v0.106.0, v0.105.0 Oct 26, 2022
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants