Clearer error in case of Android.bp being unreadable

Bug: 64600838
Test: mkdir errtest \
      && ln -s /tmp/dontexist errtest/Android.bp \
      # and add errtest to ./Android.bp \
      && m nothing \
      # and check that the error message mentions a symlink

Change-Id: I841ec12d613f61ccc3396538062bee48c8c1ca27
diff --git a/context.go b/context.go
index 9dd68b4..4b9ea72 100644
--- a/context.go
+++ b/context.go
@@ -19,6 +19,7 @@
 	"errors"
 	"fmt"
 	"io"
+	"os"
 	"path/filepath"
 	"reflect"
 	"runtime"
@@ -793,6 +794,23 @@
 
 	f, err := c.fs.Open(filename)
 	if err != nil {
+		// couldn't open the file; see if we can provide a clearer error than "could not open file"
+		stats, statErr := c.fs.Lstat(filename)
+		if statErr == nil {
+			isSymlink := stats.Mode()&os.ModeSymlink != 0
+			if isSymlink {
+				err = fmt.Errorf("could not open symlink %v : %v", filename, err)
+				target, readlinkErr := os.Readlink(filename)
+				if readlinkErr == nil {
+					_, targetStatsErr := c.fs.Lstat(target)
+					if targetStatsErr != nil {
+						err = fmt.Errorf("could not open symlink %v; its target (%v) cannot be opened", filename, target)
+					}
+				}
+			} else {
+				err = fmt.Errorf("%v exists but could not be opened: %v", filename, err)
+			}
+		}
 		errsCh <- []error{err}
 		return
 	}