Backtestshugorun-20261024-162305#15174

Issue #15174 Merged fix PR # Run 24 Oct 2026 · run-20261024-162305 gohugoio/hugo

Panic when deleting a directory in static when server is running

Worse

The merged fix was judged better than our patch.

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

Both patches address the same two-part root cause: (1) SourceFilesystem.Contains depended on the mount directory still existing on disk, so IsStatic returned false for a removed static directory and the event leaked into the dynamic rebuild path, and (2) processPartialFileEvents' component switch had no case for static. The independent tests confirm both patches fix every scenario (subdir, nested dir, root, mounted dir, rename, mixed batch). The difference is in how they get there. Patch B's Contains is a two-line reuse of the existing ReverseLookup(filename, false) machinery, which is already stat-free and already handles mounts, overlays and single-file mounts consistently with the rest of BaseFs; its switch change is the minimal, self-documenting 'handled by the static file syncer' case that keeps the rest of the loop (path parsing, cache-buster compilation) untouched. Patch A instead adds a new public RootMappingFs.MountDirs API, reimplements prefix matching by walking the filesystem stack and comparing r.To and r.Meta.Component, and restructures the pss loop to drop static paths entirely before cache-buster compilation. That is more surface area and more behavioural change than the issue asks for, and the early `continue` silently excludes static paths from cache busting rather than just from the component switch; it also leaves the original switch panic in place for any static path that might still arrive by another route. On tests, Patch A's Go rebuild tests (TestRunning + RemoveFiles for subdir and root deletion) are deterministic and directly mirror the issue, and its basefs unit test pins the Contains contract; these are stronger than Patch B's timing-based testscripts (`sleep 2`, `! stderr .`), which also only exercise module.mounts cases rather than a plain static/ subdirectory. That test advantage is real but does not outweigh B's cleaner, lower-risk fix: B resolves the root cause with less new code, better reuse of existing abstractions, and no change to the handling of non-static events, so B is the better patch.

Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.

Solve

1h 28m

Wall-clock time in the container

Full test suite

No new failures

2,883 passed · 8 skipped · 2,955 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

134vs 78 merged

Changed lines, ours vs merged

unknown  component: "static" in processPartialFileEvents (hugolib/hugo_sites_build.go:1084, your v0.165.0-DEV-8a5cb2344)

The two patches

