summaryrefslogtreecommitdiffstats
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2023-12-24 19:11:05 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2024-01-27 16:28:14 +0100
commit7285e74090852b5d52f25e577850fa75f4aa8573 (patch)
tree54d07cb4a7de2db5c89f2590266595f0aca6cbd6 /tpl
parent5fd1e7490305570872d3899f5edda950903c5213 (diff)
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaningdevelop2024
There are some breaking changes in this commit, see #11455. Closes #11455 Closes #11549 This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build. The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate. A list of the notable new features: * A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server. * A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently. * You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs. We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections). Memory Limit * Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory. New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively. This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive): Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers. Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds. Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role). Fixes #10169 Fixes #10364 Fixes #10482 Fixes #10630 Fixes #10656 Fixes #10694 Fixes #10918 Fixes #11262 Fixes #11439 Fixes #11453 Fixes #11457 Fixes #11466 Fixes #11540 Fixes #11551 Fixes #11556 Fixes #11654 Fixes #11661 Fixes #11663 Fixes #11664 Fixes #11669 Fixes #11671 Fixes #11807 Fixes #11808 Fixes #11809 Fixes #11815 Fixes #11840 Fixes #11853 Fixes #11860 Fixes #11883 Fixes #11904 Fixes #7388 Fixes #7425 Fixes #7436 Fixes #7544 Fixes #7882 Fixes #7960 Fixes #8255 Fixes #8307 Fixes #8863 Fixes #8927 Fixes #9192 Fixes #9324
Diffstat (limited to 'tpl')
-rw-r--r--tpl/collections/apply.go2
-rw-r--r--tpl/collections/apply_test.go5
-rw-r--r--tpl/collections/collections.go7
-rw-r--r--tpl/collections/collections_test.go4
-rw-r--r--tpl/collections/integration_test.go7
-rw-r--r--tpl/collections/where.go4
-rw-r--r--tpl/data/data.go4
-rw-r--r--tpl/data/resources.go3
-rw-r--r--tpl/data/resources_test.go7
-rw-r--r--tpl/debug/integration_test.go4
-rw-r--r--tpl/diagrams/diagrams.go2
-rw-r--r--tpl/diagrams/goat.go2
-rw-r--r--tpl/diagrams/init.go2
-rw-r--r--tpl/fmt/integration_test.go3
-rw-r--r--tpl/images/integration_test.go2
-rw-r--r--tpl/internal/go_templates/staticcheck.conf1
-rw-r--r--tpl/internal/go_templates/texttemplate/hugo_template.go19
-rw-r--r--tpl/internal/go_templates/texttemplate/hugo_template_test.go4
-rw-r--r--tpl/internal/templatefuncsRegistry.go2
-rw-r--r--tpl/js/js.go1
-rw-r--r--tpl/lang/lang_test.go9
-rw-r--r--tpl/math/math_test.go4
-rw-r--r--tpl/openapi/openapi3/integration_test.go2
-rw-r--r--tpl/openapi/openapi3/openapi3.go28
-rw-r--r--tpl/os/integration_test.go2
-rw-r--r--tpl/page/init.go4
-rw-r--r--tpl/page/integration_test.go29
-rw-r--r--tpl/partials/integration_test.go5
-rw-r--r--tpl/partials/partials.go31
-rw-r--r--tpl/reflect/reflect_test.go2
-rw-r--r--tpl/resources/integration_test.go7
-rw-r--r--tpl/resources/resources.go16
-rw-r--r--tpl/safe/init.go5
-rw-r--r--tpl/safe/safe.go7
-rw-r--r--tpl/safe/safe_test.go27
-rw-r--r--tpl/site/init.go2
-rw-r--r--tpl/strings/strings.go12
-rw-r--r--tpl/template.go76
-rw-r--r--tpl/template_info.go14
-rw-r--r--tpl/template_test.go2
-rw-r--r--tpl/templates/integration_test.go2
-rw-r--r--tpl/time/init.go2
-rw-r--r--tpl/time/time.go42
-rw-r--r--tpl/tplimpl/template.go136
-rw-r--r--tpl/tplimpl/template_ast_transformers.go47
-rw-r--r--tpl/tplimpl/template_ast_transformers_test.go1
-rw-r--r--tpl/tplimpl/template_errors.go23
-rw-r--r--tpl/tplimpl/template_funcs.go70
-rw-r--r--tpl/transform/integration_test.go4
-rw-r--r--tpl/transform/transform.go22
-rw-r--r--tpl/transform/unmarshal.go43
-rw-r--r--tpl/transform/unmarshal_test.go9
52 files changed, 362 insertions, 408 deletions
diff --git a/tpl/collections/apply.go b/tpl/collections/apply.go
index 397ba0fdb..3d50395b9 100644
--- a/tpl/collections/apply.go
+++ b/tpl/collections/apply.go
@@ -67,7 +67,7 @@ func (ns *Namespace) Apply(ctx context.Context, c any, fname string, args ...any
func applyFnToThis(ctx context.Context, fn, this reflect.Value, args ...any) (reflect.Value, error) {
num := fn.Type().NumIn()
- if num > 0 && fn.Type().In(0).Implements(hreflect.ContextInterface) {
+ if num > 0 && hreflect.IsContextType(fn.Type().In(0)) {
args = append([]any{ctx}, args...)
}
diff --git a/tpl/collections/apply_test.go b/tpl/collections/apply_test.go
index aa39923b7..0a5764264 100644
--- a/tpl/collections/apply_test.go
+++ b/tpl/collections/apply_test.go
@@ -22,6 +22,7 @@ import (
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/config/testconfig"
+ "github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/output"
"github.com/gohugoio/hugo/output/layouts"
"github.com/gohugoio/hugo/tpl"
@@ -29,6 +30,10 @@ import (
type templateFinder int
+func (templateFinder) GetIdentity(string) (identity.Identity, bool) {
+ return identity.StringIdentity("test"), true
+}
+
func (templateFinder) Lookup(name string) (tpl.Template, bool) {
return nil, false
}
diff --git a/tpl/collections/collections.go b/tpl/collections/collections.go
index e34753f17..61fd138e9 100644
--- a/tpl/collections/collections.go
+++ b/tpl/collections/collections.go
@@ -35,11 +35,6 @@ import (
"github.com/spf13/cast"
)
-func init() {
- // htime.Now cannot be used here
- rand.Seed(time.Now().UTC().UnixNano())
-}
-
// New returns a new instance of the collections-namespaced template functions.
func New(deps *deps.Deps) *Namespace {
language := deps.Conf.Language()
@@ -149,7 +144,7 @@ func (ns *Namespace) Delimit(ctx context.Context, l, sep any, last ...any) (stri
}
default:
- return "", fmt.Errorf("can't iterate over %v", l)
+ return "", fmt.Errorf("can't iterate over %T", l)
}
return str, nil
diff --git a/tpl/collections/collections_test.go b/tpl/collections/collections_test.go
index dcdd3bd5c..7dd518759 100644
--- a/tpl/collections/collections_test.go
+++ b/tpl/collections/collections_test.go
@@ -699,7 +699,6 @@ func TestShuffleRandomising(t *testing.T) {
// of the sequence happens to be the same as the original sequence. However
// the probability of the event is 10^-158 which is negligible.
seqLen := 100
- rand.Seed(time.Now().UTC().UnixNano())
for _, test := range []struct {
seq []int
@@ -895,6 +894,7 @@ func (x TstX) TstRv2() string {
return "r" + x.B
}
+//lint:ignore U1000 reflect test
func (x TstX) unexportedMethod() string {
return x.unexported
}
@@ -923,7 +923,7 @@ func (x TstX) String() string {
type TstX struct {
A, B string
- unexported string
+ unexported string //lint:ignore U1000 reflect test
}
type TstParams struct {
diff --git a/tpl/collections/integration_test.go b/tpl/collections/integration_test.go
index a443755f8..24727a12c 100644
--- a/tpl/collections/integration_test.go
+++ b/tpl/collections/integration_test.go
@@ -1,4 +1,4 @@
-// Copyright 2022 The Hugo Authors. All rights reserved.
+// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -97,11 +97,9 @@ func TestAppendSliceToASliceOfSlices(t *testing.T) {
).Build()
b.AssertFileContent("public/index.html", "[[a] [b] [c]]")
-
}
func TestAppendNilToSlice(t *testing.T) {
-
t.Parallel()
files := `
@@ -123,11 +121,9 @@ func TestAppendNilToSlice(t *testing.T) {
).Build()
b.AssertFileContent("public/index.html", "[a &lt;nil&gt;]")
-
}
func TestAppendNilsToSliceWithNils(t *testing.T) {
-
t.Parallel()
files := `
@@ -153,7 +149,6 @@ func TestAppendNilsToSliceWithNils(t *testing.T) {
b.AssertFileContent("public/index.html", "[a &lt;nil&gt; c &lt;nil&gt;]")
}
-
}
// Issue 11234.
diff --git a/tpl/collections/where.go b/tpl/collections/where.go
index 07c2d3deb..bf3f75044 100644
--- a/tpl/collections/where.go
+++ b/tpl/collections/where.go
@@ -51,7 +51,7 @@ func (ns *Namespace) Where(ctx context.Context, c, key any, args ...any) (any, e
case reflect.Map:
return ns.checkWhereMap(ctxv, seqv, kv, mv, path, op)
default:
- return nil, fmt.Errorf("can't iterate over %v", c)
+ return nil, fmt.Errorf("can't iterate over %T", c)
}
}
@@ -320,7 +320,7 @@ func evaluateSubElem(ctx, obj reflect.Value, elemName string) (reflect.Value, er
mt := objPtr.Type().Method(index)
num := mt.Type.NumIn()
maxNumIn := 1
- if num > 1 && mt.Type.In(1).Implements(hreflect.ContextInterface) {
+ if num > 1 && hreflect.IsContextType(mt.Type.In(1)) {
args = []reflect.Value{ctx}
maxNumIn = 2
}
diff --git a/tpl/data/data.go b/tpl/data/data.go
index 380c25685..b6b0515e8 100644
--- a/tpl/data/data.go
+++ b/tpl/data/data.go
@@ -24,6 +24,7 @@ import (
"net/http"
"strings"
+ "github.com/gohugoio/hugo/cache/filecache"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/config/security"
@@ -33,7 +34,6 @@ import (
"github.com/spf13/cast"
- "github.com/gohugoio/hugo/cache/filecache"
"github.com/gohugoio/hugo/deps"
)
@@ -108,7 +108,7 @@ func (ns *Namespace) GetJSON(args ...any) (any, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
- return nil, fmt.Errorf("Failed to create request for getJSON resource %s: %w", url, err)
+ return nil, fmt.Errorf("failed to create request for getJSON resource %s: %w", url, err)
}
unmarshal := func(b []byte) (bool, error) {
diff --git a/tpl/data/resources.go b/tpl/data/resources.go
index 45764dae7..3a3701d60 100644
--- a/tpl/data/resources.go
+++ b/tpl/data/resources.go
@@ -23,7 +23,6 @@ import (
"time"
"github.com/gohugoio/hugo/cache/filecache"
-
"github.com/gohugoio/hugo/helpers"
"github.com/spf13/afero"
)
@@ -68,7 +67,7 @@ func (ns *Namespace) getRemote(cache *filecache.Cache, unmarshal func([]byte) (b
res.Body.Close()
if isHTTPError(res) {
- return nil, fmt.Errorf("Failed to retrieve remote file: %s, body: %q", http.StatusText(res.StatusCode), b)
+ return nil, fmt.Errorf("failed to retrieve remote file: %s, body: %q", http.StatusText(res.StatusCode), b)
}
retry, err = unmarshal(b)
diff --git a/tpl/data/resources_test.go b/tpl/data/resources_test.go
index d452a2a43..b8003bf43 100644
--- a/tpl/data/resources_test.go
+++ b/tpl/data/resources_test.go
@@ -15,9 +15,6 @@ package data
import (
"bytes"
-
- "github.com/gohugoio/hugo/common/loggers"
-
"net/http"
"net/http/httptest"
"net/url"
@@ -26,12 +23,14 @@ import (
"testing"
"time"
+ "github.com/gohugoio/hugo/cache/filecache"
+ "github.com/gohugoio/hugo/common/loggers"
+
"github.com/gohugoio/hugo/config/testconfig"
"github.com/gohugoio/hugo/helpers"
qt "github.com/frankban/quicktest"
- "github.com/gohugoio/hugo/cache/filecache"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/hugofs"
diff --git a/tpl/debug/integration_test.go b/tpl/debug/integration_test.go
index 3d120580d..9a36e2d12 100644
--- a/tpl/debug/integration_test.go
+++ b/tpl/debug/integration_test.go
@@ -1,4 +1,4 @@
-// Copyright 2023 The Hugo Authors. All rights reserved.
+// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -41,5 +41,5 @@ disableKinds = ["taxonomy", "term"]
},
).Build()
- b.AssertLogContains("imer: name \"foo\" count '\\x05' duration")
+ b.AssertLogContains("timer: name foo count 5 duration")
}
diff --git a/tpl/diagrams/diagrams.go b/tpl/diagrams/diagrams.go
index dfa29a978..6a58bcfe4 100644
--- a/tpl/diagrams/diagrams.go
+++ b/tpl/diagrams/diagrams.go
@@ -1,4 +1,4 @@
-// Copyright 2022 The Hugo Authors. All rights reserved.
+// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/tpl/diagrams/goat.go b/tpl/diagrams/goat.go
index f3d4f4bfb..fe156f1e8 100644
--- a/tpl/diagrams/goat.go
+++ b/tpl/diagrams/goat.go
@@ -1,4 +1,4 @@
-// Copyright 2022 The Hugo Authors. All rights reserved.
+// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/tpl/diagrams/init.go b/tpl/diagrams/init.go
index e6356ce9c..0cbec7e1b 100644
--- a/tpl/diagrams/init.go
+++ b/tpl/diagrams/init.go
@@ -1,4 +1,4 @@
-// Copyright 2022 The Hugo Authors. All rights reserved.
+// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/tpl/fmt/integration_test.go b/tpl/fmt/integration_test.go
index 5010fa90e..40bfefcdc 100644
--- a/tpl/fmt/integration_test.go
+++ b/tpl/fmt/integration_test.go
@@ -1,4 +1,4 @@
-// Copyright 2023 The Hugo Authors. All rights reserved.
+// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -41,5 +41,4 @@ ignoreErrors = ['error-b']
b.BuildE()
b.AssertLogMatches(`^ERROR a\nYou can suppress this error by adding the following to your site configuration:\nignoreErrors = \['error-a'\]\n$`)
-
}
diff --git a/tpl/images/integration_test.go b/tpl/images/integration_test.go
index ad810ad92..81f35e39c 100644
--- a/tpl/images/integration_test.go
+++ b/tpl/images/integration_test.go
@@ -1,4 +1,4 @@
-// Copyright 2023 The Hugo Authors. All rights reserved.
+// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
diff --git a/tpl/internal/go_templates/staticcheck.conf b/tpl/internal/go_templates/staticcheck.conf
new file mode 100644
index 000000000..9cf5a78a4
--- /dev/null
+++ b/tpl/internal/go_templates/staticcheck.conf
@@ -0,0 +1 @@
+checks = ["none"] \ No newline at end of file
diff --git a/tpl/internal/go_templates/texttemplate/hugo_template.go b/tpl/internal/go_templates/texttemplate/hugo_template.go
index 78be55e18..4db40ce82 100644
--- a/tpl/internal/go_templates/texttemplate/hugo_template.go
+++ b/tpl/internal/go_templates/texttemplate/hugo_template.go
@@ -1,4 +1,4 @@
-// Copyright 2022 The Hugo Authors. All rights reserved.
+// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -59,23 +59,6 @@ func NewExecuter(helper ExecHelper) Executer {
return &executer{helper: helper}
}
-type (
- pageContextKeyType string
- hasLockContextKeyType string
- stackContextKeyType string
- callbackContextKeyType string
-)
-
-const (
- // The data page passed to ExecuteWithContext gets stored with this key.
- PageContextKey = pageContextKeyType("page")
- // Used in partialCached to signal to nested templates that a lock is already taken.
- HasLockContextKey = hasLockContextKeyType("hasLock")
-
- // Used to pass down a callback function to nested templates.
- CallbackContextKey = callbackContextKeyType("callback")
-)
-
// Note: The context is currently not fully implemented in Hugo. This is a work in progress.
func (t *executer) ExecuteWithContext(ctx context.Context, p Preparer, wr io.Writer, data any) error {
if ctx == nil {
diff --git a/tpl/internal/go_templates/texttemplate/hugo_template_test.go b/tpl/internal/go_templates/texttemplate/hugo_template_test.go
index cc88151e3..c68b747dd 100644
--- a/tpl/internal/go_templates/texttemplate/hugo_template_test.go
+++ b/tpl/internal/go_templates/texttemplate/hugo_template_test.go
@@ -1,4 +1,4 @@
-// Copyright 2022 The Hugo Authors. All rights reserved.
+// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ func (e *execHelper) GetMapValue(ctx context.Context, tmpl Preparer, m, key refl
return m.MapIndex(key), true
}
-func (e *execHelper) GetMethod(ctx context.Context, tmpl Preparer, receiver reflect.Value, name string) (method reflect.Value, firstArg reflect.Value) {
+func (e *execHelper) GetMethod(ctx context.Context, tmpl Preparer, receiver reflect.Value, name string) (reflect.Value, reflect.Value) {
if name != "Hello1" {
return zero, zero
}
diff --git a/tpl/internal/templatefuncsRegistry.go b/tpl/internal/templatefuncsRegistry.go
index c1b01f5a5..fc02a6ef9 100644
--- a/tpl/internal/templatefuncsRegistry.go
+++ b/tpl/internal/templatefuncsRegistry.go
@@ -170,7 +170,7 @@ func (namespaces TemplateFuncsNamespaces) MarshalJSON() ([]byte, error) {
for i, ns := range namespaces {
- b, err := ns.toJSON(context.TODO())
+ b, err := ns.toJSON(context.Background())
if err != nil {
return nil, err
}
diff --git a/tpl/js/js.go b/tpl/js/js.go
index bb8d20966..63a676532 100644
--- a/tpl/js/js.go
+++ b/tpl/js/js.go
@@ -34,7 +34,6 @@ func New(deps *deps.Deps) *Namespace {
// Namespace provides template functions for the "js" namespace.
type Namespace struct {
- deps *deps.Deps
client *js.Client
}
diff --git a/tpl/lang/lang_test.go b/tpl/lang/lang_test.go
index 8d5430f6f..6ec40cab3 100644
--- a/tpl/lang/lang_test.go
+++ b/tpl/lang/lang_test.go
@@ -41,8 +41,8 @@ func TestNumFmt(t *testing.T) {
{6, -12345.6789, "-|,| ", "|", "-12 345,678900"},
// Arabic, ar_AE
- {6, -12345.6789, "‏- ٫ ٬", "", "‏-12٬345٫678900"},
- {6, -12345.6789, "‏-|٫| ", "|", "‏-12 345٫678900"},