Skip to content

Commit 214bb7c

Browse files
committed
py_proto_library: Fix for strip_import_prefix
Fixes the handling of transitive proto_library dependencies with strip_import_prefix.
1 parent 2b05d9f commit 214bb7c

6 files changed

Lines changed: 51 additions & 1 deletion

File tree

examples/py_proto_library/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ py_test(
88
"//example.com/proto:pricetag_proto_py_pb2",
99
],
1010
)
11+
12+
py_test(
13+
name = "message_test",
14+
srcs = ["message_test.py"],
15+
deps = [
16+
"//example.com/another_proto:message_proto_py_pb2",
17+
],
18+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@rules_proto//proto:defs.bzl", "proto_library")
2+
load("@rules_python//python:proto.bzl", "py_proto_library")
3+
4+
py_proto_library(
5+
name = "message_proto_py_pb2",
6+
visibility = ["//visibility:public"],
7+
deps = [":message_proto"],
8+
)
9+
10+
proto_library(
11+
name = "message_proto",
12+
srcs = ["message.proto"],
13+
# https://bazel.build/reference/be/protocol-buffer#proto_library.strip_import_prefix
14+
strip_import_prefix = "/example.com",
15+
deps = ["//example.com/proto:pricetag_proto"],
16+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
syntax = "proto3";
2+
3+
package rules_python;
4+
5+
import "proto/pricetag.proto";
6+
7+
message TestMessage {
8+
uint32 index = 1;
9+
PriceTag pricetag = 2;
10+
}

examples/py_proto_library/example.com/proto/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ proto_library(
1212
srcs = ["pricetag.proto"],
1313
# https://bazel.build/reference/be/protocol-buffer#proto_library.strip_import_prefix
1414
strip_import_prefix = "/example.com",
15+
visibility = ["//visibility:public"],
1516
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sys
2+
import unittest
3+
4+
from another_proto import message_pb2
5+
6+
class TestCase(unittest.TestCase):
7+
def test_message(self):
8+
got = message_pb2.TestMessage(
9+
index = 5,
10+
)
11+
self.assertIsNotNone(got)
12+
13+
14+
if __name__ == "__main__":
15+
sys.exit(unittest.main())

python/private/proto/py_proto_library.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def _py_proto_aspect_impl(target, ctx):
114114
# will put the repo root at the top of the PYTHONPATH, ahead of
115115
# directories added through `imports` attributes.
116116
[proto_root] if "_virtual_imports" in proto_root else [],
117-
transitive = [dep[PyInfo].imports for dep in api_deps],
117+
transitive = [dep[PyInfo].imports for dep in api_deps] + [dep.imports for dep in deps],
118118
),
119119
runfiles_from_proto_deps = runfiles_from_proto_deps,
120120
transitive_sources = transitive_sources,

0 commit comments

Comments
 (0)