]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix rebuilds when running hugo -w
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 25 Apr 2024 10:30:16 +0000 (12:30 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 25 Apr 2024 12:35:49 +0000 (14:35 +0200)
This was partly broken in Hugo 0.123.0.

We have two internal config options that gets set from the CLI:

* Running; a web server is running
* Watching; either set via `hugo -w`  or `hugo server --watch=false`

Part of the change detection code wrongly used the `Running` as a flag when `Watching` would be the correct.

Fixes #12296

cache/dynacache/dynacache.go
deps/deps.go
hugolib/integrationtest_builder.go
hugolib/rebuild_test.go
hugolib/site_new.go
tpl/tplimpl/template_funcs.go

index e79de5a5bb9507ac0722e2db16a62d5bad07026e..87ef68f5be28e9c6ce50e5afed5d486788b51640 100644 (file)
@@ -67,7 +67,7 @@ func New(opts Options) *Cache {
        evictedIdentities := collections.NewStack[identity.Identity]()
 
        onEvict := func(k, v any) {
-               if !opts.Running {
+               if !opts.Watching {
                        return
                }
                identity.WalkIdentitiesShallow(v, func(level int, id identity.Identity) bool {
@@ -97,7 +97,7 @@ type Options struct {
        CheckInterval time.Duration
        MaxSize       int
        MinMaxSize    int
-       Running       bool
+       Watching      bool
 }
 
 // Options for a partition.
index 78a5330b7e9fe63f08a5cd46430a6bf2e2d32347..41a8ecb3e73bcd2a1693f0d8be17fe2b4d8de94b 100644 (file)
@@ -155,7 +155,7 @@ func (d *Deps) Init() error {
        }
 
        if d.MemCache == nil {
-               d.MemCache = dynacache.New(dynacache.Options{Running: d.Conf.Running(), Log: d.Log})
+               d.MemCache = dynacache.New(dynacache.Options{Watching: d.Conf.Watching(), Log: d.Log})
        }
 
        if d.PathSpec == nil {
index 8daf7b1caffe77c5f286a767f45b362f3ee39122..7495d33417fd66f514d0a7e01a31df9dc9393e2c 100644 (file)
@@ -38,12 +38,20 @@ import (
 
 type TestOpt func(*IntegrationTestConfig)
 
+// TestOptRunning will enable running in integration tests.
 func TestOptRunning() TestOpt {
        return func(c *IntegrationTestConfig) {
                c.Running = true
        }
 }
 
+// TestOptWatching will enable watching in integration tests.
+func TestOptWatching() TestOpt {
+       return func(c *IntegrationTestConfig) {
+               c.Watching = true
+       }
+}
+
 // Enable tracing in integration tests.
 // THis should only be used during development and not committed to the repo.
 func TestOptTrace() TestOpt {
@@ -570,6 +578,10 @@ func (s *IntegrationTestBuilder) initBuilder() error {
                                "running": s.Cfg.Running,
                                "watch":   s.Cfg.Running,
                        })
+               } else if s.Cfg.Watching {
+                       flags.Set("internal", maps.Params{
+                               "watch": s.Cfg.Watching,
+                       })
                }
 
                if s.Cfg.WorkingDir != "" {
@@ -817,6 +829,11 @@ type IntegrationTestConfig struct {
        // Whether to simulate server mode.
        Running bool
 
+       // Watch for changes.
+       // This is (currently) always set to true when Running is set.
+       // Note that the CLI for the server does allow for --watch=false, but that is not used in these test.
+       Watching bool
+
        // Will print the log buffer after the build
        Verbose bool
 
index ea7efcb6fa672a0efe29a0626b9e9933fabb9d84..4336d8b61057981528427055e9a3ccd36ad10f14 100644 (file)
@@ -121,14 +121,23 @@ func TestRebuildEditTextFileInBranchBundle(t *testing.T) {
        b.AssertRenderCountContent(1)
 }
 
+func testRebuildBothWatchingAndRunning(t *testing.T, files string, withB func(b *IntegrationTestBuilder)) {
+       t.Helper()
+       for _, opt := range []TestOpt{TestOptWatching(), TestOptRunning()} {
+               b := Test(t, files, opt)
+               withB(b)
+       }
+}
+
 func TestRebuildRenameTextFileInLeafBundle(t *testing.T) {
-       b := TestRunning(t, rebuildFilesSimple)
-       b.AssertFileContent("public/mysection/mysectionbundle/index.html", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
+       testRebuildBothWatchingAndRunning(t, rebuildFilesSimple, func(b *IntegrationTestBuilder) {
+               b.AssertFileContent("public/mysection/mysectionbundle/index.html", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
 
-       b.RenameFile("content/mysection/mysectionbundle/mysectionbundletext.txt", "content/mysection/mysectionbundle/mysectionbundletext2.txt").Build()
-       b.AssertFileContent("public/mysection/mysectionbundle/index.html", "mysectionbundletext2", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
-       b.AssertRenderCountPage(3)
-       b.AssertRenderCountContent(3)
+               b.RenameFile("content/mysection/mysectionbundle/mysectionbundletext.txt", "content/mysection/mysectionbundle/mysectionbundletext2.txt").Build()
+               b.AssertFileContent("public/mysection/mysectionbundle/index.html", "mysectionbundletext2", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
+               b.AssertRenderCountPage(3)
+               b.AssertRenderCountContent(3)
+       })
 }
 
 func TestRebuilEditContentFileInLeafBundle(t *testing.T) {
@@ -367,8 +376,6 @@ My short.
 }
 
 func TestRebuildBaseof(t *testing.T) {
-       t.Parallel()
-
        files := `
 -- hugo.toml --
 title = "Hugo Site"
@@ -383,12 +390,13 @@ Baseof: {{ .Title }}|
 Home: {{ .Title }}|{{ .Content }}|
 {{ end }}
 `
-       b := Test(t, files, TestOptRunning())
-       b.AssertFileContent("public/index.html", "Baseof: Hugo Site|", "Home: Hugo Site||")
-       b.EditFileReplaceFunc("layouts/_default/baseof.html", func(s string) string {
-               return strings.Replace(s, "Baseof", "Baseof Edited", 1)
-       }).Build()
-       b.AssertFileContent("public/index.html", "Baseof Edited: Hugo Site|", "Home: Hugo Site||")
+       testRebuildBothWatchingAndRunning(t, files, func(b *IntegrationTestBuilder) {
+               b.AssertFileContent("public/index.html", "Baseof: Hugo Site|", "Home: Hugo Site||")
+               b.EditFileReplaceFunc("layouts/_default/baseof.html", func(s string) string {
+                       return strings.Replace(s, "Baseof", "Baseof Edited", 1)
+               }).Build()
+               b.AssertFileContent("public/index.html", "Baseof Edited: Hugo Site|", "Home: Hugo Site||")
+       })
 }
 
 func TestRebuildSingleWithBaseof(t *testing.T) {
index 59b97e887eb6b427b8646163bcc36b087c0e96a3..496889295d2c8eae58aa55814787da391c6f21e6 100644 (file)
@@ -123,14 +123,14 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) {
                        HandlerPost:        logHookLast,
                        Stdout:             cfg.LogOut,
                        Stderr:             cfg.LogOut,
-                       StoreErrors:        conf.Running(),
+                       StoreErrors:        conf.Watching(),
                        SuppressStatements: conf.IgnoredLogs(),
                }
                logger = loggers.New(logOpts)
 
        }
 
-       memCache := dynacache.New(dynacache.Options{Running: conf.Running(), Log: logger})
+       memCache := dynacache.New(dynacache.Options{Watching: conf.Watching(), Log: logger})
 
        firstSiteDeps := &deps.Deps{
                Fs:                  cfg.Fs,
index 9d14b9e56b2ace05108fe74fbae56337b6a87a7f..e5ee4d54f80be5716e54e9ec082b8c855260f04d 100644 (file)
@@ -71,7 +71,7 @@ var (
 )
 
 type templateExecHelper struct {
-       running    bool // whether we're in server mode.
+       watching   bool // whether we're in server/watch mode.
        site       reflect.Value
        siteParams reflect.Value
        funcs      map[string]reflect.Value
@@ -95,7 +95,7 @@ func (t *templateExecHelper) GetFunc(ctx context.Context, tmpl texttemplate.Prep
 }
 
 func (t *templateExecHelper) Init(ctx context.Context, tmpl texttemplate.Preparer) {
-       if t.running {
+       if t.watching {
                _, ok := tmpl.(identity.IdentityProvider)
                if ok {
                        t.trackDependencies(ctx, tmpl, "", reflect.Value{})
@@ -129,7 +129,7 @@ func (t *templateExecHelper) GetMethod(ctx context.Context, tmpl texttemplate.Pr
                name = "MainSections"
        }
 
-       if t.running {
+       if t.watching {
                ctx = t.trackDependencies(ctx, tmpl, name, receiver)
        }
 
@@ -151,7 +151,7 @@ func (t *templateExecHelper) GetMethod(ctx context.Context, tmpl texttemplate.Pr
 }
 
 func (t *templateExecHelper) OnCalled(ctx context.Context, tmpl texttemplate.Preparer, name string, args []reflect.Value, result reflect.Value) {
-       if !t.running {
+       if !t.watching {
                return
        }
 
@@ -238,7 +238,7 @@ func newTemplateExecuter(d *deps.Deps) (texttemplate.Executer, map[string]reflec
        }
 
        exeHelper := &templateExecHelper{
-               running:    d.Conf.Running(),
+               watching:   d.Conf.Watching(),
                funcs:      funcsv,
                site:       reflect.ValueOf(d.Site),
                siteParams: reflect.ValueOf(d.Site.Params()),