bazelbuild / rules_go

Go rules for Bazel
Apache License 2.0
1.35k stars 635 forks source link

Correct way to use the core gRPC libraries? #3971

Open popovicu opened 6 days ago

popovicu commented 6 days ago

What version of rules_go are you using?

0.46.0

What version of gazelle are you using?

N/A, not using explicitly. Gazelle is an indirect dependency.

What version of Bazel are you using?

7.2.0 (via Bazelisk v1.18.0)

Does this issue reproduce with the latest releases of all the above?

Yes.

What operating system and processor architecture are you using?

Mac OS on M2. Also Linux/x86_64.

Any other potentially useful information about your toolchain?

Invoking Bazel via Bazelisk.

What did you do?

Please check out my BUILD file for building the Go gRPC library here. The BUILD file for my simple server is here and the alias for the core gRPC library is here.

What did you expect to see?

Instead of debugging what are the repos that got injected by Gazelle during the build flow, I hoped it would be more straightforward to use something like @grpc//:go_default_library.

What did you see instead?

The only way I figured out to get the Go library for the core gRPC functionality is via the @@gazelle~~go_deps~org_golang_google_grpc//:go_default_library label.

fmeum commented 6 days ago

We have moved away from "injecting" third-party dependencies into your build as that only leads to trouble down the road. You can add a dependency on grpc like you would outside Bazel and then use the go_deps extension to make it available under a nice label. See https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/bzlmod.md#specifying-external-dependencies for the details and feel free to reopen if that doesn't resolve your issue.

popovicu commented 6 days ago

Right. I've tried this before, but I get issues with cyclical dependencies.

Please take a look at this pull request to see the changes I've made: https://github.com/popovicu/go-grpc-bazel-example/pull/1/commits/f215918d992b0811295da566ca628ed7349bc170

If I do bazel build //proto:foo_go_proto, this is the output I get:

ERROR: /private/var/tmp/_bazel_uros/670d6fc3c8a0fd52c40728110c12cfd3/external/gazelle~~go_deps~org_golang_google_protobuf/compiler/protogen/BUILD.bazel:3:11: in go_library rule @@gazelle~~go_deps~org_golang_google_protobuf//compiler/protogen:protogen: cycle in dependency graph:
    //proto:foo_go_proto (8e4ab22e89843aa348ba782be25eadb5bae40f2ae62dec3edd2cdaf809e8d7b1)
    //proto:foo_go_proto (096dcc84165363e69a851ebe8131b032f5448c94ddc4951775429dc78e79f898)
    @@rules_go~//proto:go_grpc_v2 (096dcc84165363e69a851ebe8131b032f5448c94ddc4951775429dc78e79f898)
    @@rules_go~//proto:go_grpc_v2_reset_plugin_ (6f6e3fee8212c902eb428d9d1af78e5a07aed64216bca6679c1cfc5d78f89b4e)
    @@gazelle~~go_deps~org_golang_google_grpc_cmd_protoc_gen_go_grpc//:protoc-gen-go-grpc (0247d78883ba174476539e1ef0d8f1b79d20e12e1df16389f8f4a830b863103a)
    @@gazelle~~go_deps~org_golang_google_grpc_cmd_protoc_gen_go_grpc//:protoc-gen-go-grpc_lib (0247d78883ba174476539e1ef0d8f1b79d20e12e1df16389f8f4a830b863103a)
.-> @@gazelle~~go_deps~org_golang_google_protobuf//compiler/protogen:protogen (0247d78883ba174476539e1ef0d8f1b79d20e12e1df16389f8f4a830b863103a)
|   @@gazelle~~go_deps~org_golang_google_protobuf//reflect/protodesc:protodesc (0247d78883ba174476539e1ef0d8f1b79d20e12e1df16389f8f4a830b863103a)
|   @@gazelle~~go_deps~org_golang_google_protobuf//types/gofeaturespb:gofeaturespb (0247d78883ba174476539e1ef0d8f1b79d20e12e1df16389f8f4a830b863103a)
|   @@gazelle~~go_deps~org_golang_google_protobuf//types/gofeaturespb:gofeaturespb_go_proto (0247d78883ba174476539e1ef0d8f1b79d20e12e1df16389f8f4a830b863103a)
|   @@rules_go~//proto:go_proto (0247d78883ba174476539e1ef0d8f1b79d20e12e1df16389f8f4a830b863103a)
|   @@rules_go~//proto:go_proto_reset_plugin_ (0247d78883ba174476539e1ef0d8f1b79d20e12e1df16389f8f4a830b863103a)
|   @@gazelle~~go_deps~org_golang_google_protobuf//cmd/protoc-gen-go:protoc-gen-go (0247d78883ba174476539e1ef0d8f1b79d20e12e1df16389f8f4a830b863103a)
|   @@gazelle~~go_deps~org_golang_google_protobuf//cmd/protoc-gen-go:protoc-gen-go_lib (0247d78883ba174476539e1ef0d8f1b79d20e12e1df16389f8f4a830b863103a)
`-- @@gazelle~~go_deps~org_golang_google_protobuf//compiler/protogen:protogen (0247d78883ba174476539e1ef0d8f1b79d20e12e1df16389f8f4a830b863103a)
ERROR: Analysis of target '//proto:foo_go_proto' failed; build aborted
INFO: Elapsed time: 9.346s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully

Am I doing something wrong here?

fmeum commented 6 days ago

@linzhp I remember having seen this before, do you know how it could be solved?

popovicu commented 6 days ago

@fmeum, can we please reopen the issue? I don't have the permission to do it. 😞