Skip to content

Commit 7203a95

Browse files
committed
Fix rebuilds when running hugo -w
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
1 parent fb08439 commit 7203a95

File tree

6 files changed

+49
-24
lines changed

6 files changed

+49
-24
lines changed

cache/dynacache/dynacache.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func New(opts Options) *Cache {
6767
evictedIdentities := collections.NewStack[identity.Identity]()
6868

6969
onEvict := func(k, v any) {
70-
if !opts.Running {
70+
if !opts.Watching {
7171
return
7272
}
7373
identity.WalkIdentitiesShallow(v, func(level int, id identity.Identity) bool {
@@ -97,7 +97,7 @@ type Options struct {
9797
CheckInterval time.Duration
9898
MaxSize int
9999
MinMaxSize int
100-
Running bool
100+
Watching bool
101101
}
102102

103103
// Options for a partition.

deps/deps.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (d *Deps) Init() error {
155155
}
156156

157157
if d.MemCache == nil {
158-
d.MemCache = dynacache.New(dynacache.Options{Running: d.Conf.Running(), Log: d.Log})
158+
d.MemCache = dynacache.New(dynacache.Options{Watching: d.Conf.Watching(), Log: d.Log})
159159
}
160160

161161
if d.PathSpec == nil {

hugolib/integrationtest_builder.go

+17
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,20 @@ import (
3838

3939
type TestOpt func(*IntegrationTestConfig)
4040

41+
// TestOptRunning will enable running in integration tests.
4142
func TestOptRunning() TestOpt {
4243
return func(c *IntegrationTestConfig) {
4344
c.Running = true
4445
}
4546
}
4647

48+
// TestOptWatching will enable watching in integration tests.
49+
func TestOptWatching() TestOpt {
50+
return func(c *IntegrationTestConfig) {
51+
c.Watching = true
52+
}
53+
}
54+
4755
// Enable tracing in integration tests.
4856
// THis should only be used during development and not committed to the repo.
4957
func TestOptTrace() TestOpt {
@@ -570,6 +578,10 @@ func (s *IntegrationTestBuilder) initBuilder() error {
570578
"running": s.Cfg.Running,
571579
"watch": s.Cfg.Running,
572580
})
581+
} else if s.Cfg.Watching {
582+
flags.Set("internal", maps.Params{
583+
"watch": s.Cfg.Watching,
584+
})
573585
}
574586

575587
if s.Cfg.WorkingDir != "" {
@@ -817,6 +829,11 @@ type IntegrationTestConfig struct {
817829
// Whether to simulate server mode.
818830
Running bool
819831

832+
// Watch for changes.
833+
// This is (currently) always set to true when Running is set.
834+
// Note that the CLI for the server does allow for --watch=false, but that is not used in these test.
835+
Watching bool
836+
820837
// Will print the log buffer after the build
821838
Verbose bool
822839

hugolib/rebuild_test.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,23 @@ func TestRebuildEditTextFileInBranchBundle(t *testing.T) {
121121
b.AssertRenderCountContent(1)
122122
}
123123

124+
func testRebuildBothWatchingAndRunning(t *testing.T, files string, withB func(b *IntegrationTestBuilder)) {
125+
t.Helper()
126+
for _, opt := range []TestOpt{TestOptWatching(), TestOptRunning()} {
127+
b := Test(t, files, opt)
128+
withB(b)
129+
}
130+
}
131+
124132
func TestRebuildRenameTextFileInLeafBundle(t *testing.T) {
125-
b := TestRunning(t, rebuildFilesSimple)
126-
b.AssertFileContent("public/mysection/mysectionbundle/index.html", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
133+
testRebuildBothWatchingAndRunning(t, rebuildFilesSimple, func(b *IntegrationTestBuilder) {
134+
b.AssertFileContent("public/mysection/mysectionbundle/index.html", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
127135

128-
b.RenameFile("content/mysection/mysectionbundle/mysectionbundletext.txt", "content/mysection/mysectionbundle/mysectionbundletext2.txt").Build()
129-
b.AssertFileContent("public/mysection/mysectionbundle/index.html", "mysectionbundletext2", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
130-
b.AssertRenderCountPage(3)
131-
b.AssertRenderCountContent(3)
136+
b.RenameFile("content/mysection/mysectionbundle/mysectionbundletext.txt", "content/mysection/mysectionbundle/mysectionbundletext2.txt").Build()
137+
b.AssertFileContent("public/mysection/mysectionbundle/index.html", "mysectionbundletext2", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
138+
b.AssertRenderCountPage(3)
139+
b.AssertRenderCountContent(3)
140+
})
132141
}
133142

134143
func TestRebuilEditContentFileInLeafBundle(t *testing.T) {
@@ -367,8 +376,6 @@ My short.
367376
}
368377

369378
func TestRebuildBaseof(t *testing.T) {
370-
t.Parallel()
371-
372379
files := `
373380
-- hugo.toml --
374381
title = "Hugo Site"
@@ -383,12 +390,13 @@ Baseof: {{ .Title }}|
383390
Home: {{ .Title }}|{{ .Content }}|
384391
{{ end }}
385392
`
386-
b := Test(t, files, TestOptRunning())
387-
b.AssertFileContent("public/index.html", "Baseof: Hugo Site|", "Home: Hugo Site||")
388-
b.EditFileReplaceFunc("layouts/_default/baseof.html", func(s string) string {
389-
return strings.Replace(s, "Baseof", "Baseof Edited", 1)
390-
}).Build()
391-
b.AssertFileContent("public/index.html", "Baseof Edited: Hugo Site|", "Home: Hugo Site||")
393+
testRebuildBothWatchingAndRunning(t, files, func(b *IntegrationTestBuilder) {
394+
b.AssertFileContent("public/index.html", "Baseof: Hugo Site|", "Home: Hugo Site||")
395+
b.EditFileReplaceFunc("layouts/_default/baseof.html", func(s string) string {
396+
return strings.Replace(s, "Baseof", "Baseof Edited", 1)
397+
}).Build()
398+
b.AssertFileContent("public/index.html", "Baseof Edited: Hugo Site|", "Home: Hugo Site||")
399+
})
392400
}
393401

394402
func TestRebuildSingleWithBaseof(t *testing.T) {

hugolib/site_new.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) {
123123
HandlerPost: logHookLast,
124124
Stdout: cfg.LogOut,
125125
Stderr: cfg.LogOut,
126-
StoreErrors: conf.Running(),
126+
StoreErrors: conf.Watching(),
127127
SuppressStatements: conf.IgnoredLogs(),
128128
}
129129
logger = loggers.New(logOpts)
130130

131131
}
132132

133-
memCache := dynacache.New(dynacache.Options{Running: conf.Running(), Log: logger})
133+
memCache := dynacache.New(dynacache.Options{Watching: conf.Watching(), Log: logger})
134134

135135
firstSiteDeps := &deps.Deps{
136136
Fs: cfg.Fs,

tpl/tplimpl/template_funcs.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ var (
7171
)
7272

7373
type templateExecHelper struct {
74-
running bool // whether we're in server mode.
74+
watching bool // whether we're in server/watch mode.
7575
site reflect.Value
7676
siteParams reflect.Value
7777
funcs map[string]reflect.Value
@@ -95,7 +95,7 @@ func (t *templateExecHelper) GetFunc(ctx context.Context, tmpl texttemplate.Prep
9595
}
9696

9797
func (t *templateExecHelper) Init(ctx context.Context, tmpl texttemplate.Preparer) {
98-
if t.running {
98+
if t.watching {
9999
_, ok := tmpl.(identity.IdentityProvider)
100100
if ok {
101101
t.trackDependencies(ctx, tmpl, "", reflect.Value{})
@@ -129,7 +129,7 @@ func (t *templateExecHelper) GetMethod(ctx context.Context, tmpl texttemplate.Pr
129129
name = "MainSections"
130130
}
131131

132-
if t.running {
132+
if t.watching {
133133
ctx = t.trackDependencies(ctx, tmpl, name, receiver)
134134
}
135135

@@ -151,7 +151,7 @@ func (t *templateExecHelper) GetMethod(ctx context.Context, tmpl texttemplate.Pr
151151
}
152152

153153
func (t *templateExecHelper) OnCalled(ctx context.Context, tmpl texttemplate.Preparer, name string, args []reflect.Value, result reflect.Value) {
154-
if !t.running {
154+
if !t.watching {
155155
return
156156
}
157157

@@ -238,7 +238,7 @@ func newTemplateExecuter(d *deps.Deps) (texttemplate.Executer, map[string]reflec
238238
}
239239

240240
exeHelper := &templateExecHelper{
241-
running: d.Conf.Running(),
241+
watching: d.Conf.Watching(),
242242
funcs: funcsv,
243243
site: reflect.ValueOf(d.Site),
244244
siteParams: reflect.ValueOf(d.Site.Params()),

0 commit comments

Comments
 (0)