Skip to content

Minor tweaks to fix grpc example #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: update
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions examples/proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand All @@ -18,7 +18,7 @@ py_proto_library(
deps = [":greeter_proto"],
)

python_grpc_compile(
python_grpc_library(
name = "greeter_py_grpc",
protos = [":greeter_proto"],
)
Expand Down
60 changes: 60 additions & 0 deletions examples/python/python_grpc_library.bzl
Original file line number Diff line number Diff line change
@@ -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")),
]