Backtestshugorun-20261005-133023#15174
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 B = our agent, Patch A = the merged fix.
Both patches find the same root cause. `SourceFilesystem.Contains` walks `d.mounts()`, which stats each mount. Once a static directory (or the mount root itself) is deleted, that mount is skipped. The removal event then isn't classified as static and falls through to `processPartialFileEvents`, which panics on the unknown component. Both make `Contains` stat-free and also add a guard in `processPartialFileEvents`. Patch A reuses the existing `ReverseLookup(filename, false)` API, which is the stat-free reverse mapping the filesystem already provides. The fix is a two-line change. Because it goes through the general reverse lookup, it also covers single-file mounts into static. Its new testscript for deleting a file mounted at `static/robots.txt` exercises exactly that, so the static syncer still sees the removal. The added `case files.ComponentFolderStatic` is a minimal, clearly commented guard placed exactly where the panic occurred. Patch B adds a new `RootMappingFs.MountDirs` method and walks the filesystems by hand to rebuild the prefix check. This is more new surface area for the same result. It deliberately skips single-file mounts (`fiSingleFile != nil`), so a deleted file mounted into static is still not recognized as static by `Contains`. The early `continue` in `processPartialFileEvents` avoids the panic for that case, but the event is silently dropped rather than routed to the static syncer. It also rewrites the `pss` slice construction, adding churn to a hot loop. On tests, B's Go integration tests are good and directly assert `IsStatic`/`IsContent` on removed roots. A's testscripts run the real server and check for no stderr output, so they reproduce the reported scenario end-to-end, including mounted dirs and files. Overall A is the smaller, more idiomatic fix with broader coverage, so A is preferred.
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
49m 52s
Wall-clock time in the container
Full test suite
No new failures
3,694 passed · 99 skipped · 3,802 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
119vs 78 merged
Changed lines, ours vs merged
The issue
View on GitHubunknown component: "static" in processPartialFileEvents (hugolib/hugo_sites_build.go:1084, your v0.165.0-DEV-8a5cb2344)The two patches
Our patch+107 −12
hugofs/rootmapping_fs.goindex e91ee334c..fc9aad8e4 100644@@ -264,6 +264,23 @@ func (fs *RootMappingFs) Mounts(base string) ([]FileMetaInfo, error) { return fss, nil } +// MountDirs returns the real filenames of all directory mounts below base.+// Unlike Mounts, this does not stat the mounts, so it will also return+// mounts that do not currently exist on disk (e.g. a deleted directory).+func (fs *RootMappingFs) MountDirs(base string) []string {+ base = filepathSeparator + fs.cleanName(base)+ roots := fs.getRootsWithPrefix(base)++ var dirs []string+ for _, r := range roots {+ if r.fiSingleFile != nil {+ continue+ }+ dirs = append(dirs, r.To)+ }+ return dirs+}+ func (fs *RootMappingFs) Key() string { return fs.id }hugolib/filesystems/basefs.goindex 13cc942fc..900d651ec 100644@@ -426,16 +426,22 @@ func (d *SourceFilesystem) RealFilename(rel string) string { } // Contains returns whether the given filename is a member of the current filesystem.+// Note that this does not check if the file exists, so it can be used for+// removed files and directories, including removed mount roots. 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+ found := false+ hugofs.WalkFilesystems(d.Fs, func(fs afero.Fs) bool {+ if rfs, ok := fs.(*hugofs.RootMappingFs); ok {+ for _, dir := range rfs.MountDirs(d.Name) {+ if strings.HasPrefix(filename, dir) {+ 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..55fe1e753 100644@@ -621,6 +621,38 @@ f2.txt false `) } +func TestIsStaticIsContentRemovedMountRoot(t *testing.T) {+ files := `+-- hugo.toml --+disableKinds = ["taxonomy", "term", "rss", "sitemap"]+-- static/foo/bar.txt --+bar+-- content/p1.md --+---+title: p1+---+`+ b := hugolib.TestRunning(t, files)+ bfs := b.H.BaseFs.SourceFilesystems+ staticFoo := filepath.FromSlash("/static/foo")+ staticBar := filepath.FromSlash("/static/foo/bar.txt")+ contentP1 := filepath.FromSlash("/content/p1.md")++ b.Assert(bfs.IsStatic(staticFoo), qt.IsTrue)+ b.Assert(bfs.IsContent(contentP1), qt.IsTrue)++ b.RemoveFiles("static/foo/bar.txt", "static/foo", "static", "content/p1.md", "content")++ // Removed files and mount roots should still be classified correctly.+ b.Assert(bfs.IsStatic(filepath.FromSlash("/static")), qt.IsTrue)+ b.Assert(bfs.IsStatic(staticFoo), qt.IsTrue)+ b.Assert(bfs.IsStatic(staticBar), qt.IsTrue)+ b.Assert(bfs.IsContent(staticFoo), qt.IsFalse)+ b.Assert(bfs.IsContent(filepath.FromSlash("/content")), qt.IsTrue)+ b.Assert(bfs.IsContent(contentP1), qt.IsTrue)+ b.Assert(bfs.IsStatic(contentP1), qt.IsFalse)+}+ func checkFileCount(fs afero.Fs, dirname string, c *qt.C, expected int) { c.Helper() count, names, err := countFilesAndGetFilenames(fs, dirname)hugolib/hugo_sites_build.goindex 823f9811e..e38e4a174 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 (see the server command).+ 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)+ pi := h.Configs.ContentPathParser.Parse(cps.Component, p)+ pss = append(pss, pi) if ev.added && !ev.isChangedDir && cps.Component == files.ComponentFolderContent {- addedContentPaths = append(addedContentPaths, pss[i])+ addedContentPaths = append(addedContentPaths, pi) } // Compile cache buster.hugolib/rebuild_test.goindex b6d716c37..09b9a1bff 100644@@ -222,6 +222,40 @@ func testRebuildBothWatchingAndRunning(t *testing.T, files string, withB func(b } } +func TestRebuildDeleteStaticDir(t *testing.T) {+ files := `+-- hugo.toml --+baseURL = "https://example.com/"+disableKinds = ["taxonomy", "term", "rss", "sitemap"]+-- static/foo/bar.txt --+bar+-- static/baz.txt --+baz+-- content/p1.md --+---+title: p1+---+-- layouts/home.html --+Home.+-- layouts/single.html --+Single: {{ .Title }}|+`+ b := TestRunning(t, files)+ b.AssertFileContent("public/p1/index.html", "Single: p1|")++ // Delete a directory in static.+ b.RemoveFiles("static/foo/bar.txt", "static/foo").Build()+ b.AssertFileContent("public/p1/index.html", "Single: p1|")++ // Delete the static directory itself.+ b.RemoveFiles("static/baz.txt", "static").Build()+ b.AssertFileContent("public/p1/index.html", "Single: p1|")++ // The site should still rebuild as normal.+ b.EditFileReplaceAll("content/p1.md", "title: p1", "title: p1 edited").Build()+ b.AssertFileContent("public/p1/index.html", "Single: p1 edited|")+}+ func TestRebuildRenameTextFileInLeafBundle(t *testing.T) { testRebuildBothWatchingAndRunning(t, rebuildFilesSimple, func(b *IntegrationTestBuilder) { b.AssertFileContent("public/mysection/mysectionbundle/index.html", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
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