Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions libs/fileset/fileset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fileset

import (
"errors"
"fmt"
"io/fs"
pathlib "path"
Expand Down Expand Up @@ -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
}

Expand Down
32 changes: 32 additions & 0 deletions libs/fileset/fileset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fileset

import (
"errors"
iofs "io/fs"
"slices"
"testing"

Expand Down Expand Up @@ -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)
}
Loading