Fix skyframe-native filesets to honor 'excludes' while in a directory traversal.

RELNOTES: None
PiperOrigin-RevId: 165465128
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/FilesetEntryFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/FilesetEntryFunction.java
index fe23f37..58dd224 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/FilesetEntryFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/FilesetEntryFunction.java
@@ -170,7 +170,9 @@
 
         // Check whether the symlink is excluded before attempting to resolve it.
         // It may be dangling, but excluding it is still fine.
-        if (exclusions.contains(linkName.getPathString())) {
+        // TODO(b/64754128): Investigate if we could have made the exclude earlier before
+        //                   unnecessarily iterating over all the files in an excluded directory.
+        if (linkName.segmentCount() > 0 && exclusions.contains(linkName.getSegment(0))) {
           continue;
         }
 
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/FilesetEntryFunctionTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/FilesetEntryFunctionTest.java
index 91b1a65..8c18bd0 100644
--- a/src/test/java/com/google/devtools/build/lib/skyframe/FilesetEntryFunctionTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skyframe/FilesetEntryFunctionTest.java
@@ -707,6 +707,43 @@
   }
 
   @Test
+  public void testExcludes() throws Exception {
+    Artifact buildFile = getSourceArtifact("foo/BUILD");
+    createFile(buildFile);
+    Artifact outerFile = getSourceArtifact("foo/outerfile.txt");
+    createFile(outerFile);
+    Artifact innerFile = getSourceArtifact("foo/dir/innerfile.txt");
+    createFile(innerFile);
+
+    FilesetTraversalParams params =
+        FilesetTraversalParamsFactory.recursiveTraversalOfPackage(
+            /* ownerLabel */ label("//foo"),
+            /* buildFile */ buildFile,
+            PathFragment.create("output-name"),
+            /* excludes */ ImmutableSet.of(),
+            /* symlinkBehaviorMode */ SymlinkBehavior.COPY,
+            /* pkgBoundaryMode */ PackageBoundaryMode.DONT_CROSS);
+    assertSymlinksInOrder(
+        params,
+        symlink("output-name/BUILD", buildFile),
+        symlink("output-name/outerfile.txt", outerFile),
+        symlink("output-name/dir/innerfile.txt", innerFile));
+
+    // Make sure the file within the excluded directory is no longer present.
+    params = FilesetTraversalParamsFactory.recursiveTraversalOfPackage(
+            /* ownerLabel */ label("//foo"),
+            /* buildFile */ buildFile,
+            PathFragment.create("output-name"),
+            /* excludes */ ImmutableSet.of("dir"),
+            /* symlinkBehaviorMode */ SymlinkBehavior.COPY,
+            /* pkgBoundaryMode */ PackageBoundaryMode.DONT_CROSS);
+    assertSymlinksInOrder(
+        params,
+        symlink("output-name/BUILD", buildFile),
+        symlink("output-name/outerfile.txt", outerFile));
+  }
+
+  @Test
   public void testFileTraversalForNonExistentFile() throws Exception {
     Artifact path = getSourceArtifact("foo/non-existent");
     FilesetTraversalParams params =