Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: decode bazel event from bep #50

Merged
merged 6 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bazel/buildeventstream/proto/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:exclude dummy.go
3 changes: 3 additions & 0 deletions bazel/buildeventstream/proto/dummy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file exists to make the go tooling happy. This package is generated by
// bazel in //third-party/github.com/bazelbuild/bazel/...
package proto
2 changes: 1 addition & 1 deletion pkg/aspect/build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ go_library(
"//pkg/bazel",
"//pkg/hooks",
"//pkg/ioutils",
"//third-party/github.com/bazelbuild/bazel/src/main/java/com/google/devtools/build/lib/buildeventstream/proto",
"@com_github_spf13_cobra//:cobra",
"@go_googleapis//google/devtools/build/v1:build_go_proto",
],
)

Expand Down
3 changes: 3 additions & 0 deletions pkg/aspect/build/bep/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
deps = [
"//pkg/aspecterrors",
"//pkg/aspectgrpc",
"//third-party/github.com/bazelbuild/bazel/src/main/java/com/google/devtools/build/lib/buildeventstream/proto",
"@go_googleapis//google/devtools/build/v1:build_go_proto",
"@io_bazel_rules_go//proto/wkt:empty_go_proto",
"@org_golang_google_grpc//:go_default_library",
Expand All @@ -22,8 +23,10 @@ go_test(
"//pkg/aspecterrors",
"//pkg/aspectgrpc/mock",
"//pkg/stdlib/mock",
"//third-party/github.com/bazelbuild/bazel/src/main/java/com/google/devtools/build/lib/buildeventstream/proto",
"@com_github_golang_mock//gomock",
"@com_github_onsi_gomega//:gomega",
"@go_googleapis//google/devtools/build/v1:build_go_proto",
"@org_golang_google_protobuf//types/known/anypb",
],
)
21 changes: 15 additions & 6 deletions pkg/aspect/build/bep/bes_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
buildv1 "google.golang.org/genproto/googleapis/devtools/build/v1"
"google.golang.org/grpc"

