Skip to content

Commit 3576a1f

Browse files
authored
Loosen py_proto_library check to be on the import path instead of full directory (i.e. excluding external/module-name prefix). (#20569)
The module name does not actually make it into the name used in python imports, so this should allow py_proto_library to work for modules with hyphens in their name (i.e. `bazel build @com_google_protobuf-examples//:addressbook_py_pb2`). PiperOrigin-RevId: 733763224
1 parent efa65c5 commit 3576a1f

File tree

7 files changed

+53
-4
lines changed

7 files changed

+53
-4
lines changed

.github/workflows/test_bazel.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,6 @@ jobs:
6969
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
7070
bazel-cache: examples
7171
version: ${{ matrix.bazelversion }}
72-
bash: cd examples && bazel build //... $BAZEL_FLAGS --enable_bzlmod=${{ matrix.bzlmod }} --enable_workspace=${{ !matrix.bzlmod }} ${{ matrix.toolchain_resolution }}
72+
bash: >
73+
cd examples;
74+
bazel build //... @com_google_protobuf-examples-with-hyphen//... $BAZEL_FLAGS --enable_bzlmod=${{ matrix.bzlmod }} --enable_workspace=${{ !matrix.bzlmod }} ${{ matrix.toolchain_resolution }};

bazel/py_proto_library.bzl

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ def _py_proto_aspect_impl(target, ctx):
4444

4545
# Check Proto file names
4646
for proto in target[ProtoInfo].direct_sources:
47-
if proto.is_source and "-" in proto.dirname:
48-
fail("Cannot generate Python code for a .proto whose path contains '-' ({}).".format(
47+
import_path = proto_common.get_import_path(proto)
48+
if proto.is_source and "-" in import_path:
49+
fail("Cannot generate Python code for a .proto whose python import path contains '-' ({}).".format(
4950
proto.path,
5051
))
5152

examples/MODULE.bazel

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ local_path_override(
1212
path = "..",
1313
)
1414

15+
bazel_dep(name = "com_google_protobuf-examples-with-hyphen", version = "0.0.0", dev_dependency = True)
16+
local_path_override(
17+
module_name = "com_google_protobuf-examples-with-hyphen",
18+
path = "examples_with_hyphen",
19+
)
20+
1521
bazel_dep(name = "bazel_skylib", version = "1.7.1")
1622
bazel_dep(name = "platforms", version = "0.0.10")
1723
bazel_dep(name = "rules_cc", version = "0.0.17")

examples/WORKSPACE

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ local_repository(
2020
path = "..",
2121
)
2222

23+
local_repository(
24+
name = "com_google_protobuf-examples-with-hyphen",
25+
path = "examples_with_hyphen",
26+
)
27+
2328
# Similar to com_google_protobuf but for Java lite. If you are building
2429
# for Android, the lite version should be preferred because it has a much
2530
# smaller code size.
@@ -38,7 +43,7 @@ http_archive(
3843
],
3944
)
4045

41-
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps", "PROTOBUF_MAVEN_ARTIFACTS")
46+
load("@com_google_protobuf//:protobuf_deps.bzl", "PROTOBUF_MAVEN_ARTIFACTS", "protobuf_deps")
4247

4348
protobuf_deps()
4449

@@ -61,12 +66,15 @@ rules_cc_dependencies()
6166
rules_cc_toolchains()
6267

6368
load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")
69+
6470
rules_jvm_external_deps()
6571

6672
load("@rules_jvm_external//:setup.bzl", "rules_jvm_external_setup")
73+
6774
rules_jvm_external_setup()
6875

6976
load("@rules_jvm_external//:defs.bzl", "maven_install")
77+
7078
maven_install(
7179
name = "maven",
7280
artifacts = PROTOBUF_MAVEN_ARTIFACTS,
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
2+
load("@com_google_protobuf//bazel:py_proto_library.bzl", "py_proto_library")
3+
4+
proto_library(
5+
name = "empty_proto",
6+
srcs = ["empty.proto"],
7+
)
8+
9+
# py_proto_library rule with hyphen in @com_google_protobuf-examples-with-hyphen module name.
10+
py_proto_library(
11+
name = "empty_py_pb2",
12+
visibility = ["//visibility:public"],
13+
deps = [":empty_proto"],
14+
)
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""Bazel module dependencies"""
2+
3+
module(
4+
name = "com_google_protobuf-examples-with-hyphen",
5+
version = "0.0.0",
6+
compatibility_level = 1,
7+
)
8+
9+
bazel_dep(name = "protobuf", version = "0.0.0", repo_name = "com_google_protobuf")
10+
local_path_override(
11+
module_name = "protobuf",
12+
path = "..",
13+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
edition = "2023";
2+
3+
package examples.with.hyphen;
4+
5+
message EmptyMessage {}

0 commit comments

Comments
 (0)