From 0db2960b4dba3b5710734deb69966ca3b848c0c2 Mon Sep 17 00:00:00 2001
From: "DD (Devdatta) Deshpande" <dd@codewits.in>
Date: Thu, 6 Jul 2023 00:19:22 +0530
Subject: [PATCH] fix: use filepath.EvalSymlinks if os.Readlink fails to
 evaluate the link (#1884)

Signed-off-by: DD (Devdatta) Deshpande <dd@codewits.in>
Co-authored-by: Keith Zantow <kzantow@gmail.com>
---
 syft/internal/fileresolver/directory_indexer.go | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/syft/internal/fileresolver/directory_indexer.go b/syft/internal/fileresolver/directory_indexer.go
index 47349a4456b..01f332a3de6 100644
--- a/syft/internal/fileresolver/directory_indexer.go
+++ b/syft/internal/fileresolver/directory_indexer.go
@@ -332,7 +332,19 @@ func (r directoryIndexer) addFileToIndex(p string, info os.FileInfo) error {
 func (r directoryIndexer) addSymlinkToIndex(p string, info os.FileInfo) (string, error) {
 	linkTarget, err := os.Readlink(p)
 	if err != nil {
-		return "", fmt.Errorf("unable to readlink for path=%q: %w", p, err)
+		if runtime.GOOS == WindowsOS {
+			p = posixToWindows(p)
+		}
+
+		linkTarget, err = filepath.EvalSymlinks(p)
+
+		if runtime.GOOS == WindowsOS {
+			p = windowsToPosix(p)
+		}
+
+		if err != nil {
+			return "", fmt.Errorf("unable to readlink for path=%q: %w", p, err)
+		}
 	}
 
 	if filepath.IsAbs(linkTarget) {