buildeventstream "aspect.build/cli/bazel/buildeventstream/proto"
"aspect.build/cli/pkg/aspecterrors"
"aspect.build/cli/pkg/aspectgrpc"
)
Expand Down Expand Up @@ -118,7 +119,7 @@ func (bb *besBackend) Errors() []error {

// CallbackFn is the signature for the callback function used by the subscribers
// of the Build Event Protocol events.
type CallbackFn func(*buildv1.BuildEvent) error
type CallbackFn func(*buildeventstream.BuildEvent) error

// RegisterSubscriber registers a new subscriber callback function to the
// Build Event Protocol events.
Expand Down Expand Up @@ -149,12 +150,20 @@ func (bb *besBackend) PublishBuildToolEventStream(
}
event := req.OrderedBuildEvent.Event
if event != nil {
s := bb.subscribers.head
for s != nil {
if err := s.callback(event); err != nil {
bb.errors.Insert(err)
bazelEvent := event.GetBazelEvent()
if bazelEvent != nil {
var buildEvent buildeventstream.BuildEvent
if err := bazelEvent.UnmarshalTo(&buildEvent); err != nil {
return err
}

s := bb.subscribers.head
for s != nil {
if err := s.callback(&buildEvent); err != nil {
bb.errors.Insert(err)
}
s = s.next
}
s = s.next
}
}
res := &buildv1.PublishBuildToolEventStreamResponse{
Expand Down
19 changes: 12 additions & 7 deletions pkg/aspect/build/bep/bes_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
"github.com/golang/mock/gomock"
. "github.com/onsi/gomega"
buildv1 "google.golang.org/genproto/googleapis/devtools/build/v1"
"google.golang.org/protobuf/types/known/anypb"

buildeventstream "aspect.build/cli/bazel/buildeventstream/proto"
"aspect.build/cli/pkg/aspecterrors"
grpc_mock "aspect.build/cli/pkg/aspectgrpc/mock"
stdlib_mock "aspect.build/cli/pkg/stdlib/mock"
Expand Down Expand Up @@ -310,7 +312,10 @@ func TestPublishBuildToolEventStream(t *testing.T) {
defer ctrl.Finish()

eventStream := grpc_mock.NewMockPublishBuildEvent_PublishBuildToolEventStreamServer(ctrl)
event := &buildv1.BuildEvent{}
buildEvent := &buildeventstream.BuildEvent{}
var anyBuildEvent anypb.Any
anyBuildEvent.MarshalFrom(buildEvent)
event := &buildv1.BuildEvent{Event: &buildv1.BuildEvent_BazelEvent{BazelEvent: &anyBuildEvent}}
streamId := &buildv1.StreamId{BuildId: "1"}
orderedBuildEvent := &buildv1.OrderedBuildEvent{
StreamId: streamId,
Expand Down Expand Up @@ -345,20 +350,20 @@ func TestPublishBuildToolEventStream(t *testing.T) {
errors: &aspecterrors.ErrorList{},
}
var calledSubscriber1, calledSubscriber2, calledSubscriber3 bool
besBackend.RegisterSubscriber(func(evt *buildv1.BuildEvent) error {
g.Expect(evt).To(Equal(event))
besBackend.RegisterSubscriber(func(evt *buildeventstream.BuildEvent) error {
g.Expect(evt).To(Equal(buildEvent))
calledSubscriber1 = true
return nil
})
expectedSubscriber2Err := fmt.Errorf("error from subscriber 2")
besBackend.RegisterSubscriber(func(evt *buildv1.BuildEvent) error {
g.Expect(evt).To(Equal(event))
besBackend.RegisterSubscriber(func(evt *buildeventstream.BuildEvent) error {
g.Expect(evt).To(Equal(buildEvent))
calledSubscriber2 = true
return expectedSubscriber2Err
})
expectedSubscriber3Err := fmt.Errorf("error from subscriber 3")
besBackend.RegisterSubscriber(func(evt *buildv1.BuildEvent) error {
g.Expect(evt).To(Equal(event))
besBackend.RegisterSubscriber(func(evt *buildeventstream.BuildEvent) error {
g.Expect(evt).To(Equal(buildEvent))
calledSubscriber3 = true
return expectedSubscriber3Err
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/aspect/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"time"

"github.com/spf13/cobra"
buildv1 "google.golang.org/genproto/googleapis/devtools/build/v1"

buildeventstream "aspect.build/cli/bazel/buildeventstream/proto"
"aspect.build/cli/pkg/aspect/build/bep"
"aspect.build/cli/pkg/aspecterrors"
"aspect.build/cli/pkg/bazel"
Expand Down Expand Up @@ -100,7 +100,7 @@ func (b *Build) Run(ctx context.Context, cmd *cobra.Command, args []string) (exi
type Plugin interface {
// BEPEventsSubscriber is used to verify whether an Aspect plugin registers
// itself to receive the Build Event Protocol events.
BEPEventCallback(event *buildv1.BuildEvent) error
BEPEventCallback(event *buildeventstream.BuildEvent) error
// TODO(f0rmiga): test the build hooks after implementing the plugin system.
PostBuildHook() error
}
2 changes: 1 addition & 1 deletion pkg/plugins/fix_visibility/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ go_library(
importpath = "aspect.build/cli/pkg/plugins/fix_visibility",
visibility = ["//cmd/aspect/build:__pkg__"],
deps = [
"//third-party/github.com/bazelbuild/bazel/src/main/java/com/google/devtools/build/lib/buildeventstream/proto",
"@bazel_gazelle//label:go_default_library",
"@com_github_bazelbuild_buildtools//edit:go_default_library",
"@com_github_manifoldco_promptui//:promptui",
"@com_github_mattn_go_isatty//:go-isatty",
"@go_googleapis//google/devtools/build/v1:build_go_proto",
],
)
24 changes: 12 additions & 12 deletions pkg/plugins/fix_visibility/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
"github.com/bazelbuild/buildtools/edit"
"github.com/manifoldco/promptui"
isatty "github.com/mattn/go-isatty"
buildv1 "google.golang.org/genproto/googleapis/devtools/build/v1"

buildeventstream "aspect.build/cli/bazel/buildeventstream/proto"
)

type FixVisibilityPlugin struct {
Expand Down Expand Up @@ -54,19 +55,18 @@ func NewPlugin(
}

var visibilityIssueRegex = regexp.MustCompile(`.*target '(.*)' is not visible from target '(.*)'.*`)
var visibilityIssueSubstring = []byte("is not visible from target")

func (plugin *FixVisibilityPlugin) BEPEventCallback(event *buildv1.BuildEvent) error {
bazelEvent := event.GetBazelEvent()
if bazelEvent != nil {
if !bytes.Contains(bazelEvent.Value, visibilityIssueSubstring) {
return nil
}
matches := visibilityIssueRegex.FindSubmatch(bazelEvent.Value)
if len(matches) != 3 {
return nil
const visibilityIssueSubstring = "is not visible from target"

func (plugin *FixVisibilityPlugin) BEPEventCallback(event *buildeventstream.BuildEvent) error {
aborted := event.GetAborted()
if aborted != nil &&
aborted.Reason == buildeventstream.Aborted_ANALYSIS_FAILURE &&
strings.Contains(aborted.Description, visibilityIssueSubstring) {
matches := visibilityIssueRegex.FindStringSubmatch(aborted.Description)
if len(matches) == 3 {
plugin.targetsToFix.insert(matches[1], matches[2])
}
plugin.targetsToFix.insert(string(matches[1]), string(matches[2]))
}
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions third-party/github.com/bazelbuild/bazel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# bazel

This is a partial clone of the `github.com/bazelbuild/bazel` repository. The
vendored files here avoid a full clone of the original repository, which is big.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

proto_library(
name = "proto_proto",
srcs = ["build_event_stream.proto"],
visibility = ["//visibility:public"],
deps = [
"//third-party/github.com/bazelbuild/bazel/src/main/protobuf/command_line:command_line_proto",
"//third-party/github.com/bazelbuild/bazel/src/main/protobuf/failure_details:failure_details_proto",
"//third-party/github.com/bazelbuild/bazel/src/main/protobuf/invocation_policy:invocation_policy_proto",
],
)

go_proto_library(
name = "proto_go_proto",
importpath = "aspect.build/cli/bazel/buildeventstream/proto",
proto = ":proto_proto",
visibility = ["//visibility:public"],
deps = [
"//third-party/github.com/bazelbuild/bazel/src/main/protobuf/command_line",
"//third-party/github.com/bazelbuild/bazel/src/main/protobuf/failure_details",
"//third-party/github.com/bazelbuild/bazel/src/main/protobuf/invocation_policy",
],
)

go_library(
name = "proto",
embed = [":proto_go_proto"],
importpath = "aspect.build/cli/bazel/buildeventstream/proto",
visibility = ["//visibility:public"],
)
Loading