Repository: gohugoio/hugo (language: go)

Issue:
Repeatable panic when editing i18n file, triggered after opening browser
Found when testing that [#12039](https://github.com/gohugoio/hugo/issues/12039) was resolved with hugo v0.123.0-DEV-f1491c900e.

To reproduce:

1. Close browser tab if open
2. Clone and start server

    ```text
    git clone --single-branch -b hugo-github-issue-12047 https://github.com/jmooring/hugo-testing hugo-github-issue-12047
    cd hugo-github-issue-12047
    hugo server
    ```

3. While server is running, modify i18n/en.toml, changing `foo = "FOO_EN"` to `foo = "FOO_ENsomething"` (it doesn't matter which of the i18n files you modify).

4. Now point your browser to `http://localhost:1313/`... bang.


<details>
<summary>console log</summary>

```text
Change detected, rebuilding site (#7).
2024-02-16 07:23:12.605 -0800
i18n changed /de.toml
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x134f82c]

goroutine 1076 [running]:
github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate.(*Template).New(0x0?, {0xc000f0cdf8?, 0x4?})
        /home/jmooring/code/hugo/tpl/internal/go_templates/htmltemplate/template.go:306 +0x2c
github.com/gohugoio/hugo/tpl/tplimpl.(*templateHandler).applyBaseTemplate(0x0?, {{0xc000f0cdf8, 0x12}, {0xc0005628c0, 0x1b1}, 0x0, 0x0, 0xc0009c7380}, {{0xc000f0cde0, 0x14}, ...})
        /home/jmooring/code/hugo/tpl/tplimpl/template.go:714 +0x385
github.com/gohugoio/hugo/tpl/tplimpl.(*templateHandler).findLayout(_, {{0x28a2eab, 0x4}, {0x0, 0x0}, {0x28a2feb, 0x4}, {0x0, 0x0}, {0xc00092591c, ...}, ...}, ...)
        /home/jmooring/code/hugo/tpl/tplimpl/template.go:495 +0x393
github.com/gohugoio/hugo/tpl/tplimpl.(*templateHandler).LookupLayout(_, {{0x28a2eab, 0x4}, {0x0, 0x0}, {0x28a2feb, 0x4}, {0x0, 0x0}, {0xc00092591c, ...}, ...}, ...)
        /home/jmooring/code/hugo/tpl/tplimpl/template.go:398 +0x2ca
github.com/gohugoio/hugo/hugolib.(*pageState).resolveTemplate(0xc0006e5650, {0x0, 0x0, 0x6c2e736968747b29?})
        /home/jmooring/code/hugo/hugolib/page.go:474 +0x198
github.com/gohugoio/hugo/hugolib.pageRenderer(0xc00103aa50, 0xc000b36600, 0x722e736968743a72?, 0x2c726564616f6c65?, 0xc00104af98?)
        /home/jmooring/code/hugo/hugolib/site_render.go:145 +0x25d
created by github.com/gohugoio/hugo/hugolib.(*Site).renderPages in goroutine 12
        /home/jmooring/code/hugo/hugolib/site_render.go:80 +0x107
```
</details>
<br>



Discussion / hints:

Fix patch:
diff --git a/hugolib/hugo_sites_build.go b/hugolib/hugo_sites_build.go
index aa43d541a93..3a046df3b1b 100644
--- a/hugolib/hugo_sites_build.go
+++ b/hugolib/hugo_sites_build.go
@@ -739,7 +739,6 @@ func (h *HugoSites) processPartial(ctx context.Context, l logg.LevelLogger, conf
 
 		case files.ComponentFolderLayouts:
 			tmplChanged = true
-			h.init.layouts.Reset()
 			templatePath := pathInfo.TrimLeadingSlash().PathNoLang()
 			if !h.Tmpl().HasTemplate(templatePath) {
 				tmplAdded = true
@@ -899,6 +898,9 @@ func (h *HugoSites) processPartial(ctx context.Context, l logg.LevelLogger, conf
 	}
 
 	if tmplChanged || i18nChanged {
+		// TODO(bep) we should split this, but currently the loading of i18n and layout files are tied together. See #12048.
+		h.init.layouts.Reset()
+
 		if err := loggers.TimeTrackfn(func() (logg.LevelLogger, error) {
 			// TODO(bep) this could probably be optimized to somehow
 			// only load the changed templates and its dependencies, but that is non-trivial.


Test patch: