3

So I am trying to convert a monorepo of micro services (C#, Go, NodeJS) to use bazel. Just playing with it for now. I focus on one go service to get started and isolated it as a WORKSPACE. The go service is gRPC service that uses protobuf obviously, grpc-gateway with the protoc-gen-swagger and also protoc-gen-gorm (this one does not support bazel).

The code builds using a command like go build cmd/server/server.go

I am hoping to get some guidance on how to get started to build this project with all the dependencies.

I see several rules available for protobuf/go and I am not yet comfortable browsing through them or deciding which is better (i cannot get any to work due to grpc gateway or protoc gen gorm) - https://github.com/stackb/rules_proto - https://github.com/bazelbuild/rules_go - https://github.com/stackb/rules_proto/tree/master/github.com/grpc-ecosystem/grpc-gateway

Code structure looks like this:

/repo
   svc1
   svc2
   svc3
       cmd/server
           BUILD.bazel 
           server.go
       pkg
           contains folders and some go files and a BUILD.bazel in each
       proto
           BUILD.bazel
           test.proto
       WORKSPACE
       BUILD.bazel

Right now I only work on svc3. Later i will probably move the WORKSPACE to the parent folder.

My WORKSPACE looks like this:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "io_bazel_rules_go",
    sha256 = "96b1f81de5acc7658e1f5a86d7dc9e1b89bc935d83799b711363a748652c471a",
    urls = [
        "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/0.19.2/rules_go-0.19.2.tar.gz",
        "https://github.com/bazelbuild/rules_go/releases/download/0.19.2/rules_go-0.19.2.tar.gz",
    ],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains()

http_archive(
    name = "bazel_gazelle",
    urls = [
        "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/bazel-gazelle/releases/download/0.18.1/bazel-gazelle-0.18.1.tar.gz",
        "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.18.1/bazel-gazelle-0.18.1.tar.gz",
    ],
    sha256 = "be9296bfd64882e3c08e3283c58fcb461fa6dd3c171764fcc4cf322f60615a9b",
)

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

gazelle_dependencies()

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
    name = "com_google_protobuf",
    commit = "09745575a923640154bcf307fba8aedff47f240a",
    remote = "https://github.com/protocolbuffers/protobuf",
    shallow_since = "1558721209 -0700",
)

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

+ a bunch of go_repository() created by Gazelle

Running gazelle created a bunch of build.bazel files for my go project in each folder.

Next to the .proto, I have a generated build.bazel file:

load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")

proto_library(
    name = "svc_proto",
    srcs = ["test.proto"],
    visibility = ["//visibility:public"],
    deps = [
        # the two github below are referenced as go_repository
        "@com_github_infobloxopen_protoc_gen_gorm//options:proto_library", # not sure what to put after the colon
        "@com_github_grpc_ecosystem_grpc_gateway//protoc-gen-swagger/options:proto_library",
        "@go_googleapis//google/api:annotations_proto",
    ],
)

go_proto_library(
    name = "svc_go_proto",
    compilers = ["@io_bazel_rules_go//proto:go_grpc"],
    importpath = "src/test/proto/v1",
    proto = ":svc_proto",
    visibility = ["//visibility:public"],
    deps = [
        "//github.com/infobloxopen/protoc-gen-gorm/options:go_default_library",
        "//github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/options:go_default_library",
        "@go_googleapis//google/api:annotations_go_proto",
    ],
)

go_library(
    name = "go_default_library",
    embed = [":svc_go_proto"],
    importpath = "src/test/proto/v1",
    visibility = ["//visibility:public"],
)

Now the questions:

  1. not sure what to put to reference other proto files: "@com_github_infobloxopen_protoc_gen_gorm//options:proto_library" ? and not sure this is the best way to reference other external libraries from git.
  2. if i build the above using bazel build //proto/v1:svc_proto, i get: no such target '@com_github_grpc_ecosystem_grpc_gateway//protoc-gen-swagger/options:proto_library': target 'proto_library' not declared in package 'protoc-gen-swagger/options'. Probably linked to 1.
  3. i am not sure which rule to use. As I need grpc gateway, I guess i need to exclusively use https://github.com/stackb/rules_proto/tree/master/github.com/grpc-ecosystem/grpc-gateway but i can't make them to work either.
  4. I use statik (https://github.com/rakyll/statik) to package the swagger file in go to server the swagger. Is there any alternative or if not, how can i call a custom bash/command as part of the build process in the chain?

In summary, I am pretty sure my BUILD.bazel file to build the proto and library is structured wrong and would appreciate some up-to-date guidance (github is full of repos that are outdated, using outdated rules or simply not working).

Jon
  • 835
  • 8
  • 17

0 Answers0