Skip to content

Commit

Permalink
feat: fix-visibility
Browse files Browse the repository at this point in the history
Signed-off-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>
  • Loading branch information
f0rmiga committed Sep 25, 2021
1 parent 0b2d2a3 commit 908dc2e
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 22 deletions.
3 changes: 2 additions & 1 deletion cmd/aspect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Not licensed for re-use.
package main

import (
"context"
"errors"
"fmt"
"os"
Expand All @@ -28,7 +29,7 @@ func main() {
// - tools/bazel file and put our bootstrap code in there
//
cmd := root.NewDefaultRootCmd()
if err := cmd.Execute(); err != nil {
if err := cmd.ExecuteContext(context.Background()); err != nil {
var exitErr *aspecterrors.ExitError
if errors.As(err, &exitErr) {
if exitErr.Err != nil {
Expand Down
25 changes: 23 additions & 2 deletions go.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,27 @@ def deps():
sum = "h1:BUAU3CGlLvorLI26FmByPp2eC2qla6E1Tw+scpcg/to=",
version = "v0.0.0-20180808171621-7fddfc383310",
)
go_repository(
name = "com_github_bazelbuild_bazel_gazelle",
importpath = "github.com/bazelbuild/bazel-gazelle",
sum = "h1:Ks6YN+WkOv2lYWlvf7ksxUpLvrDbBHPBXXUrBFQ3BZM=",
version = "v0.23.0",
)

go_repository(
name = "com_github_bazelbuild_bazelisk",
build_naming_convention = "go_default_library",
importpath = "github.com/bazelbuild/bazelisk",
sum = "h1:2EWA2lRrt/k8B5ASt0mlTDQ+7mnzvKdF6ShNWLbk0o0=",
version = "v1.10.1",
)
go_repository(
name = "com_github_bazelbuild_buildtools",
importpath = "github.com/bazelbuild/buildtools",
sum = "h1:Et1IIXrXwhpDvR5wH9REPEZ0sUtzUoJSq19nfmBqzBY=",
version = "v0.0.0-20200718160251-b1667ff58f71",
)

go_repository(
name = "com_github_bazelbuild_rules_go",
importpath = "github.com/bazelbuild/rules_go",
Expand All @@ -71,6 +85,13 @@ def deps():
sum = "h1:+0HFd5KSZ/mm3JmhmrDukiId5iR6w4+BdFtfSy4yWIc=",
version = "v0.0.3-0.20200106085610-5cbc8cc4026c",
)
go_repository(
name = "com_github_bmatcuk_doublestar",
importpath = "github.com/bmatcuk/doublestar",
sum = "h1:oC24CykoSAB8zd7XgruHo33E0cHJf/WhQA/7BeXj+x0=",
version = "v1.2.2",
)

go_repository(
name = "com_github_burntsushi_toml",
importpath = "github.com/BurntSushi/toml",
Expand Down Expand Up @@ -1012,8 +1033,8 @@ def deps():
go_repository(
name = "org_golang_x_mod",
importpath = "golang.org/x/mod",
sum = "h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=",
version = "v0.3.0",
sum = "h1:Kvvh58BN8Y9/lBi7hTekvtMpm07eUZ0ck5pRHpsMWrY=",
version = "v0.4.1",
)
go_repository(
name = "org_golang_x_net",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module aspect.build/cli
go 1.16

require (
github.com/bazelbuild/bazel-gazelle v0.23.0
github.com/bazelbuild/bazelisk v1.10.1
github.com/bazelbuild/rules_go v0.28.0
github.com/fatih/color v1.12.0
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/bazelbuild/bazel-gazelle v0.23.0 h1:Ks6YN+WkOv2lYWlvf7ksxUpLvrDbBHPBXXUrBFQ3BZM=
github.com/bazelbuild/bazel-gazelle v0.23.0/go.mod h1:3mHi4TYn0QxwdMKPJfj3FKhZxYgWm46DjWQQPOg20BY=
github.com/bazelbuild/bazelisk v1.10.1 h1:2EWA2lRrt/k8B5ASt0mlTDQ+7mnzvKdF6ShNWLbk0o0=
github.com/bazelbuild/bazelisk v1.10.1/go.mod h1:s3ZIQZj3l9iCk/03rBgjhYvqz0c5SPRvoQCZuz8Lw/4=
github.com/bazelbuild/buildtools v0.0.0-20200718160251-b1667ff58f71/go.mod h1:5JP0TXzWDHXv8qvxRC4InIazwdyDseBDbzESUMKk1yU=
github.com/bazelbuild/rules_go v0.0.0-20190719190356-6dae44dc5cab/go.mod h1:MC23Dc/wkXEyk3Wpq6lCqz0ZAYOZDw2DR5y3N1q2i7M=
github.com/bazelbuild/rules_go v0.28.0 h1:fNtx0dJpG5ENGdMj3/GICoi/7z+ixB3IIW5rERTzOgM=
github.com/bazelbuild/rules_go v0.28.0/go.mod h1:MC23Dc/wkXEyk3Wpq6lCqz0ZAYOZDw2DR5y3N1q2i7M=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/bmatcuk/doublestar v1.2.2/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down Expand Up @@ -85,6 +90,7 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
Expand Down Expand Up @@ -278,6 +284,7 @@ golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCc
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -306,6 +313,7 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
3 changes: 3 additions & 0 deletions pkg/aspect/build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ go_library(
"//pkg/aspect/build/bep",
"//pkg/aspecterrors",
"//pkg/bazel",
"//pkg/hooks",
"//pkg/ioutils",
"//pkg/plugins/fix_visibility",
"@com_github_spf13_cobra//:cobra",
"@go_googleapis//google/devtools/build/v1:build_go_proto",
],
)

Expand Down
7 changes: 0 additions & 7 deletions pkg/aspect/build/bep/bes_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,3 @@ type subscriberNode struct {
next *subscriberNode
callback CallbackFn
}

// Plugin is the interface that wraps the BEPEventsSubscriber used to verify
// whether an Aspect plugin registers itself to receive the Build Event Protocol
// events.
type Plugin interface {
BEPEventsSubscriber() CallbackFn
}
63 changes: 51 additions & 12 deletions pkg/aspect/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@ package build

import (
"context"
"errors"
"fmt"
"time"

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

"aspect.build/cli/pkg/aspect/build/bep"
"aspect.build/cli/pkg/aspecterrors"
"aspect.build/cli/pkg/bazel"
"aspect.build/cli/pkg/hooks"
"aspect.build/cli/pkg/ioutils"
"aspect.build/cli/pkg/plugins/fix_visibility"
)

// Build represents the aspect build command.
type Build struct {
ioutils.Streams
bzl bazel.Spawner
besBackend bep.BESBackend
hooks *hooks.Hooks
}

// New creates a Build command.
Expand All @@ -36,14 +41,37 @@ func New(
Streams: streams,
bzl: bzl,
besBackend: besBackend,
hooks: hooks.New(),
}
}

// Run runs the aspect build command, calling `bazel build` with a local Build
// Event Protocol backend used by Aspect plugins to subscribe to build events.
func (b *Build) Run(_ *cobra.Command, args []string) error {
// TODO: register the BEP subscribers here with:
// besBackend.RegisterSubscriber(plugin.BEPEventsSubscriber())
func (b *Build) Run(cmd *cobra.Command, args []string) (exitErr error) {
ctx := cmd.Context()

var fixVisibilityPlugin Plugin = fix_visibility.NewDefaultPlugin()

// TODO(f0rmiga): this is a hook for the build command and should be discussed
// as part of the plugin design.
defer func(ctx context.Context) {
errs := b.hooks.ExecutePostBuild(ctx).Errors()
if len(errs) > 0 {
for _, err := range errs {
fmt.Fprintf(b.Streams.Stderr, "Error: failed to run build command: %v\n", err)
}
var err *aspecterrors.ExitError
if errors.As(exitErr, &err) {
err.ExitCode = 1
}
}
}(ctx)

// TODO(f0rmiga): this should also be part of the plugin design, as
// registering BEP event subscribers should not be hardcoded here.
b.besBackend.RegisterSubscriber(fixVisibilityPlugin.BEPEventCallback)
b.hooks.RegisterPostBuild(fixVisibilityPlugin.PostBuildHook)

if err := b.besBackend.Setup(); err != nil {
return fmt.Errorf("failed to run build command: %w", err)
}
Expand All @@ -55,22 +83,33 @@ func (b *Build) Run(_ *cobra.Command, args []string) error {
defer b.besBackend.GracefulStop()

besBackendFlag := fmt.Sprintf("--bes_backend=grpc://%s", b.besBackend.Addr())
cmd := append([]string{"build", besBackendFlag}, args...)
if exitCode, err := b.bzl.Spawn(cmd); exitCode != 0 {
err = &aspecterrors.ExitError{
Err: err, // err can be nil, so don't wrap it with the full context.
ExitCode: exitCode,
}
return err
}
exitCode, bazelErr := b.bzl.Spawn(append([]string{"build", besBackendFlag}, args...))

// Process the subscribers errors before the Bazel one.
subscriberErrors := b.besBackend.Errors()
if len(subscriberErrors) > 0 {
for _, err := range subscriberErrors {
fmt.Fprintf(b.Streams.Stderr, "Error: failed to run build command: %v\n", err)
}
return &aspecterrors.ExitError{ExitCode: 1}
exitCode = 1
}

if exitCode != 0 {
err := &aspecterrors.ExitError{ExitCode: exitCode}
if bazelErr != nil {
err.Err = bazelErr
}
return err
}

return nil
}

// Plugin defines only the methods for the build command.
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
// PostBuildHook is
PostBuildHook(ctx context.Context) error
}
9 changes: 9 additions & 0 deletions pkg/hooks/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "hooks",
srcs = ["hooks.go"],
importpath = "aspect.build/cli/pkg/hooks",
visibility = ["//visibility:public"],
deps = ["//pkg/aspecterrors"],
)
61 changes: 61 additions & 0 deletions pkg/hooks/hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright © 2021 Aspect Build Systems Inc
Not licensed for re-use.
*/

package hooks

import (
"context"

"aspect.build/cli/pkg/aspecterrors"
)

type Hooks struct {
postBuild *hookList
}

func New() *Hooks {
return &Hooks{
postBuild: &hookList{},
}
}

func (hooks *Hooks) RegisterPostBuild(fn PostBuildFn) {
hooks.postBuild.insert(fn)
}

func (hooks *Hooks) ExecutePostBuild(ctx context.Context) *aspecterrors.ErrorList {
errors := &aspecterrors.ErrorList{}
node := hooks.postBuild.head
for node != nil {
if err := node.fn.(PostBuildFn)(ctx); err != nil {
errors.Insert(err)
}
node = node.next
}
return errors
}

type PostBuildFn func(ctx context.Context) error

type hookList struct {
head *hookNode
tail *hookNode
}

func (l *hookList) insert(fn interface{}) {
node := &hookNode{fn: fn}
if l.head == nil {
l.head = node
} else {
l.tail.next = node
}
l.tail = node
}

type hookNode struct {
next *hookNode
fn interface{}
}
12 changes: 12 additions & 0 deletions pkg/plugins/fix_visibility/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "fix_visibility",
srcs = ["plugin.go"],
importpath = "aspect.build/cli/pkg/plugins/fix_visibility",
visibility = ["//pkg/aspect/build:__pkg__"],
deps = [
"@bazel_gazelle//label:go_default_library",
"@go_googleapis//google/devtools/build/v1:build_go_proto",
],
)
Loading

0 comments on commit 908dc2e

Please sign in to comment.