From 609b39f6e6258df1ea79d76e454634ccfdcba634 Mon Sep 17 00:00:00 2001 From: Chris Chua Date: Sat, 12 Apr 2025 10:17:58 +0800 Subject: [PATCH 1/2] make example just work --- examples/proto/BUILD.bazel | 4 +- examples/python/python_grpc_library.bzl | 60 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 examples/python/python_grpc_library.bzl diff --git a/examples/proto/BUILD.bazel b/examples/proto/BUILD.bazel index 797eb18..c4e53cb 100644 --- a/examples/proto/BUILD.bazel +++ b/examples/proto/BUILD.bazel @@ -3,7 +3,7 @@ load("@rules_go//proto:def.bzl", "go_proto_library") load("@rules_proto//proto:defs.bzl", "proto_library") load("@rules_rust_prost//:defs.bzl", "rust_prost_library") load("@protobuf//bazel:py_proto_library.bzl", "py_proto_library") -load("//python:python_grpc_compile.bzl", "python_grpc_compile") +load("//python:python_grpc_library.bzl", "python_grpc_library") package(default_visibility = ["//visibility:public"]) @@ -18,7 +18,7 @@ py_proto_library( deps = [":greeter_proto"], ) -python_grpc_compile( +python_grpc_library( name = "greeter_py_grpc", protos = [":greeter_proto"], ) diff --git a/examples/python/python_grpc_library.bzl b/examples/python/python_grpc_library.bzl new file mode 100644 index 0000000..0e9071f --- /dev/null +++ b/examples/python/python_grpc_library.bzl @@ -0,0 +1,60 @@ +"""Generated definition of python_grpc_library.""" + +load("@rules_proto_grpc//:defs.bzl", "bazel_build_rule_common_attrs", "proto_compile_attrs") +load("@pypi//:requirements.bzl", "requirement") +load("@rules_python//python:defs.bzl", "py_library") +load(":python_grpc_compile.bzl", "python_grpc_compile") + +def python_grpc_library(name, generate_pyi = False, **kwargs): + """ + python_grpc_library generates Python code from proto and gRPC, and creates a py_library for them. + + Args: + name: the name of the target. + generate_pyi: flag to specify whether .pyi files should be created. + **kwargs: common Bazel attributes will be passed to both python_grpc_compile and py_library; + python_grpc_compile attributes will be passed to python_grpc_compile only. + """ + + # Compile protos + name_pb = name + "_pb" + python_grpc_compile( + name = name_pb, + **{ + k: v + for (k, v) in kwargs.items() + if k in proto_compile_attrs.keys() or + k in bazel_build_rule_common_attrs + } # Forward args + ) + + # For other code to import generated code with prefix_path if it's given + output_mode = kwargs.get("output_mode", "PREFIXED") + if output_mode == "PREFIXED": + imports = [name_pb] + else: + imports = ["."] + + # for pb2_grpc.py to import pb2.py + prefix_path = kwargs.get("prefix_path", None) + if prefix_path: + imports.append(imports[0] + "/" + prefix_path) + + # Create python library + py_library( + name = name, + srcs = [name_pb], + deps = GRPC_DEPS + kwargs.get("deps", []), + data = kwargs.get("data", []), # See https://github.com/rules-proto-grpc/rules_proto_grpc/issues/257 for use case + imports = imports, + **{ + k: v + for (k, v) in kwargs.items() + if k in bazel_build_rule_common_attrs + } # Forward Bazel common args + ) + +GRPC_DEPS = [ + Label(requirement("grpcio")), + Label(requirement("protobuf")), +] \ No newline at end of file From 479828b658a67f116297cf89fde46fedd456545d Mon Sep 17 00:00:00 2001 From: Chris Chua Date: Tue, 15 Apr 2025 20:41:37 +0800 Subject: [PATCH 2/2] get the toolchain prioritzed --- examples/MODULE.bazel | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel index 68aa9b2..67ad840 100644 --- a/examples/MODULE.bazel +++ b/examples/MODULE.bazel @@ -38,8 +38,6 @@ protoc.toolchain( ) use_repo(protoc, "com_google_protobuf", "toolchains_protoc_hub") -register_toolchains("@toolchains_protoc_hub//:all") - # NB: the `:all` here is critical, because `proto_lang_toolchain` expands into two targets: # - proto_lang_toolchain rule [name] # - toolchain rule [name]_toolchain @@ -49,6 +47,8 @@ register_toolchains("@toolchains_protoc_hub//:all") # Declared toolchains should be created with the 'toolchain' rule and should not have dependencies that themselves require toolchains. register_toolchains("//tools/toolchains:all") +register_toolchains("@toolchains_protoc_hub//:all") + ####### PYTHON ########## # Shows how a typical Python user fetches all the dependencies of their app, including the protobuf runtime dev_pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")