Stop blaze crash due to a symlink cycle or unbounded expansion encountered
while traversing filesets.

RELNOTES: None
PiperOrigin-RevId: 166913262
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/RecursiveFilesystemTraversalFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/RecursiveFilesystemTraversalFunction.java
index e122307..50ae176 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/RecursiveFilesystemTraversalFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/RecursiveFilesystemTraversalFunction.java
@@ -31,6 +31,7 @@
 import com.google.devtools.build.skyframe.SkyFunctionException;
 import com.google.devtools.build.skyframe.SkyKey;
 import com.google.devtools.build.skyframe.SkyValue;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -97,6 +98,13 @@
     }
   }
 
+  /** Thrown when we encounter errors from underlying File operations */
+  public static final class FileOperationException extends RecursiveFilesystemTraversalException {
+    public FileOperationException(String message) {
+      super(message);
+    }
+  }
+
   /** Exception type thrown by {@link RecursiveFilesystemTraversalFunction#compute}. */
   private static final class RecursiveFilesystemTraversalFunctionException extends
       SkyFunctionException {
@@ -166,6 +174,9 @@
       // We are free to traverse this directory.
       Collection<SkyKey> dependentKeys = createRecursiveTraversalKeys(env, traversal);
       return resultForDirectory(traversal, rootInfo, traverseChildren(env, dependentKeys));
+    } catch (FileSymlinkException | InconsistentFilesystemException | IOException e) {
+      throw new RecursiveFilesystemTraversalFunctionException(
+          new FileOperationException("Error while traversing fileset: " + e.getMessage()));
     } catch (MissingDepException e) {
       return null;
     }
@@ -202,9 +213,20 @@
   }
 
   private static FileInfo lookUpFileInfo(Environment env, TraversalRequest traversal)
-      throws MissingDepException, InterruptedException {
+      throws MissingDepException, FileSymlinkException, InconsistentFilesystemException,
+          IOException, InterruptedException {
     // Stat the file.
-    FileValue fileValue = (FileValue) getDependentSkyValue(env, FileValue.key(traversal.path));
+    FileValue fileValue =
+        (FileValue)
+            env.getValueOrThrow(
+                FileValue.key(traversal.path),
+                FileSymlinkException.class,
+                InconsistentFilesystemException.class,
+                IOException.class);
+
+    if (env.valuesMissing()) {
+      throw new MissingDepException();
+    }
     if (fileValue.exists()) {
       // If it exists, it may either be a symlink or a file/directory.
       PathFragment unresolvedLinkTarget = null;
@@ -279,7 +301,8 @@
    */
   private static PkgLookupResult checkIfPackage(
       Environment env, TraversalRequest traversal, FileInfo rootInfo)
-      throws MissingDepException, InterruptedException {
+      throws MissingDepException, FileSymlinkException, InconsistentFilesystemException,
+          IOException, InterruptedException {
     Preconditions.checkArgument(rootInfo.type.exists() && !rootInfo.type.isFile(),
         "{%s} {%s}", traversal, rootInfo);
     PackageLookupValue pkgLookup = (PackageLookupValue) getDependentSkyValue(env,