Backtestshugorun-20261009-153657#14937

Issue #14937 Merged fix PR #15377 Run 9 Oct 2026 · run-20261009-153657 gohugoio/hugo

Add build.cleanDestinationDir config option

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Patch A is the better fix: it is correct, adds the requested option without risk, and is better tested.

**Patch A**
- Adds `build.cleanDestinationDir.keep`, a list of glob patterns relative to the publish directory. This is what the issue asks for.
- Adds `.git` to the files that are always kept. This directly fixes the worktree/submodule `.git` file being deleted.
- Keeps all existing protections: `.gitignore`, `.gitattributes` and dot-directories are still preserved. User patterns only add to that list.
- Its testscript covers the issue's exact scenario: `.git` file, `.domains`, `_redirects` and `foo/bar/.mysecretfile`. It runs both with and without a `static/` directory, and also checks a kept directory with nested content, `**` patterns and a submodule `.git` file in a subdirectory.
- A unit test covers pattern matching, leading-slash normalization, case sensitivity and invalid-glob errors.
- Docs are updated.
- The change is small and limited to the cleanup logic.

**Patch B**
- Moves `cleanDestinationDir` from the root config to `build.cleanDestinationDir.enable`. It deletes the RootConfig field, remaps the CLI flag and adds a deprecation path. This is a broader redesign than the issue requires.
- The built-in protections are only default values for `KeepFiles` and `KeepDirs`, so any user setting replaces them. Its own Scenario 4 shows this: setting `keepFiles = ['.domains', '_redirects']` and `keepDirs = ['keep-me']` deletes `.gitignore` and the `.hidden/` directory.
- In the issue's own setup, a user who adds `.domains` to `keepFiles` would lose protection of the worktree `.git` file. Setting any `keepDirs` would also allow real `.git` directories to be deleted.
- That is a data-loss footgun, and it undermines the core use case.
- Removing the root field adds the risk of missing other references to it.
- Negation support is extra scope that does nothing for the reported problem.

On correctness, safety and test coverage, Patch A is clearly better.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

1h 1m

Wall-clock time in the container

Full test suite

No new failures

2,097 passed · 42 skipped · 2,139 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

215vs 194 merged

Changed lines, ours vs merged

<!-- Describe this new feature. Think about if it really belongs in the Hugo core module; you may want to discuss it on https://discourse.gohugo.io/ first. -->

Usecase

I'm using Codeberg Pages to host my website, you can find the repostitory at https://codeberg.org/ilovelinux/pages

I have two branches:

  • main: hugo project
  • pages: hugo build (output of public/)

Since I'm using Codeberg Pages with a custom domain, I also need a .domains file[^codebergDomains] to be pushed in the pages branch.

When I

  • Have files in the static/ directory.
  • Switch to main branch.
  • Create the public/ folder using a git worktree of the pages branch.
  • Run hugo build --cleanDestinationDir.

