diff --git a/syft/source/directory_resolver.go b/syft/source/directory_resolver.go index b735f744e..d3ffabc22 100644 --- a/syft/source/directory_resolver.go +++ b/syft/source/directory_resolver.go @@ -132,10 +132,15 @@ func (r *directoryResolver) indexTree(root string, stager *progress.Stage) ([]st } func (r *directoryResolver) indexPath(path string, info os.FileInfo, err error) (string, error) { + // link cycles could cause a revisit --we should not allow this + if r.hasBeenIndexed(path) { + return "", nil + } + // ignore any path which a filter function returns true for _, filterFn := range r.pathFilterFns { if filterFn != nil && filterFn(path, info) { - if info.IsDir() { + if info != nil && info.IsDir() { return "", fs.SkipDir } return "", nil @@ -146,11 +151,6 @@ func (r *directoryResolver) indexPath(path string, info os.FileInfo, err error) return "", nil } - // link cycles could cause a revisit --we should not allow this - if r.hasBeenIndexed(path) { - return "", nil - } - if info == nil { // walk may not be able to provide a FileInfo object, don't allow for this to stop indexing; keep track of the paths and continue. r.errPaths[path] = fmt.Errorf("no file info observable at path=%q", path) diff --git a/syft/source/directory_resolver_test.go b/syft/source/directory_resolver_test.go index 795ef0b79..f178414c5 100644 --- a/syft/source/directory_resolver_test.go +++ b/syft/source/directory_resolver_test.go @@ -865,3 +865,24 @@ func Test_IncludeRootPathInIndex(t *testing.T) { _, exists = resolver.metadata[ref.ID()] require.True(t, exists) } + +func TestDirectoryResolver_indexPath(t *testing.T) { + // TODO: Ideally we can use an OS abstraction, which would obviate the need for real FS setup. + tempFile, err := os.CreateTemp("", "") + require.NoError(t, err) + + resolver, err := newDirectoryResolver(tempFile.Name()) + require.NoError(t, err) + + t.Run("filtering path with nil os.FileInfo", func(t *testing.T) { + // We use one of these prefixes in order to trigger a pathFilterFn + filteredPath := unixSystemRuntimePrefixes[0] + + var fileInfo os.FileInfo = nil + + assert.NotPanics(t, func() { + _, err := resolver.indexPath(filteredPath, fileInfo, nil) + assert.NoError(t, err) + }) + }) +}