As promised in my last post, I’ve fixed the hugo new content
future date bug.
When running hugo new content
, the static site generator Hugo crashes with a surprising error message under these conditions:
- You’ve configured Hugo to extract publish dates from file paths in the
hugo.toml
configuration, and - configured Hugo to use an archetype to populate your new file, and
- create new content with the path containing a future date.
Here’s the error that Hugo prints out when it crashes under these conditions (year 2099 added for exaggeration):
# Running hugo new content content/posts/2099-01-26-(random).md
panic: [BUG] no Page found for "[…]/content/posts/2099-01-26-7265.md"
goroutine 1 [running]:
github.com/gohugoio/hugo/create.(*contentBuilder).applyArcheType(0x140000f95c0, {0x140000545a0, 0x45}, {0x140009adad0, 0xa})
/root/project/hugo/create/content.go:278 +0x238
github.com/gohugoio/hugo/create.(*contentBuilder).buildFile(0x140000f95c0)
/root/project/hugo/create/content.go:246 +0x16c
github.com/gohugoio/hugo/create.NewContent.func1()
/root/project/hugo/create/content.go:105 +0x24c
github.com/gohugoio/hugo/create.NewContent(0x14000993cc0, {0x0, 0x0}, {0x16b2e6984, 0x20}, 0x0)
/root/project/hugo/create/content.go:109 +0x498
github.com/gohugoio/hugo/commands.newNewCommand.func1({0x0?, 0x0?}, 0x0?, 0x0?, {0x14000795c50, 0x0?, 0x0?})
/root/project/hugo/commands/new.go:60 +0x160
github.com/gohugoio/hugo/commands.(*simpleCommand).Run(0x0?, {0x108232a18?, 0x109510780?}, 0x0?, {0x14000795c50?, 0x0?, 0x0?})
/root/project/hugo/commands/commandeer.go:588 +0x48
github.com/bep/simplecobra.(*Commandeer).compile.func1(0x140007c4500?, {0x14000795c50?, 0x4?, 0x1065db95c?})
/root/project/gomodcache/github.com/bep/simplecobra@v0.4.0/simplecobra.go:113 +0x54
github.com/spf13/cobra.(*Command).execute(0x140008cf800, {0x14000795c10, 0x1, 0x1})
/root/project/gomodcache/github.com/spf13/cobra@v1.8.0/command.go:983 +0x840
github.com/spf13/cobra.(*Command).ExecuteC(0x14000023800)
/root/project/gomodcache/github.com/spf13/cobra@v1.8.0/command.go:1115 +0x344
github.com/spf13/cobra.(*Command).ExecuteContextC(...)
/root/project/gomodcache/github.com/spf13/cobra@v1.8.0/command.go:1048
github.com/bep/simplecobra.(*Exec).Execute(0x140005ced18, {0x108232a18?, 0x109510780?}, {0x1400004c0d0?, 0x1400008a648?, 0x104b202ec?})
/root/project/gomodcache/github.com/bep/simplecobra@v0.4.0/simplecobra.go:155 +0xb4
github.com/gohugoio/hugo/commands.Execute({0x1400004c0d0, 0x3, 0x3})
/root/project/hugo/commands/commandeer.go:65 +0x284
main.main()
/root/project/hugo/main.go:25 +0x70
This even happened when creating an article at 8:59 with my time zone set to UTC+9. This being a timezone-sensitive bug made me laugh. Perhaps I wasn’t meant to do any creative writing before 9:00 in the morning. Evidently: the world of software development still has lots to learn about time.
Here’s why Hugo crashes when trying to create content with future publish dates:
- Hugo creates a new content file for you at the provided path.
- Hugo then tries to populate the file with a content archetype.
- Hugo doesn’t see the file because it has a future publish date.
- When the
applyArcheType
function attempts to fill the file with a future publish date, it doesn’t find the file and Hugo panics.
My patch resolves this issue by configuring hugo new content
to not disregard
files with future publish dates:
diff --git a/commands/new.go b/commands/new.go
index 81e1c65a4..08b63a866 100644
--- a/commands/new.go
+++ b/commands/new.go
@@ -53,7 +53,9 @@
if len(args) < 1 {
return newUserError("path needs to be provided")
}
- h, err := r.Hugo(flagsToCfg(cd, nil))
+ cfg := flagsToCfg(cd, nil)
+ cfg.Set("BuildFuture", true)
+ h, err := r.Hugo(cfg)
if err != nil {
return err
}
This works, and that’s good. Still, I believe this problem can be solved one level deeper.
Here’s why Hugo disregards files with future publish dates: by default, Hugo doesn’t render pages with future publish dates. This lets you schedule and publish posts in the future without having to keeping them in a separate repository. From a usability point of view, this behavior is useful and reasonable.
For hugo content new
, a better solution would be to change Hugo to now
disregard files with future publish dates when populating
them with archetype templates. After all, you’re not rendering your entire
site and publishing it. You’re just trying to create a new blog post.
Read the pull request for a detailed description if you would like to learn more.
This is my first contribution to Hugo and I’m happy with how fast the
process was. Thanks to @jmooring
and @bep
for your help.