Provide clearer build event proto output for Starlark flags.

Context: https://docs.bazel.build/versions/master/build-event-protocol.html.

Starlark flags are both options and targets. Current BEP output
includes them in "target_configured" and similar messages, which are really
intended to track which of a build's requested targets succeed and fail.

Even though Starlark flags are technically loaded and processed as targets,
log and error reporting should treat them as options.

PiperOrigin-RevId: 346330399
diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkOptionsParsingTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkOptionsParsingTest.java
index 42ef9a8..aba35319 100644
--- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkOptionsParsingTest.java
+++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkOptionsParsingTest.java
@@ -18,12 +18,21 @@
 import static org.junit.Assert.assertThrows;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.devtools.build.lib.events.Event;
+import com.google.devtools.build.lib.events.ExtendedEventHandler;
+import com.google.devtools.build.lib.events.ExtendedEventHandler.Postable;
+import com.google.devtools.build.lib.pkgcache.TargetParsingCompleteEvent;
 import com.google.devtools.build.lib.runtime.StarlarkOptionsParser;
 import com.google.devtools.build.lib.starlark.util.StarlarkOptionsTestCase;
 import com.google.devtools.build.lib.util.Pair;
 import com.google.devtools.common.options.OptionsParsingException;
 import com.google.devtools.common.options.OptionsParsingResult;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
 import net.starlark.java.eval.StarlarkInt;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
@@ -32,6 +41,30 @@
 @RunWith(JUnit4.class)
 public class StarlarkOptionsParsingTest extends StarlarkOptionsTestCase {
 
+  private List<Postable> postedEvents;
+
+  @Before
+  public void addPostableEventHandler() {
+    postedEvents = new ArrayList<>();
+    reporter.addHandler(
+        new ExtendedEventHandler() {
+          @Override
+          public void post(Postable obj) {
+            postedEvents.add(obj);
+          }
+
+          @Override
+          public void handle(Event event) {}
+        });
+  }
+
+  /** Returns only the posted events of the given class. */
+  private List<Postable> eventsOfType(Class<? extends Postable> clazz) {
+    return postedEvents.stream()
+        .filter(event -> event.getClass().equals(clazz))
+        .collect(Collectors.toList());
+  }
+
   // test --flag=value
   @Test
   public void testFlagEqualsValueForm() throws Exception {
@@ -331,4 +364,51 @@
     assertThat(residueAndStarlarkOptions.getSecond())
         .containsExactly("some-random-residue", "--mangled//external/starlark/option");
   }
+
+  /**
+   * When Starlark flags are only set as flags, they shouldn't produce {@link
+   * TargetParsingCompleteEvent}s. That's intended to communicate (to the build event protocol)
+   * which of the targets in {@code blaze build //foo:all //bar:all} were built.
+   */
+  @Test
+  public void testExpectedBuildEventOutput_asFlag() throws Exception {
+    writeBasicIntFlag();
+    scratch.file("blah/BUILD", "cc_library(name = 'mylib')");
+    useConfiguration(ImmutableMap.of("//test:my_int_setting", "15"));
+    update(
+        ImmutableList.of("//blah:mylib"),
+        /*keepGoing=*/ false,
+        /*loadingPhaseThreads=*/ LOADING_PHASE_THREADS,
+        /*doAnalysis*/ true,
+        eventBus);
+    List<Postable> targetParsingCompleteEvents = eventsOfType(TargetParsingCompleteEvent.class);
+    assertThat(targetParsingCompleteEvents).hasSize(1);
+    assertThat(
+            ((TargetParsingCompleteEvent) targetParsingCompleteEvents.get(0))
+                .getOriginalTargetPattern())
+        .containsExactly("//blah:mylib");
+  }
+
+  /**
+   * But Starlark are also targets. When they're requested as normal build targets they should
+   * produce {@link TargetParsingCompleteEvent} just like any other target.
+   */
+  @Test
+  public void testExpectedBuildEventOutput_asTarget() throws Exception {
+    writeBasicIntFlag();
+    scratch.file("blah/BUILD", "cc_library(name = 'mylib')");
+    useConfiguration(ImmutableMap.of("//test:my_int_setting", "15"));
+    update(
+        ImmutableList.of("//blah:mylib", "//test:my_int_setting"),
+        /*keepGoing=*/ false,
+        /*loadingPhaseThreads=*/ LOADING_PHASE_THREADS,
+        /*doAnalysis*/ true,
+        eventBus);
+    List<Postable> targetParsingCompleteEvents = eventsOfType(TargetParsingCompleteEvent.class);
+    assertThat(targetParsingCompleteEvents).hasSize(1);
+    assertThat(
+            ((TargetParsingCompleteEvent) targetParsingCompleteEvents.get(0))
+                .getOriginalTargetPattern())
+        .containsExactly("//blah:mylib", "//test:my_int_setting");
+  }
 }