Our patch+122 −12
hugofs/rootmapping_fs.goindex e91ee334c..5971f05c8 100644@@ -264,6 +264,21 @@ func (fs *RootMappingFs) Mounts(base string) ([]FileMetaInfo, error) { 	return fss, nil } +// MountDirs returns the source directories of the directory mounts below the given base.+// Unlike Mounts, this also includes directories that no longer exist on disk+// (e.g. removed while the server is running).+func (fs *RootMappingFs) MountDirs(base string) []*RootMapping {+	base = filepathSeparator + fs.cleanName(base)+	var roots []*RootMapping+	for _, r := range fs.getRootsWithPrefix(base) {+		if r.fiSingleFile != nil {+			continue+		}+		roots = append(roots, r)+	}+	return roots+}+ func (fs *RootMappingFs) Key() string { 	return fs.id }hugolib/filesystems/basefs.goindex 13cc942fc..238e734c4 100644@@ -427,15 +427,21 @@ func (d *SourceFilesystem) RealFilename(rel string) string {  // Contains returns whether the given filename is a member of the current filesystem. func (d *SourceFilesystem) Contains(filename string) bool {-	for _, dir := range d.mounts() {-		if !dir.IsDir() {-			continue-		}-		if strings.HasPrefix(filename, dir.Meta().Filename) {-			return true+	// Note that we don't require the mount directory to exist,+	// as this is used to classify events for removed files and directories.+	var found bool+	hugofs.WalkFilesystems(d.Fs, func(fs afero.Fs) bool {+		if rfs, ok := fs.(*hugofs.RootMappingFs); ok {+			for _, r := range rfs.MountDirs(d.Name) {+				if r.Meta.Component == d.Name && strings.HasPrefix(filename, r.To) {+					found = true+					return true+				}+			} 		}-	}-	return false+		return false+	})+	return found }  // RealDirs gets a list of absolute paths to directories starting from the givenhugolib/filesystems/basefs_test.goindex 883cf4d28..9e6b56319 100644@@ -680,3 +680,40 @@ func setConfigAndWriteSomeFilesTo(fs afero.Fs, v config.Provider, key, val strin 		afero.WriteFile(fs, filename, fmt.Appendf(nil, "content:%s:%d", key, i+1), 0o755) 	} }++// File events for removed files and directories must still be classified+// by component, even if the mount root itself has been removed.+func TestIsStaticIsContentRemovedMountRoot(t *testing.T) {+	files := `+-- hugo.toml --+-- static/mydir/foo.txt --+Foo.+-- content/p1.md --+---+title: "P1"+---+`+	b := hugolib.Test(t, files)+	bfs := b.H.BaseFs+	fs := b.H.Fs.Source++	staticDir := filepath.FromSlash("/static")+	staticFile := filepath.Join(staticDir, "mydir", "foo.txt")+	contentDir := filepath.FromSlash("/content")+	contentFile := filepath.Join(contentDir, "p1.md")++	b.Assert(bfs.IsStatic(staticFile), qt.IsTrue)+	b.Assert(bfs.IsContent(contentFile), qt.IsTrue)+	b.Assert(bfs.IsStatic(contentFile), qt.IsFalse)+	b.Assert(bfs.IsContent(staticFile), qt.IsFalse)++	b.Assert(fs.RemoveAll(staticDir), qt.IsNil)+	b.Assert(fs.RemoveAll(contentDir), qt.IsNil)++	b.Assert(bfs.IsStatic(staticDir), qt.IsTrue)+	b.Assert(bfs.IsStatic(filepath.Join(staticDir, "mydir")), qt.IsTrue)+	b.Assert(bfs.IsStatic(staticFile), qt.IsTrue)+	b.Assert(bfs.IsContent(contentFile), qt.IsTrue)+	b.Assert(bfs.IsStatic(contentFile), qt.IsFalse)+	b.Assert(bfs.IsContent(staticFile), qt.IsFalse)+}hugolib/hugo_sites_build.goindex 823f9811e..7c5b36a44 100644@@ -919,8 +919,13 @@ func (h *HugoSites) processPartialFileEvents(ctx context.Context, l logg.LevelLo  	for _, ev := range eventInfos { 		cpss := h.BaseFs.ResolvePaths(ev.Name)-		pss := make([]*paths.Path, len(cpss))-		for i, cps := range cpss {+		pss := make([]*paths.Path, 0, len(cpss))+		for _, cps := range cpss {+			if cps.Component == files.ComponentFolderStatic {+				// Static files are not part of the site build;+				// they are synced to the publish dir separately.+				continue+			} 			p := cps.Path 			if ev.removed && !paths.HasExt(p) { 				// Assume this is a renamed/removed directory.@@ -932,9 +937,10 @@ func (h *HugoSites) processPartialFileEvents(ctx context.Context, l logg.LevelLo 				deletedDirs = append(deletedDirs, cps.Path) 			} -			pss[i] = h.Configs.ContentPathParser.Parse(cps.Component, p)+			ps := h.Configs.ContentPathParser.Parse(cps.Component, p)+			pss = append(pss, ps) 			if ev.added && !ev.isChangedDir && cps.Component == files.ComponentFolderContent {-				addedContentPaths = append(addedContentPaths, pss[i])+				addedContentPaths = append(addedContentPaths, ps) 			}  			// Compile cache buster.hugolib/rebuild_test.goindex b6d716c37..e3f243a92 100644@@ -2211,3 +2211,49 @@ objects: ["o1"] 	b.AssertFileContent("public/objects/index.html", "Objects|taxonomy|<p>Objects content edited.</p>") 	b.AssertFileContent("public/objects/o1/index.html", "O1|term|") }++// Static files are synced to the publish dir outside of the site build, so+// file events for them should not be processed by the partial rebuild.+func TestRebuildDeleteStaticDir(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+baseURL = "https://example.com"+disableKinds = ["term", "taxonomy", "sitemap", "robotstxt", "404", "rss"]+disableLiveReload = true+-- static/mydir/foo.txt --+Foo.+-- static/bar.txt --+Bar.+-- content/p1.md --+---+title: "P1"+---+P1 Content.+-- layouts/home.html --+Home.+-- layouts/page.html --+{{ .Title }}|{{ .Content }}|+`++	for _, tc := range []struct {+		name   string+		remove []string+	}{+		{"subdir", []string{"static/mydir/foo.txt", "static/mydir"}},+		{"root", []string{"static/mydir/foo.txt", "static/mydir", "static/bar.txt", "static"}},+	} {+		t.Run(tc.name, func(t *testing.T) {+			t.Parallel()+			b := TestRunning(t, files)+			b.AssertFileContent("public/p1/index.html", "P1|<p>P1 Content.</p>\n|")++			b.RemoveFiles(tc.remove...).Build()+			b.AssertFileContent("public/p1/index.html", "P1|<p>P1 Content.</p>\n|")++			b.EditFileReplaceAll("content/p1.md", "P1 Content.", "P1 Content Edited.").Build()+			b.AssertFileContent("public/p1/index.html", "P1|<p>P1 Content Edited.</p>\n|")+		})+	}+} 
Merged fix · PR #+69 −9
hugolib/filesystems/basefs.goindex 13cc942fc..74f59b03c 100644@@ -426,16 +426,10 @@ func (d *SourceFilesystem) RealFilename(rel string) string { }  // Contains returns whether the given filename is a member of the current filesystem.+// It's stat-free, so it also works for deleted files. func (d *SourceFilesystem) Contains(filename string) bool {-	for _, dir := range d.mounts() {-		if !dir.IsDir() {-			continue-		}-		if strings.HasPrefix(filename, dir.Meta().Filename) {-			return true-		}-	}-	return false+	cps, _ := d.ReverseLookup(filename, false)+	return len(cps) > 0 }  // RealDirs gets a list of absolute paths to directories starting from the givenhugolib/hugo_sites_build.goindex 823f9811e..0feaada59 100644@@ -1080,6 +1080,8 @@ func (h *HugoSites) processPartialFileEvents(ctx context.Context, l logg.LevelLo 			changes = append(changes, identity.GenghisKhan) 		case files.ComponentFolderArchetypes: 			// Ignore for now.+		case files.ComponentFolderStatic:+			// Handled by the static file syncer. 		default: 			panic(fmt.Sprintf("unknown component: %q", pathInfo.Component())) 		}testscripts/server/server__delete_static_file_mount.txtnew file mode 100644index 000000000..df77b2a04@@ -0,0 +1,32 @@+# Test the hugo server command when removing a file mounted into static.+# See issue 15174.++hugo server &++waitServer++rm myfiles/robots.txt++sleep 2++stopServer+! stderr .++-- hugo.toml --+title = "Hugo Server Test"+baseURL = "https://example.org/"+disableKinds = ["taxonomy", "term", "sitemap"]+[[module.mounts]]+source = "content"+target = "content"+[[module.mounts]]+source = "layouts"+target = "layouts"+[[module.mounts]]+source = "myfiles/robots.txt"+target = "static/robots.txt"+-- layouts/all.html --+All.+-- content/p1.md --+-- myfiles/robots.txt --+robotstestscripts/server/server__delete_static_mount_dir.txtnew file mode 100644index 000000000..8c338812e@@ -0,0 +1,32 @@+# Test the hugo server command when removing a directory mounted into static.+# See issue 15174.++hugo server &++waitServer++rm mystatic/foo++sleep 2++stopServer+! stderr .++-- hugo.toml --+title = "Hugo Server Test"+baseURL = "https://example.org/"+disableKinds = ["taxonomy", "term", "sitemap"]+[[module.mounts]]+source = "content"+target = "content"+[[module.mounts]]+source = "layouts"+target = "layouts"+[[module.mounts]]+source = "mystatic/foo"+target = "static/foo"+-- layouts/all.html --+All.+-- content/p1.md --+-- mystatic/foo/static.txt --+static