Skip to content

Commit 5530ec2

Browse files
committed
filesystem: handle missing dir in 'ReadDirRecursive()'
If 'ReadDirRecursive()' receives a non-existent root path, the initial 'os.ReadDir()' throws a ENOENT error. Handle this case more gracefully (i.e., without error) by returning an empty entry list with no error if the we should exit gracefully if the error from 'os.ReadDir()' is 'os.ErrNotExist' (corresponding to ENOENT). Signed-off-by: Victoria Dye <[email protected]>
1 parent 05fe98e commit 5530ec2

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

internal/common/filesystem.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,13 @@ func (f *fileSystem) ReadDirRecursive(path string, depth int, strictDepth bool)
215215

216216
dirEntries, err := os.ReadDir(path)
217217
if err != nil {
218-
return nil, err
218+
if errors.Is(err, os.ErrNotExist) {
219+
// We tried to read the directory, but it doesn't exist - return
220+
// empty result.
221+
return []ReadDirEntry{}, nil
222+
} else {
223+
return nil, err
224+
}
219225
}
220226

221227
entries := utils.Map(dirEntries, mapDirEntry(path))

0 commit comments

Comments
 (0)