Skip to content

Commit 6905e63

Browse files
aignasrickeylev
andauthored
fix: make py_proto_library respect PyInfo imports (bazel-contrib#1046)
py_proto_library has an implicitly dependency on the protobuf client runtime, and it was ending up in runfiles, but it wasn't imported because the `imports` value it was setting wasn't be propagated. To fix, make py_proto_library properly propagate the imports attribute the protobuf client runtime so that the libraries are added to sys.path correct. Also adds an example for bzlmod and old way of using the py_proto_library as a test. --------- Co-authored-by: Richard Levasseur <richardlev@gmail.com>
1 parent 6bcee35 commit 6905e63

File tree

13 files changed

+203
-4
lines changed

13 files changed

+203
-4
lines changed

.bazelci/presubmit.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ buildifier:
3131
- "..."
3232
test_flags:
3333
- "--test_tag_filters=-integration-test"
34+
.common_bzlmod_flags: &common_bzlmod_flags
35+
test_flags:
36+
- "--experimental_enable_bzlmod"
37+
build_flags:
38+
- "--experimental_enable_bzlmod"
3439
.reusable_build_test_all: &reusable_build_test_all
3540
build_targets: ["..."]
3641
test_targets: ["..."]
@@ -211,6 +216,53 @@ tasks:
211216
# We don't run pip_parse_vendored under Windows as the file checked in is
212217
# generated from a repository rule containing OS-specific rendered paths.
213218

219+
integration_test_py_proto_library_ubuntu:
220+
<<: *reusable_build_test_all
221+
name: py_proto_library integration tests on Ubuntu
222+
working_directory: examples/py_proto_library
223+
platform: ubuntu2004
224+
integration_test_py_proto_library_debian:
225+
<<: *reusable_build_test_all
226+
name: py_proto_library integration tests on Debian
227+
working_directory: examples/py_proto_library
228+
platform: debian11
229+
integration_test_py_proto_library_macos:
230+
<<: *reusable_build_test_all
231+
name: py_proto_library integration tests on macOS
232+
working_directory: examples/py_proto_library
233+
platform: macos
234+
integration_test_py_proto_library_windows:
235+
<<: *reusable_build_test_all
236+
name: py_proto_library integration tests on Windows
237+
working_directory: examples/py_proto_library
238+
platform: windows
239+
240+
# Check the same using bzlmod as well
241+
integration_test_py_proto_library_bzlmod_ubuntu:
242+
<<: *reusable_build_test_all
243+
<<: *common_bzlmod_flags
244+
name: py_proto_library bzlmod integration tests on Ubuntu
245+
working_directory: examples/py_proto_library
246+
platform: ubuntu2004
247+
integration_test_py_proto_library_bzlmod_debian:
248+
<<: *reusable_build_test_all
249+
<<: *common_bzlmod_flags
250+
name: py_proto_library bzlmod integration tests on Debian
251+
working_directory: examples/py_proto_library
252+
platform: debian11
253+
integration_test_py_proto_library_bzlmod_macos:
254+
<<: *reusable_build_test_all
255+
<<: *common_bzlmod_flags
256+
name: py_proto_library bzlmod integration tests on macOS
257+
working_directory: examples/py_proto_library
258+
platform: macos
259+
integration_test_py_proto_library_bzlmod_windows:
260+
<<: *reusable_build_test_all
261+
<<: *common_bzlmod_flags
262+
name: py_proto_library bzlmod integration tests on Windows
263+
working_directory: examples/py_proto_library
264+
platform: windows
265+
214266
integration_test_pip_repository_annotations_ubuntu:
215267
<<: *reusable_build_test_all
216268
name: pip_repository_annotations integration tests on Ubuntu

.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/get_url,examples/bzlmod,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/runfiles,examples/multi_python_versions,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements,tests/compile_pip_requirements,tests/pip_repository_entry_points,tests/pip_deps
7-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/get_url,examples/bzlmod,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/runfiles,examples/multi_python_versions,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements,tests/compile_pip_requirements,tests/pip_repository_entry_points,tests/pip_deps
6+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/get_url,examples/bzlmod,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/runfiles,examples/multi_python_versions,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_import,examples/py_proto_library,examples/relative_requirements,tests/compile_pip_requirements,tests/pip_repository_entry_points,tests/pip_deps
7+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/get_url,examples/bzlmod,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/runfiles,examples/multi_python_versions,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_import,examples/py_proto_library,examples/relative_requirements,tests/compile_pip_requirements,tests/pip_repository_entry_points,tests/pip_deps
88

99
test --test_output=errors
1010

examples/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ bazel_integration_test(
4343
timeout = "long",
4444
)
4545

46+
bazel_integration_test(
47+
name = "py_proto_library_example",
48+
timeout = "long",
49+
)
50+
51+
bazel_integration_test(
52+
name = "py_proto_library_example_bzlmod",
53+
timeout = "long",
54+
bzlmod = True,
55+
dirname = "py_proto_library",
56+
)
57+
4658
bazel_integration_test(
4759
name = "multi_python_versions_example",
4860
timeout = "long",

examples/py_proto_library/.bazelrc

Whitespace-only changes.

examples/py_proto_library/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# git ignore patterns
2+
3+
/bazel-*
4+
user.bazelrc

examples/py_proto_library/BUILD.bazel

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
load("@rules_proto//proto:defs.bzl", "proto_library")
2+
load("@rules_python//python:defs.bzl", "py_test")
3+
load("@rules_python//python:proto.bzl", "py_proto_library")
4+
5+
py_proto_library(
6+
name = "pricetag_proto_py_pb2",
7+
deps = [":pricetag_proto"],
8+
)
9+
10+
proto_library(
11+
name = "pricetag_proto",
12+
srcs = ["pricetag.proto"],
13+
)
14+
15+
py_test(
16+
name = "pricetag_test",
17+
srcs = ["test.py"],
18+
main = "test.py",
19+
deps = [
20+
":pricetag_proto_py_pb2",
21+
],
22+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module(
2+
name = "rules_python_py_proto_library_example",
3+
version = "0.0.0",
4+
compatibility_level = 1,
5+
)
6+
7+
bazel_dep(name = "rules_python", version = "0.17.3")
8+
9+
# The following local_path_override is only needed to run this example as part of our CI.
10+
local_path_override(
11+
module_name = "rules_python",
12+
path = "../..",
13+
)
14+
15+
python = use_extension("@rules_python//python:extensions.bzl", "python")
16+
python.toolchain(
17+
name = "python3_9",
18+
configure_coverage_tool = True,
19+
python_version = "3.9",
20+
)
21+
use_repo(python, "python3_9_toolchains")
22+
23+
register_toolchains(
24+
"@python3_9_toolchains//:all",
25+
)
26+
27+
# We are using rules_proto to define rules_proto targets to be consumed by py_proto_library.
28+
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")

examples/py_proto_library/WORKSPACE

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
workspace(name = "rules_python_py_proto_library_example")
2+
3+
# The following local_path_override is only needed to run this example as part of our CI.
4+
local_repository(
5+
name = "rules_python",
6+
path = "../..",
7+
)
8+
9+
# When not using this example in the rules_python git repo you would load the python
10+
# rules using http_archive(), as documented in the release notes.
11+
12+
load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")
13+
14+
# We install the rules_python dependencies using the function below.
15+
py_repositories()
16+
17+
python_register_toolchains(
18+
name = "python39",
19+
python_version = "3.9",
20+
)
21+
22+
# Then we need to setup dependencies in order to use py_proto_library
23+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
24+
25+
http_archive(
26+
name = "rules_proto",
27+
sha256 = "dc3fb206a2cb3441b485eb1e423165b231235a1ea9b031b4433cf7bc1fa460dd",
28+
strip_prefix = "rules_proto-5.3.0-21.7",
29+
urls = [
30+
"https://github.com/bazelbuild/rules_proto/archive/refs/tags/5.3.0-21.7.tar.gz",
31+
],
32+
)
33+
34+
http_archive(
35+
name = "com_google_protobuf",
36+
sha256 = "75be42bd736f4df6d702a0e4e4d30de9ee40eac024c4b845d17ae4cc831fe4ae",
37+
strip_prefix = "protobuf-21.7",
38+
urls = [
39+
"https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v21.7.tar.gz",
40+
"https://github.com/protocolbuffers/protobuf/archive/v21.7.tar.gz",
41+
],
42+
)
43+
44+
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
45+
46+
rules_proto_dependencies()
47+
48+
rules_proto_toolchains()

examples/py_proto_library/WORKSPACE.bzlmod

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
syntax = "proto3";
2+
3+
package rules_python;
4+
5+
message PriceTag {
6+
string name = 2;
7+
double cost = 1;
8+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy