Repository: gohugoio/hugo (language: go)

Issue:
Avoid writing to hugo_stats.json when there are no changes
I'm trying to use the newly added `cachebusters` with TailwindCSS 3 with a construct like the one below:

```toml
[module]
  [[module.mounts]]
    source = "assets"
    target = "assets"
  [[module.mounts]]
    source = "hugo_stats.json"
    target = "assets/watching/hugo_stats.json"
[build]
  writeStats = true
  [[build.cachebusters]]
    source = "assets/watching/hugo_stats\\.json"
    target = "styles\\.css"
```

Which looks odd and funky, but should allow me to do more fine grained builds as I would not have to trigger a Tailwind build for every template change. Unfortunately the above goes into an endless loop, which I suspect should be possible to prevent.


Discussion / hints:

Fix patch:
diff --git a/hugolib/hugo_sites_build.go b/hugolib/hugo_sites_build.go
index f86d425b366..3464385d33e 100644
--- a/hugolib/hugo_sites_build.go
+++ b/hugolib/hugo_sites_build.go
@@ -455,12 +455,21 @@ func (h *HugoSites) writeBuildStats() error {
 		HTMLElements: *htmlElements,
 	}
 
+	const hugoStatsName = "hugo_stats.json"
+
 	js, err := json.MarshalIndent(stats, "", "  ")
 	if err != nil {
 		return err
 	}
 
-	filename := filepath.Join(h.Configs.LoadingInfo.BaseConfig.WorkingDir, "hugo_stats.json")
+	filename := filepath.Join(h.Configs.LoadingInfo.BaseConfig.WorkingDir, hugoStatsName)
+
+	if existingContent, err := afero.ReadFile(hugofs.Os, filename); err == nil {
+		// Check if the content has changed.
+		if bytes.Equal(existingContent, js) {
+			return nil
+		}
+	}
 
 	// Make sure it's always written to the OS fs.
 	if err := afero.WriteFile(hugofs.Os, filename, js, 0666); err != nil {


Test patch: