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

Draft _index.md gives "invalid memory address or nil pointer dereference" #11441

Closed
JVimes opened this issue Sep 10, 2023 · 6 comments
Closed

Comments

@JVimes
Copy link

JVimes commented Sep 10, 2023

What version of Hugo are you using?

hugo v0.118.2-da7983ac4b94d97d776d7c2405040de97e95c03d+extended windows/amd64 BuildDate=2023-08-31T11:23:51Z VendorInfo=gohugoio

Does this issue reproduce with the latest release?

Yes

Summary

When I use a draft _index.md at the root level I get:

invalid memory address or nil pointer dereference

The error goes away if I change draft to false for _index.md.

Repro

hugo new site foo
cd foo
git init
git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke.git themes/ananke
echo "theme = 'ananke'" >> hugo.toml
hugo new _index.md
hugo server

Final output:

Error: error building site:
render: render of "404" failed: "C:\foo\themes\ananke\layouts\404.html:1:24":
execute of template failed: template: 404.html:1:24:
executing "header" at <partial "page-header.html" .>:
error calling partial: "C:\foo\themes\ananke\layouts\partials\page-header.html:22:9":
execute of template failed: template: partials/page-header.html:22:9:
executing "partials/page-header.html" at <partial "site-navigation.html" .>:
error calling partial: "C:\foo\themes\ananke\layouts\partials\site-navigation.html:3:21":
execute of template failed: template: partials/site-navigation.html:3:21:
executing "partials/site-navigation.html" at <.Site.Home.RelPermalink>:
error calling RelPermalink:
runtime error: invalid memory address or nil pointer dereference

Other themes

Same error from different file/lines.

PaperMod:

Error: error building site:
render: render of "404" failed: "C:\foo\themes\PaperMod\layouts\_default\baseof.html:16:8":
execute of template failed: template: 404.html:16:8:
executing "404.html" at <partialCached "header.html" . .Page>:
error calling partialCached: "C:\foo\themes\PaperMod\layouts\partials\header.html:101:29":
execute of template failed: template: partials/header.html:101:29:
executing "partials/header.html" at <site.Home.AllTranslations>:
error calling AllTranslations:
runtime error: invalid memory address or nil pointer dereference

Blowfish:

Error: error building site: render: render of "404" failed: "C:\foo\themes\Blowfish\layouts\_default\baseof.html:20:5":
execute of template failed: template: 404.html:20:5:
executing "404.html" at <partial $header .>:
error calling partial: "C:\foo\themes\Blowfish\layouts\partials\header\basic.html:21:30":
execute of template failed: template: partials/header/basic.html:21:30:
executing "partials/header/basic.html" at <markdownify>:
error calling markdownify: runtime error: invalid memory address or nil pointer dereference
@jmooring
Copy link
Member

When a page is in draft mode, it does not exist. So in your case, the home page (_index.md) does not exist. Which means that the .Site.Home object does not exist.

  • Ananke calls the .Site.Home object directly
  • PaperMod: calls site.Home which returns .Site.Home
  • Blowfish: calls the markdownify function, which is aliased to .Site.Home.RenderString

The short answer is, "Don't do that." Perhaps we should just ignore the draft setting on the home page.

@JVimes
Copy link
Author

JVimes commented Sep 10, 2023

Thanks. A memory error isn't an appropriate way to handle it. Draft is conceptually valid for the home page. I want to use it while I work on the home page text.

@jmooring
Copy link
Member

A memory error isn't an appropriate way to handle it.

I agree this could be better, but what you've done is uncommon.

Draft is conceptually valid for the home page.

If draft is true on the home page, none of the content is published. So conceptually, the only way it makes sense to keep the home page in draft mode is if you are running hugo --buildDrafts or hugo server --buildDrafts, which works fine.

@JVimes
Copy link
Author

JVimes commented Sep 10, 2023

Well, that's not entirely true. My home page is the Recent Articles section. The index file is for a bit of marketing text at the top, I'm working on.

@bep bep removed the NeedsTriage label Sep 15, 2023
@bep bep added this to the v0.119.0 milestone Sep 15, 2023
vladh added a commit to vladh/hugo that referenced this issue Sep 24, 2023
Previously, it was possible to create an _index.md, but have it not be
built, such as by setting `draft: true`. This would result in there
being no homepage, and when e.g. a theme would call `.Site.Home`, this
would result in a “runtime error: invalid memory address or nil pointer
dereference”. Now, if the homepage does not end up being built, we fall
back to creating a default homepage, as if _index.md never exited.

Fixes gohugoio#11441
vladh added a commit to vladh/hugo that referenced this issue Sep 24, 2023
Previously, it was possible to create an _index.md, but have it not be
built, such as by setting `draft: true`. This would result in there
being no homepage, and when e.g. a theme would call `.Site.Home`, this
would result in a “runtime error: invalid memory address or nil pointer
dereference”. Now, if the homepage does not end up being built, we fall
back to creating a default homepage, as if _index.md never exited.

Fixes gohugoio#11441
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 5, 2023
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@bep bep modified the milestones: v0.121.0, v0.122.0 Dec 6, 2023
@jmooring
Copy link
Member

Closing as duplicate of #11809.

Copy link

github-actions bot commented Feb 9, 2024

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 Feb 9, 2024
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.

3 participants