diff options
3 files changed, 36 insertions, 3 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/buildeventservice/BazelBuildEventServiceModule.java b/src/main/java/com/google/devtools/build/lib/buildeventservice/BazelBuildEventServiceModule.java index c80f77e58e..0ba8d605eb 100644 --- a/src/main/java/com/google/devtools/build/lib/buildeventservice/BazelBuildEventServiceModule.java +++ b/src/main/java/com/google/devtools/build/lib/buildeventservice/BazelBuildEventServiceModule.java @@ -14,9 +14,11 @@ package com.google.devtools.build.lib.buildeventservice; +import com.google.common.collect.ImmutableSet; import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions; import com.google.devtools.build.lib.buildeventservice.client.BuildEventServiceClient; import com.google.devtools.build.lib.buildeventservice.client.BuildEventServiceGrpcClient; +import java.util.Set; /** * Bazel's BES module. @@ -37,4 +39,9 @@ public class BazelBuildEventServiceModule authAndTLSOptions.tlsAuthorityOverride, authAndTLSOptions.authCredentials, authAndTLSOptions.authScope); } + + @Override + protected Set<String> whitelistedCommands() { + return ImmutableSet.of("build", "test", "run", "query", "coverage", "mobile-install"); + } } diff --git a/src/main/java/com/google/devtools/build/lib/buildeventservice/BuildEventServiceModule.java b/src/main/java/com/google/devtools/build/lib/buildeventservice/BuildEventServiceModule.java index ad33471212..60df1913a4 100644 --- a/src/main/java/com/google/devtools/build/lib/buildeventservice/BuildEventServiceModule.java +++ b/src/main/java/com/google/devtools/build/lib/buildeventservice/BuildEventServiceModule.java @@ -47,6 +47,7 @@ import com.google.devtools.common.options.OptionsBase; import com.google.devtools.common.options.OptionsProvider; import java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.UUID; import java.util.logging.Logger; import javax.annotation.Nullable; @@ -66,6 +67,7 @@ public abstract class BuildEventServiceModule<T extends BuildEventServiceOptions private CommandEnvironment commandEnvironment; private SynchronizedOutputStream out; private SynchronizedOutputStream err; + private boolean disabled; private static class BuildEventRecorder { private final List<BuildEvent> events = new ArrayList<>(); @@ -90,6 +92,11 @@ public abstract class BuildEventServiceModule<T extends BuildEventServiceOptions @Override public void beforeCommand(CommandEnvironment commandEnvironment) throws AbruptExitException { + disabled = false; + if (!whitelistedCommands().contains(commandEnvironment.getCommandName())) { + disabled = true; + return; + } this.commandEnvironment = commandEnvironment; this.buildEventRecorder = new BuildEventRecorder(); commandEnvironment.getEventBus().register(buildEventRecorder); @@ -97,6 +104,9 @@ public abstract class BuildEventServiceModule<T extends BuildEventServiceOptions @Override public void handleOptions(OptionsProvider optionsProvider) { + if (disabled) { + return; + } checkState(commandEnvironment != null, "Methods called out of order"); BuildEventStreamer streamer = tryCreateStreamer( @@ -158,6 +168,9 @@ public abstract class BuildEventServiceModule<T extends BuildEventServiceOptions @Override public OutErr getOutputListener() { + if (disabled) { + return null; + } this.out = new SynchronizedOutputStream(); this.err = new SynchronizedOutputStream(); return OutErr.create(this.out, this.err); @@ -266,4 +279,6 @@ public abstract class BuildEventServiceModule<T extends BuildEventServiceOptions protected abstract BuildEventServiceClient createBesClient(T besOptions, AuthAndTLSOptions authAndTLSOptions); + + protected abstract Set<String> whitelistedCommands(); } diff --git a/src/test/shell/integration/build_event_stream_test.sh b/src/test/shell/integration/build_event_stream_test.sh index c1577a38c6..5782ec076c 100755 --- a/src/test/shell/integration/build_event_stream_test.sh +++ b/src/test/shell/integration/build_event_stream_test.sh @@ -379,9 +379,6 @@ function test_build_only() { function test_query() { # Verify that at least a minimally meaningful event stream is generated # for non-build. In particular, we expect bazel not to crash. - bazel version --build_event_text_file=$TEST_log \ - || fail "bazel version failed" - expect_log '^started' bazel query --build_event_text_file=$TEST_log 'tests(//...)' \ || fail "bazel query failed" expect_log '^started' @@ -397,6 +394,20 @@ function test_query() { expect_log 'name: "SUCCESS"' } +function test_command_whitelisting() { + # We expect the "help" command to not generate a build-event stream, + # but the "build" command to do. + rm -f bep.txt + bazel help --build_event_text_file=bep.txt || fail "bazel help failed" + ( [ -f bep.txt ] && fail "bazel help generated a build-event file" ) || : + bazel version --build_event_text_file=bep.txt || fail "bazel help failed" + ( [ -f bep.txt ] && fail "bazel version generated a build-event file" ) || : + bazel build --build_event_text_file=bep.txt //pkg:true \ + || fail "bazel build failed" + [ -f bep.txt ] || fail "build did not generate requested build-event file" + rm -f bep.txt +} + function test_multiple_transports() { # Verifies usage of multiple build event transports at the same time outdir=$(mktemp -d ${TEST_TMPDIR}/bazel.XXXXXXXX) |