diff --git a/syft/source/directory_resolver.go b/syft/source/directory_resolver.go index 5f1970c2a..d31994d70 100644 --- a/syft/source/directory_resolver.go +++ b/syft/source/directory_resolver.go @@ -330,12 +330,19 @@ func (r directoryResolver) responsePath(path string) string { path = posixToWindows(path) } - // always return references relative to the request path (not absolute path) + // clean references to the request path (either the root, or the base if set) if filepath.IsAbs(path) { - // we need to account for the cwd relative to the running process and the given root for the directory resolver - prefix := filepath.Clean(filepath.Join(r.currentWd, r.currentWdRelativeToRoot)) - return strings.TrimPrefix(path, prefix+string(filepath.Separator)) + var prefix string + if r.base != "" { + prefix = r.base + } else { + // we need to account for the cwd relative to the running process and the given root for the directory resolver + prefix = filepath.Clean(filepath.Join(r.currentWd, r.currentWdRelativeToRoot)) + prefix += string(filepath.Separator) + } + path = strings.TrimPrefix(path, prefix) } + return path } diff --git a/syft/source/directory_resolver_test.go b/syft/source/directory_resolver_test.go index 7305c225b..20ed1e373 100644 --- a/syft/source/directory_resolver_test.go +++ b/syft/source/directory_resolver_test.go @@ -898,7 +898,7 @@ func TestDirectoryResolver_FilesByPath_baseRoot(t *testing.T) { root: "./test-fixtures/symlinks-base/", input: "./base", expected: []string{ - "base", + "/base", }, }, { @@ -906,7 +906,7 @@ func TestDirectoryResolver_FilesByPath_baseRoot(t *testing.T) { root: "./test-fixtures/symlinks-base/", input: "./foo", expected: []string{ - "base", + "/base", }, }, { @@ -914,7 +914,7 @@ func TestDirectoryResolver_FilesByPath_baseRoot(t *testing.T) { root: "./test-fixtures/symlinks-base/", input: "./bar", expected: []string{ - "base", + "/base", }, }, { @@ -922,7 +922,7 @@ func TestDirectoryResolver_FilesByPath_baseRoot(t *testing.T) { root: "./test-fixtures/symlinks-base/", input: "./baz", expected: []string{ - "base", + "/base", }, }, { @@ -930,7 +930,7 @@ func TestDirectoryResolver_FilesByPath_baseRoot(t *testing.T) { root: "./test-fixtures/symlinks-base/", input: "./sub/link", expected: []string{ - "sub/item", + "/sub/item", }, }, { @@ -938,7 +938,7 @@ func TestDirectoryResolver_FilesByPath_baseRoot(t *testing.T) { root: "./test-fixtures/symlinks-base/", input: "./chain", expected: []string{ - "base", + "/base", }, }, }