rules_go icon indicating copy to clipboard operation
rules_go copied to clipboard

Support --incompatible_enable_proto_toolchain_resolution

Open alexeagle opened this issue 2 years ago • 6 comments

What version of rules_go are you using?

Current

What version of Bazel are you using?

7.0.0 or later

What did you do?

Try to build a go_library target with the flag enabled and a different toolchain registered for @rules_proto//proto:toolchain_type

What did you expect to see?

When --incompatible_enable_proto_toolchain_resolution is enabled, rules should consult the relevant toolchain_type to locate protoc. Example PR from rules_python: https://github.com/bazelbuild/rules_python/pull/1577

That means I ought to be able to add a Go example to https://github.com/alexeagle/toolchains_protoc/tree/main/examples which is a project that ensures we never have to build the cc_binary target for protoc from sources.

What did you see instead?

https://github.com/bazelbuild/rules_go/blob/cac338e5c869116cbe86758001dced886a38b39c/proto/BUILD.bazel#L129 depends on @com_google_protobuf//:protoc directly.

In this case I registered a pre-built protoc toolchain (using https://github.com/alexeagle/toolchains_protoc) but get an error:

$ bazel query --output=build //cli/core/pkg/aspect/config:config
# /home/alex/Projects/silo/cli/core/pkg/aspect/config/BUILD.bazel:3:11
go_library(
  name = "config",
  visibility = ["//visibility:public"],
  generator_name = "config",
  generator_function = "go_library_macro",
  generator_location = "cli/core/pkg/aspect/config/BUILD.bazel:3:11",
  srcs = ["//cli/core/pkg/aspect/config:config.go"],
  deps = ["//cli/core/pkg/bazel:bazel", "//cli/core/pkg/ioutils:ioutils", "@com_github_spf13_cobra//:cobra"],
  importpath = "github.com/aspect-build/silo/cli/core/pkg/aspect/config",
)
# Rule config instantiated at (most recent call last):
#   /home/alex/Projects/silo/cli/core/pkg/aspect/config/BUILD.bazel:3:11                                                                in <toplevel>
#   /home/alex/.cache/bazel/_bazel_alex/cf34354d28c19c286eb12fd35d42d565/external/io_bazel_rules_go/go/private/rules/wrappers.bzl:45:15 in go_library_macro
# Rule go_library defined at (most recent call last):
#   /home/alex/.cache/bazel/_bazel_alex/cf34354d28c19c286eb12fd35d42d565/external/io_bazel_rules_go/go/private/rules/library.bzl:61:18 in <toplevel>

alex@a:~/Projects/silo$ code $(bazel info output_base)/external
alex@a:~/Projects/silo$ bazel build //cli/core/pkg/aspect/config:config
ERROR: /home/alex/.cache/bazel/_bazel_alex/cf34354d28c19c286eb12fd35d42d565/external/com_google_protobuf/BUILD: no such target '@@com_google_protobuf//:protoc': target 'protoc' not declared in package '' defined by /home/alex/.cache/bazel/_bazel_alex/cf34354d28c19c286eb12fd35d42d565/external/com_google_protobuf/BUILD (Tip: use `query "@com_google_protobuf//:*"` to see all the targets in that package)
ERROR: /home/alex/.cache/bazel/_bazel_alex/cf34354d28c19c286eb12fd35d42d565/external/io_bazel_rules_go/proto/BUILD.bazel:127:20: no such target '@@com_google_protobuf//:protoc': target 'protoc' not declared in package '' defined by /home/alex/.cache/bazel/_bazel_alex/cf34354d28c19c286eb12fd35d42d565/external/com_google_protobuf/BUILD (Tip: use `query "@com_google_protobuf//:*"` to see all the targets in that package) and referenced by '@@io_bazel_rules_go//proto:protoc'

alexeagle avatar Mar 19 '24 02:03 alexeagle

I am a huge fan of this and will try to work on it in early April unless someone gets to it earlier.

Cc @linzhp

fmeum avatar Mar 19 '24 19:03 fmeum

@fmeum note the rules_python change just landed, and @thesayyn tells me rules_java is already switched. Would be nice to include rules_go in this when rules_proto 6.0.0 final goes out (it's currently RC2)

alexeagle avatar Apr 11 '24 19:04 alexeagle

@thesayyn I tried using rules_java with --incompatible_enable_proto_toolchain_resolution, but it fails to find a proto_lang_toolchain for Java even with Bazel@HEAD. Do you know what's needed to get this to work?

fmeum avatar Apr 12 '24 14:04 fmeum

Toolchain for java comes from protobuf repo, that's only one we i know of unless @alexeagle added one in his prebuilt toolchains ruleset.

thesayyn avatar Apr 12 '24 17:04 thesayyn

Right, the flag decouples from the hardcoded toolchain but doesn't automatically supply a replacement. It should be easy to fetch the runtime from Maven exactly like the protobuf docs instruct all non-Bazel users/tools should do. I'll add to the toolchains_protoc

alexeagle avatar Apr 12 '24 17:04 alexeagle

correction, https://github.com/bazelbuild/examples/blob/never_compile_protoc_again/proto/java/Main.java already shows how this works for Java.

alexeagle avatar Apr 13 '24 00:04 alexeagle

Is there an example of using this with rules_go?

johanbrandhorst avatar May 23 '24 16:05 johanbrandhorst

Is there an example of using this with rules_go?

You don't need to do anything extra for rules_go, just flip the flag and have a protoc toolchain registered (e.g. from toolchains_protoc). rules_go doesn't use proto_lang_toolchain, so you also don't have to register one.

fmeum avatar May 23 '24 17:05 fmeum

Adding the example here: https://github.com/aspect-build/toolchains_protoc/pull/10 (but it's trivial like @fmeum says, no additional toolchain to register)

alexeagle avatar May 23 '24 19:05 alexeagle