Skip to content

Commit d96214f

Browse files
authored
fix: py_proto_library: transitive strip_import_prefix (#1558)
Fixes the handling of transitive `proto_library` dependencies with `strip_import_prefix`. Fixes #1557
1 parent 85e50d2 commit d96214f

File tree

7 files changed

+53
-3
lines changed

7 files changed

+53
-3
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# This lets us glob() up all the files inside the examples to make them inputs to tests
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, run tools/bazel_integration_test/update_deleted_packages.sh
6-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/proto,tests/compile_pip_requirements,tests/compile_pip_requirements_test_from_external_workspace,tests/ignore_root_user_error,tests/pip_repository_entry_points
7-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/proto,tests/compile_pip_requirements,tests/compile_pip_requirements_test_from_external_workspace,tests/ignore_root_user_error,tests/pip_repository_entry_points
6+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,tests/compile_pip_requirements,tests/compile_pip_requirements_test_from_external_workspace,tests/ignore_root_user_error,tests/pip_repository_entry_points
7+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,tests/compile_pip_requirements,tests/compile_pip_requirements_test_from_external_workspace,tests/ignore_root_user_error,tests/pip_repository_entry_points
88

99
test --test_output=errors
1010

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)