I need

  • To remove outdated content
  • To retain:
  • .git file created by git worktree[^gitWorktreeFile].
  • .domains file used by Codeberg Pages in order to allow custom domains[^codebergDomains].
  • _redirects file for redirects (however I'm not using it yet)[^codebergRedirects].

Instead

  • The files I need to be retained are deleted.

Proposal

Add a configuration field to list relative file paths to public/ that should be retained when using --cleanDestinationDir.

Example

 filesToRetain:
  - .git
  - .domains
  - _redirects
  - foo/bar/.mysecretfile

Files retained:

  • public/.git
  • public/.domains
  • public/_redirects
  • public/foo/bar/.mysecretfile

[!NOTE]
I'm not suggesting to use filesToRetain as configuration field, but I couldn't think of a better name.

Notes

[!IMPORTANT]
--cleanDestinationDir does nothing when when the static/ directory doesn't exist since its value is used inside the copyStaticTo function.
https://github.com/gohugoio/hugo/blob/c4bbc2805c7092deafabae01e71ff2dda37bd877/commands/hugobuilder.go#L481

Workaround

[!WARNING]
This approach is error-prone.

I implemented a workaround in my Makefile that:

Related issues & PR

[^gitWorktreeFile]: https://git-scm.com/docs/git-worktree#_details
[^gitSubmoduleFile]: https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-absorbgitdirs
[^codebergDomains]: https://docs.codeberg.org/codeberg-pages/using-custom-domain/
[^codebergRedirects]: https://docs.codeberg.org/codeberg-pages/redirects/

The two patches

Our patch+209 −6
commands/hugobuilder.goindex a9cacaada..ce9785cc2 100644@@ -468,6 +468,7 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint  	syncer := fsync.NewSyncer() 	var clean bool+	var cleanCfg config.CleanDestinationDir 	c.withConf(func(conf *commonConfig) { 		syncer.NoTimes = conf.configs.Base.NoTimes 		syncer.NoChmod = conf.configs.Base.NoChmod@@ -475,6 +476,7 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint  		syncer.DestFs = conf.fs.PublishDirStatic 		clean = conf.configs.Base.CleanDestinationDir+		cleanCfg = conf.configs.Base.Build.CleanDestinationDir 	})  	syncer.SrcFs = fs@@ -494,11 +496,17 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint 	if clean { 		infol.Logf("removing stale files from destination") -		keep := func(f fsync.FileInfo) bool {+		keep := func(filename string, f fsync.FileInfo) bool { 			name := f.Name()  			// Keep git metadata files anywhere.-			if name == ".gitignore" || name == ".gitattributes" {+			// A .git file is created by e.g. git worktree and git submodule.+			if name == ".git" || name == ".gitignore" || name == ".gitattributes" {+				return true+			}++			// Keep files and directories matching build.cleanDestinationDir.keep.+			if cleanCfg.ShouldKeep(filename) { 				return true 			} @@ -522,7 +530,8 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint // removeStale removes entries in dstDir on dst that keep reports as false and // that don't exist in srcDir on src. Unlike fsync's delete pass, it applies // keep at every level, so kept files survive inside stale directories.-func removeStale(dst, src afero.Fs, dstDir, srcDir string, keep func(fsync.FileInfo) bool) error {+// The filename passed to keep is relative to the root of dst.+func removeStale(dst, src afero.Fs, dstDir, srcDir string, keep func(filename string, fi fsync.FileInfo) bool) error { 	entries, err := afero.ReadDir(dst, dstDir) 	if err != nil { 		if herrors.IsNotExist(err) {@@ -531,10 +540,10 @@ func removeStale(dst, src afero.Fs, dstDir, srcDir string, keep func(fsync.FileI 		return err 	} 	for _, entry := range entries {-		if keep(entry) {+		dstPath := filepath.Join(dstDir, entry.Name())+		if keep(dstPath, entry) { 			continue 		}-		dstPath := filepath.Join(dstDir, entry.Name()) 		srcPath := filepath.Join(srcDir, entry.Name()) 		if _, err := src.Stat(srcPath); err == nil { 			if entry.IsDir() {config/commonConfig.goindex dae0ed905..9bf265ecb 100644@@ -16,6 +16,7 @@ package config import ( 	"fmt" 	"net/http"+	"path/filepath" 	"regexp" 	"slices" 	"sort"@@ -112,6 +113,49 @@ type BuildConfig struct {  	// Can used to control how the resource cache gets evicted on rebuilds. 	CacheBusters []CacheBuster++	// Configures the cleanup of the publish directory when cleanDestinationDir is enabled.+	CleanDestinationDir CleanDestinationDir+}++// CleanDestinationDir configures the cleanup of stale files in the publish directory.+type CleanDestinationDir struct {+	// Glob patterns of files and directories, relative to the publish directory,+	// to keep when removing stale files, e.g. ".domains" or "**/.git".+	// A kept directory is kept with all its content.+	Keep []string++	compiledKeep []hstrings.Matcher+}++// CompileConfig compiles the Keep patterns.+func (c *CleanDestinationDir) CompileConfig(logger loggers.Logger) error {+	if c.compiledKeep != nil {+		return nil+	}+	compiled := make([]hstrings.Matcher, 0, len(c.Keep))+	for _, pattern := range c.Keep {+		g, err := glob.Compile(strings.Trim(filepath.ToSlash(pattern), "/"), '/')+		if err != nil {+			return fmt.Errorf("failed to compile build.cleanDestinationDir.keep glob %q: %w", pattern, err)+		}+		compiled = append(compiled, g)+	}+	c.compiledKeep = compiled+	return nil+}++// ShouldKeep reports whether the file or directory at filename, relative to+// the publish directory, matches one of the Keep patterns.+// CompileConfig must have been called first.+func (c CleanDestinationDir) ShouldKeep(filename string) bool {+	filename = strings.Trim(filepath.ToSlash(filename), "/")+	for _, g := range c.compiledKeep {+		if g.Match(filename) {+			return true+		}+	}+	return false }  // BuildStats configures if and what to write to the hugo_stats.json file.@@ -131,6 +175,8 @@ func (w BuildStats) Enabled() bool {  func (b BuildConfig) clone() BuildConfig { 	b.CacheBusters = slices.Clone(b.CacheBusters)+	b.CleanDestinationDir.Keep = slices.Clone(b.CleanDestinationDir.Keep)+	b.CleanDestinationDir.compiledKeep = slices.Clone(b.CleanDestinationDir.compiledKeep) 	return b } @@ -174,6 +220,9 @@ func (b *BuildConfig) CompileConfig(logger loggers.Logger) error { 		} 		b.CacheBusters[i] = cb 	}+	if err := b.CleanDestinationDir.CompileConfig(logger); err != nil {+		return err+	} 	return nil } config/commonConfig_test.goindex 05ba185e3..67ddbe482 100644@@ -163,6 +163,43 @@ func TestBuildConfigCacheBusters(t *testing.T) { 	c.Assert(m, qt.IsNil) } +func TestBuildConfigCleanDestinationDir(t *testing.T) {+	c := qt.New(t)+	l := loggers.NewDefault()++	conf := DecodeBuildConfig(New())+	c.Assert(conf.CompileConfig(l), qt.IsNil)+	c.Assert(conf.CleanDestinationDir.ShouldKeep(".domains"), qt.IsFalse)++	cfg := New()+	cfg.Set("build", map[string]any{+		"cleanDestinationDir": map[string]any{+			"keep": []string{".domains", "/_redirects", "foo/bar/.mysecretfile", "**/*.keep"},+		},+	})+	conf = DecodeBuildConfig(cfg)+	c.Assert(conf.CompileConfig(l), qt.IsNil)+	c.Assert(conf.CleanDestinationDir.Keep, qt.HasLen, 4)++	keep := conf.CleanDestinationDir.ShouldKeep+	c.Assert(keep("/.domains"), qt.IsTrue)+	c.Assert(keep(".domains"), qt.IsTrue)+	c.Assert(keep("_redirects"), qt.IsTrue)+	c.Assert(keep("/foo/bar/.mysecretfile"), qt.IsTrue)+	c.Assert(keep("/a/b/c.keep"), qt.IsTrue)+	c.Assert(keep("/sub/.domains"), qt.IsFalse)+	c.Assert(keep("/foo/bar"), qt.IsFalse)+	c.Assert(keep("/.DOMAINS"), qt.IsFalse)++	cfg.Set("build", map[string]any{+		"cleanDestinationDir": map[string]any{+			"keep": []string{"[a"},+		},+	})+	conf = DecodeBuildConfig(cfg)+	c.Assert(conf.CompileConfig(l), qt.ErrorMatches, `.*build.cleanDestinationDir.keep.*`)+}+ func TestBuildConfigCacheBusterstTailwindSetup(t *testing.T) { 	c := qt.New(t) 	cfg := New()docs/content/en/configuration/all.mdindex 842465254..3a1832e1c 100644@@ -46,7 +46,7 @@ aliases: [/getting-started/configuration/] : See [configure cascade][].  `cleanDestinationDir`-: (`bool`) Whether to remove files from the [`publishDir`](#publishdir) that do not exist in the [`staticDir`](#staticdir). Hugo performs this cleanup before rendering, so the current build's rendered output is unaffected. If the `staticDir` does not exist, the cleanup does not run. Note that `.gitignore` and `.gitattributes` files are always preserved, as are directories whose names begin with a dot, provided their parent directory also survives. Default is `false`.+: (`bool`) Whether to remove files from the [`publishDir`](#publishdir) that do not exist in the [`staticDir`](#staticdir). Hugo performs this cleanup before rendering, so the current build's rendered output is unaffected. The cleanup also runs if the `staticDir` does not exist. Note that `.git`, `.gitignore`, and `.gitattributes` files are always preserved, as are directories whose names begin with a dot. To preserve other files and directories, see [`build.cleanDestinationDir`](/configuration/build/#clean-destination-directory). Default is `false`.  `contentDir` : (`string`) The designated directory for content files. Default is `content`. {{% module-mounts-note %}}docs/content/en/configuration/build.mdindex 6a7924301..2332c952d 100644@@ -17,12 +17,33 @@ This is the default configuration: `cachebusters` : See the [cache busters](#cache-busters) section below. +`cleanDestinationDir`+: See the [clean destination directory](#clean-destination-directory) section below.+ `noJSConfigInAssets` : (`bool`) Whether to disable writing a `jsconfig.json` in your `assets` directory with mapping of imports from running [js.Build][]. This file is intended to help with intellisense/navigation inside code editors such as [VS Code][]. Note that if you do not use `js.Build`, no file will be written.  `useResourceCacheWhen` : (`string`) When to use the resource file cache, one of `never`, `fallback`, or `always`. Applicable when transpiling Sass to CSS. Default is `fallback`. +## Clean destination directory++When you run Hugo with the [`cleanDestinationDir`][] setting enabled, Hugo removes files from the [`publishDir`][] that do not exist in the [`staticDir`][]. Use the `keep` setting to retain additional files and directories, such as the `.domains` file used by Codeberg Pages:++{{< code-toggle file=hugo >}}+[build.cleanDestinationDir]+keep = ['.domains', '_redirects', 'foo/bar/.mysecretfile', '**/*.keep']+{{< /code-toggle >}}++`keep`+: (`[]string`) A slice of [glob](g) patterns, relative to the `publishDir`, matching files and directories to retain. A retained directory is retained with all of its content. Patterns are case-sensitive. A pattern without a slash, such as `.domains`, matches at the root of the `publishDir` only, while a pattern such as `**/*.keep` matches in subdirectories only. Default is `[]`.++Regardless of this setting, Hugo always retains `.git`, `.gitignore`, and `.gitattributes` files, as well as directories whose names begin with a dot.++[`cleanDestinationDir`]: /configuration/all/#cleandestinationdir+[`publishDir`]: /configuration/all/#publishdir+[`staticDir`]: /configuration/all/#staticdir+ ## Cache busters  The `build.cachebusters` setting was added to support development using Tailwind 3.x's JIT compiler where a `build` configuration may look like this:testscripts/commands/hugo__clean_destination_dir_keep.txtnew file mode 100644index 000000000..86c8d26cf@@ -0,0 +1,87 @@+# Test the build.cleanDestinationDir.keep option.++# --- Scenario 1: Without static directory ---++cpr golden_public public++hugo --cleanDestinationDir+tree public+cmp stdout no_static_clean_build.txt++# --- Scenario 2: With static directory ---++rm public++cpr golden_public public+cpr golden_static static++hugo --cleanDestinationDir+tree public+cmp stdout with_static_clean_build.txt++# Files++-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+[build.cleanDestinationDir]+keep = ['.domains', '_redirects', 'foo/bar/.mysecretfile', 'keepdir', '**/*.keep']+-- content/_index.md --+---+title: home+---+-- layouts/home.html --+{{ .Title }}++-- golden_public/.git --+gitdir: /path/to/repo/.git/worktrees/public+-- golden_public/.domains --+example.org+-- golden_public/_redirects --+-- golden_public/stale.txt --+-- golden_public/foo/bar/.mysecretfile --+-- golden_public/foo/bar/stale.txt --+-- golden_public/foo/stale.txt --+-- golden_public/keepdir/a.txt --+-- golden_public/keepdir/sub/b.txt --+-- golden_public/dir-a/c.keep --+-- golden_public/dir-a/d.txt --+-- golden_public/dir-b/.git --+gitdir: /path/to/repo/.git/modules/dir-b++-- golden_static/l.txt --++-- no_static_clean_build.txt --+.+└─.domains+└─.git+└─_redirects+└─dir-a/+  └─c.keep+└─dir-b/+  └─.git+└─foo/+  └─bar/+    └─.mysecretfile+└─index.html+└─keepdir/+  └─a.txt+  └─sub/+    └─b.txt+-- with_static_clean_build.txt --+.+└─.domains+└─.git+└─_redirects+└─dir-a/+  └─c.keep+└─dir-b/+  └─.git+└─foo/+  └─bar/+    └─.mysecretfile+└─index.html+└─keepdir/+  └─a.txt+  └─sub/+    └─b.txt+└─l.txt 
Merged fix · PR #15377+165 −29
commands/helpers.goindex fbe1d362a..16555b6c4 100644@@ -76,9 +76,10 @@ func flagsToCfgWithAdditionalConfigBase(cd *simplecobra.Commandeer, cfg config.P  	// Flags with a different name in the config. 	keyMap := map[string]string{-		"minify":      "minify.minifyOutput",-		"destination": "publishDir",-		"editor":      "newContentEditor",+		"minify":              "minify.minifyOutput",+		"destination":         "publishDir",+		"editor":              "newContentEditor",+		"cleanDestinationDir": "build.cleanDestinationDir.enable", 	}  	// Flags that we for some reason don't want to expose in the project config.commands/hugobuilder.goindex a9cacaada..963db35c2 100644@@ -467,14 +467,14 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint 	fs := &countingStatFs{Fs: sourceFs.Fs}  	syncer := fsync.NewSyncer()-	var clean bool+	var clean config.CleanDestinationDir 	c.withConf(func(conf *commonConfig) { 		syncer.NoTimes = conf.configs.Base.NoTimes 		syncer.NoChmod = conf.configs.Base.NoChmod 		syncer.ChmodFilter = chmodFilter  		syncer.DestFs = conf.fs.PublishDirStatic-		clean = conf.configs.Base.CleanDestinationDir+		clean = conf.configs.Base.Build.CleanDestinationDir 	})  	syncer.SrcFs = fs@@ -491,22 +491,9 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint 		return 0, err 	} -	if clean {+	if clean.Enable { 		infol.Logf("removing stale files from destination")--		keep := func(f fsync.FileInfo) bool {-			name := f.Name()--			// Keep git metadata files anywhere.-			if name == ".gitignore" || name == ".gitattributes" {-				return true-			}--			// Keep dot-directories anywhere.-			return f.IsDir() && strings.HasPrefix(name, ".")-		}--		if err := removeStale(syncer.DestFs, sourceFs.Fs, publishDir, helpers.FilePathSeparator, keep); err != nil {+		if err := removeStale(syncer.DestFs, sourceFs.Fs, publishDir, helpers.FilePathSeparator, clean.Keep); err != nil { 			return 0, err 		} 	}@@ -522,7 +509,8 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint // removeStale removes entries in dstDir on dst that keep reports as false and // that don't exist in srcDir on src. Unlike fsync's delete pass, it applies // keep at every level, so kept files survive inside stale directories.-func removeStale(dst, src afero.Fs, dstDir, srcDir string, keep func(fsync.FileInfo) bool) error {+// keep gets the slash separated path relative to srcDir's root, e.g. "dir/.gitignore".+func removeStale(dst, src afero.Fs, dstDir, srcDir string, keep func(path string, isDir bool) bool) error { 	entries, err := afero.ReadDir(dst, dstDir) 	if err != nil { 		if herrors.IsNotExist(err) {@@ -531,11 +519,11 @@ func removeStale(dst, src afero.Fs, dstDir, srcDir string, keep func(fsync.FileI 		return err 	} 	for _, entry := range entries {-		if keep(entry) {-			continue-		} 		dstPath := filepath.Join(dstDir, entry.Name()) 		srcPath := filepath.Join(srcDir, entry.Name())+		if keep(strings.TrimPrefix(filepath.ToSlash(srcPath), "/"), entry.IsDir()) {+			continue+		} 		if _, err := src.Stat(srcPath); err == nil { 			if entry.IsDir() { 				if err := removeStale(dst, src, dstPath, srcPath, keep); err != nil {@@ -644,7 +632,7 @@ func (c *hugoBuilder) fullBuild(noBuildLock bool) error { 	// and it does so at the end of copyStatic() call. 	var cleanDestinationDir bool 	c.withConf(func(conf *commonConfig) {-		cleanDestinationDir = conf.configs.Base.CleanDestinationDir+		cleanDestinationDir = conf.configs.Base.Build.CleanDestinationDir.Enable 	}) 	if cleanDestinationDir { 		if err := copyStaticFunc(); err != nil {config/allconfig/allconfig.goindex 806cedc71..a84545edb 100644@@ -785,10 +785,6 @@ type RootConfig struct { 	// Don't sync modification time of files for the static mounts. 	NoChmod bool -	// Clean the destination folder before a new build.-	// This currently only handles static files.-	CleanDestinationDir bool- 	// A Glob pattern of module paths to ignore in the _vendor folder. 	IgnoreVendorPaths string config/allconfig/load.goindex bc35af9d6..76eb65703 100644@@ -198,6 +198,13 @@ func (l configLoader) normalizeCfg(cfg config.Provider) error { 		} 	} +	if b, ok := cfg.Get("cleanDestinationDir").(bool); ok {+		hugo.Deprecate("project config cleanDestinationDir", "Use build.cleanDestinationDir.enable instead.", "v0.167.0")+		if b {+			cfg.Set("build.cleanDestinationDir.enable", true)+		}+	}+ 	return nil } config/commonConfig.goindex dae0ed905..cadfcb5ef 100644@@ -25,7 +25,9 @@ import ( 	"github.com/gobwas/glob" 	"github.com/gohugoio/hugo/common/hstrings" 	"github.com/gohugoio/hugo/common/loggers"+	"github.com/gohugoio/hugo/common/predicate" 	"github.com/gohugoio/hugo/common/types"+	"github.com/gohugoio/hugo/hugofs/hglob"  	"github.com/gohugoio/hugo/common/herrors" 	"github.com/mitchellh/mapstructure"@@ -86,6 +88,10 @@ type LoadConfigResult struct { var defaultBuild = BuildConfig{ 	UseResourceCacheWhen: "fallback", 	BuildStats:           BuildStats{},+	CleanDestinationDir: CleanDestinationDir{+		KeepFiles: []string{"{**/,}.{git,gitignore,gitattributes}"},+		KeepDirs:  []string{"{**/,}.*"},+	},  	CacheBusters: []CacheBuster{ 		{@@ -112,6 +118,42 @@ type BuildConfig struct {  	// Can used to control how the resource cache gets evicted on rebuilds. 	CacheBusters []CacheBuster++	// Removes files from the publish directory that are not found in the static source.+	CleanDestinationDir CleanDestinationDir+}++// CleanDestinationDir configures the removal of stale files from the publish directory.+type CleanDestinationDir struct {+	// Whether to remove stale files. Can also be enabled with the --cleanDestinationDir flag.+	Enable bool++	// Glob patterns of files to keep, matched against the slash separated path relative to the publish directory, e.g. "dir/.gitignore".+	// A pattern prefixed with "! " is a negation.+	KeepFiles []string++	// Glob patterns of directories to keep with all of their content.+	KeepDirs []string++	keepFiles predicate.P[string]+	keepDirs  predicate.P[string]+}++// Keep reports whether path, relative to the publish directory, should be kept.+func (c CleanDestinationDir) Keep(path string, isDir bool) bool {+	if isDir {+		return c.keepDirs(path)+	}+	return c.keepFiles(path)+}++func (c *CleanDestinationDir) compile() error {+	var err error+	if c.keepFiles, err = predicate.NewStringPredicateFromGlobs(c.KeepFiles, hglob.GetGlob); err != nil {+		return err+	}+	c.keepDirs, err = predicate.NewStringPredicateFromGlobs(c.KeepDirs, hglob.GetGlob)+	return err }  // BuildStats configures if and what to write to the hugo_stats.json file.@@ -131,6 +173,8 @@ func (w BuildStats) Enabled() bool {  func (b BuildConfig) clone() BuildConfig { 	b.CacheBusters = slices.Clone(b.CacheBusters)+	b.CleanDestinationDir.KeepFiles = slices.Clone(b.CleanDestinationDir.KeepFiles)+	b.CleanDestinationDir.KeepDirs = slices.Clone(b.CleanDestinationDir.KeepDirs) 	return b } @@ -174,6 +218,9 @@ func (b *BuildConfig) CompileConfig(logger loggers.Logger) error { 		} 		b.CacheBusters[i] = cb 	}+	if err := b.CleanDestinationDir.compile(); err != nil {+		return fmt.Errorf("failed to compile build.cleanDestinationDir: %w", err)+	} 	return nil } testscripts/commands/hugo__clean_destination_dir.txtindex 9191c77e9..c3273227e 100644@@ -37,24 +37,76 @@ hugo --cleanDestinationDir tree public cmp stdout mismatch_clean_build.txt +# --- Scenario 4: Custom keepFiles and keepDirs ---++rm public static++cpr golden_public_keep public++hugo --config hugo_keep.toml+tree public+cmp stdout keep_clean_build.txt++# --- Scenario 5: Negated keepFiles ---++rm public++cpr golden_public_keep public++hugo --config hugo_keep_negate.toml+tree public+cmp stdout keep_negate_clean_build.txt++# --- Scenario 6: Deprecated root cleanDestinationDir key with default keep patterns ---++rm public++cpr golden_public_keep public++hugo --config hugo_legacy.toml+tree public+cmp stdout legacy_clean_build.txt+ # Files  -- hugo.toml -- disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- hugo_keep.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+[build.cleanDestinationDir]+enable = true+keepFiles = ['.domains', '_redirects']+keepDirs = ['keep-me']+-- hugo_keep_negate.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+# Render home as JSON so no HTML gets rendered.+[outputs]+home = ['json']+[build.cleanDestinationDir]+enable = true+# Remove all HTML files, keep everything else.+keepFiles = ['! **.html']+-- hugo_legacy.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+cleanDestinationDir = true -- content/_index.md -- --- title: home --- -- layouts/home.html -- {{ .Title }}+-- layouts/home.json --+{{ .Title | jsonify }}  -- golden_public/.dir-a/a.txt -- -- golden_public/.b.txt --+-- golden_public/.git -- -- golden_public/.gitattributes -- -- golden_public/.gitignore -- -- golden_public/c.txt -- -- golden_public/dir-b/.dir-c/d.txt -- -- golden_public/dir-b/.e.txt --+-- golden_public/dir-b/.git -- -- golden_public/dir-b/.gitignore -- -- golden_public/dir-b/f.txt -- -- golden_public/dir-d/g.txt --@@ -68,6 +120,16 @@ title: home -- golden_static/dir-e/.m.txt -- -- golden_static/dir-e/n.txt -- +-- golden_public_keep/.domains --+-- golden_public_keep/.gitignore --+-- golden_public_keep/.hidden/x.txt --+-- golden_public_keep/_redirects --+-- golden_public_keep/a.html --+-- golden_public_keep/b.txt --+-- golden_public_keep/dir/.domains --+-- golden_public_keep/keep-me/x.html --+-- golden_public_keep/keep-me/y.txt --+ -- golden_public_mismatch/type-a/x.txt -- -- golden_public_mismatch/type-b -- @@ -79,6 +141,7 @@ title: home └─.b.txt └─.dir-a/   └─a.txt+└─.git └─.gitattributes └─.gitignore └─c.txt@@ -86,6 +149,7 @@ title: home   └─.dir-c/     └─d.txt   └─.e.txt+  └─.git   └─.gitignore   └─f.txt └─dir-d/@@ -101,11 +165,13 @@ title: home . └─.dir-a/   └─a.txt+└─.git └─.gitattributes └─.gitignore └─dir-b/   └─.dir-c/     └─d.txt+  └─.git   └─.gitignore └─dir-e/   └─.dir-f/@@ -117,6 +183,7 @@ title: home └─.b.txt └─.dir-a/   └─a.txt+└─.git └─.gitattributes └─.gitignore └─.k.txt@@ -125,6 +192,7 @@ title: home   └─.dir-c/     └─d.txt   └─.e.txt+  └─.git   └─.gitignore   └─f.txt └─dir-d/@@ -143,12 +211,14 @@ title: home . └─.dir-a/   └─a.txt+└─.git └─.gitattributes └─.gitignore └─.k.txt └─dir-b/   └─.dir-c/     └─d.txt+  └─.git   └─.gitignore └─dir-e/   └─.dir-f/@@ -164,3 +234,30 @@ title: home └─type-a └─type-b/   └─y.txt+-- keep_clean_build.txt --+.+└─.domains+└─_redirects+└─index.html+└─keep-me/+  └─x.html+  └─y.txt+-- keep_negate_clean_build.txt --+.+└─.domains+└─.gitignore+└─.hidden/+  └─x.txt+└─_redirects+└─b.txt+└─dir/+  └─.domains+└─index.json+└─keep-me/+  └─y.txt+-- legacy_clean_build.txt --+.+└─.gitignore+└─.hidden/+  └─x.txt+└─index.html