diff --git a/syft/internal/fileresolver/unindexed_directory.go b/syft/internal/fileresolver/unindexed_directory.go index c89bf24ffc2..46776e0284e 100644 --- a/syft/internal/fileresolver/unindexed_directory.go +++ b/syft/internal/fileresolver/unindexed_directory.go @@ -265,7 +265,7 @@ func (u UnindexedDirectory) FilesByMIMEType(types ...string) ([]syftFile.Locatio // RelativeFileByPath fetches a single file at the given path relative to the layer squash of the given reference. // This is helpful when attempting to find a file that is in the same layer or lower as another file. func (u UnindexedDirectory) RelativeFileByPath(l syftFile.Location, p string) *syftFile.Location { - p = path.Clean(path.Join(l.RealPath, p)) + p = path.Clean(p) locs, err := u.filesByPath(true, false, p) if err != nil || len(locs) == 0 { return nil diff --git a/syft/internal/fileresolver/unindexed_directory_test.go b/syft/internal/fileresolver/unindexed_directory_test.go index 4300c1030f2..ec81e1e7876 100644 --- a/syft/internal/fileresolver/unindexed_directory_test.go +++ b/syft/internal/fileresolver/unindexed_directory_test.go @@ -1296,6 +1296,30 @@ func Test_UnindexedDirectoryResolver_FilesByMIMEType(t *testing.T) { } } +func Test_UnindexedDirectoryResolver_RelativeFileByPath(t *testing.T) { + cases := []struct { + name string + root string + searchFile string + expected *strset.Set + }{ + { + name: "should find nested file from root", + root: "./test-fixtures/image-simple", + searchFile: "target/really/nested/file-3.txt", + expected: strset.New("file-3.txt"), + }, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + resolver := NewFromUnindexedDirectory(c.root) + rootLoc := file.NewLocation(c.root) + loc := resolver.RelativeFileByPath(rootLoc, c.searchFile) + assert.True(t, c.expected.Has(loc.Coordinates.RealPath), "does not have path %q", loc.RealPath) + }) + } +} + func testWithTimeout(t *testing.T, timeout time.Duration, test func(*testing.T)) { done := make(chan bool) go func() {