From 32e093a8fe37f9242f6547d697cd053b4464c076 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Fri, 13 Nov 2026 16:12:57 +0000 Subject: [PATCH] sync: skip files and directories removed during listing fs.WalkDir lists a directory and then reads each child. If a child directory is removed in between (e.g. while sync --watch runs), the walk returned ErrNotExist and the whole sync command failed. Skip such entries; the next listing picks up the removal. A missing top-level path is still an error. Co-authored-by: Isaac --- libs/fileset/fileset.go | 6 ++++++ libs/fileset/fileset_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/libs/fileset/fileset.go b/libs/fileset/fileset.go index ec9dcdeb79b..67aa388b7a4 100644 --- a/libs/fileset/fileset.go +++ b/libs/fileset/fileset.go @@ -1,6 +1,7 @@ package fileset import ( + "errors" "fmt" "io/fs" pathlib "path" @@ -88,6 +89,11 @@ func (w *FileSet) Files() (out []File, err error) { func (w *FileSet) recursiveListFiles(path string, seen map[string]struct{}) (out []File, err error) { err = fs.WalkDir(w.root, path, func(name string, d fs.DirEntry, err error) error { if err != nil { + // An entry under path can be removed between listing its parent and reading it, + // e.g. while sync --watch runs. Skip it; the next listing reflects the removal. + if name != path && errors.Is(err, fs.ErrNotExist) { + return nil + } return err } diff --git a/libs/fileset/fileset_test.go b/libs/fileset/fileset_test.go index bbf524b362b..56215943d13 100644 --- a/libs/fileset/fileset_test.go +++ b/libs/fileset/fileset_test.go @@ -2,6 +2,7 @@ package fileset import ( "errors" + iofs "io/fs" "slices" "testing" @@ -139,3 +140,34 @@ func (t testIgnorer) IgnoreFile(path string) (bool, error) { return false, nil } + +// vanishingDirPath simulates a directory that is removed after its parent was listed. +type vanishingDirPath struct { + vfs.Path + dir string +} + +func (p vanishingDirPath) ReadDir(name string) ([]iofs.DirEntry, error) { + if name == p.dir { + return nil, &iofs.PathError{Op: "open", Path: name, Err: iofs.ErrNotExist} + } + return p.Path.ReadDir(name) +} + +func TestFileSet_SkipsEntryRemovedDuringListing(t *testing.T) { + fs := New(vanishingDirPath{Path: vfs.MustNew("testdata"), dir: "dir1"}) + files, err := fs.Files() + if !assert.NoError(t, err) { + return + } + + assert.Len(t, files, 2) + assert.Equal(t, "dir2/a", files[0].Relative) + assert.Equal(t, "dir2/b", files[1].Relative) +} + +func TestFileSet_MissingRootPathIsAnError(t *testing.T) { + fs := New(vfs.MustNew("testdata"), []string{"does-not-exist"}) + _, err := fs.Files() + assert.ErrorIs(t, err, iofs.ErrNotExist) +}