From 324ea5a9c1735c13356636985c7f64ab7ac6af6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 15 Dec 2021 02:39:29 +0000 Subject: [PATCH] [bazel] Fix import path for virtual packages (#28103) This change moves the logic for computing the package name part of an import directory from python_rules.bzl to protobuf.bzl, and reinstates the workspace root part of the directory. The effect is that it is now possible for a py_library_rule to depend on a proto_library in a different repository whose source files are generated. --- bazel/protobuf.bzl | 6 ++-- bazel/python_rules.bzl | 2 +- bazel/test/python_second_test_repo/WORKSPACE | 1 + .../test/python_second_test_repo/proto/BUILD | 29 +++++++++++++++++++ bazel/test/python_test_repo/WORKSPACE | 5 ++++ .../test/python_test_repo/in_subpackage/BUILD | 1 + .../in_subpackage/subpackage.proto | 4 ++- 7 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 bazel/test/python_second_test_repo/WORKSPACE create mode 100644 bazel/test/python_second_test_repo/proto/BUILD diff --git a/bazel/protobuf.bzl b/bazel/protobuf.bzl index a12208e9579..4de8f05d708 100644 --- a/bazel/protobuf.bzl +++ b/bazel/protobuf.bzl @@ -299,11 +299,11 @@ def get_out_dir(protos, context): if at_least_one_virtual: out_dir = get_include_directory(protos[0]) ws_root = protos[0].owner.workspace_root - if ws_root and out_dir.find(ws_root) >= 0: - out_dir = "".join(out_dir.rsplit(ws_root, 1)) + prefix = "/" + _make_prefix(protos[0].owner) + _VIRTUAL_IMPORTS[1:] + return struct( path = out_dir, - import_path = out_dir[out_dir.find(_VIRTUAL_IMPORTS) + 1:], + import_path = out_dir[out_dir.find(prefix) + 1:], ) out_dir = context.genfiles_dir.path diff --git a/bazel/python_rules.bzl b/bazel/python_rules.bzl index fa63d4a2dc3..4cf9e1a6178 100644 --- a/bazel/python_rules.bzl +++ b/bazel/python_rules.bzl @@ -85,7 +85,7 @@ def _gen_py_aspect_impl(target, context): imports = [] if out_dir.import_path: - imports.append("%s/%s/%s" % (context.workspace_name, context.label.package, out_dir.import_path)) + imports.append("{}/{}".format(context.workspace_name, out_dir.import_path)) py_info = PyInfo(transitive_sources = depset(direct = out_files), imports = depset(direct = imports)) return PyProtoInfo( diff --git a/bazel/test/python_second_test_repo/WORKSPACE b/bazel/test/python_second_test_repo/WORKSPACE new file mode 100644 index 00000000000..05a2b5b5a60 --- /dev/null +++ b/bazel/test/python_second_test_repo/WORKSPACE @@ -0,0 +1 @@ +workspace(name = "my_messages") diff --git a/bazel/test/python_second_test_repo/proto/BUILD b/bazel/test/python_second_test_repo/proto/BUILD new file mode 100644 index 00000000000..3a0ff8e4e8f --- /dev/null +++ b/bazel/test/python_second_test_repo/proto/BUILD @@ -0,0 +1,29 @@ +# gRPC Bazel BUILD file. +# +# Copyright 2021 The gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +proto_library( + name = "my_proto", + srcs = ["my.proto"], + visibility = ["//visibility:public"], +) + +genrule( + name = "make_my_proto", + outs = ["my.proto"], + cmd = """ + echo -e "syntax = \\"proto3\\";\npackage somewhere_else;\nmessage MyMessage {};" > $@ + """, +) diff --git a/bazel/test/python_test_repo/WORKSPACE b/bazel/test/python_test_repo/WORKSPACE index a10b8f96768..e704b8b0612 100644 --- a/bazel/test/python_test_repo/WORKSPACE +++ b/bazel/test/python_test_repo/WORKSPACE @@ -13,3 +13,8 @@ grpc_deps() load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps") grpc_extra_deps() + +local_repository( + name = "some_other_repo", + path = "../python_second_test_repo", +) diff --git a/bazel/test/python_test_repo/in_subpackage/BUILD b/bazel/test/python_test_repo/in_subpackage/BUILD index d656bdaefb5..2b008c8b5d3 100644 --- a/bazel/test/python_test_repo/in_subpackage/BUILD +++ b/bazel/test/python_test_repo/in_subpackage/BUILD @@ -24,4 +24,5 @@ package( proto_library( name = "subpackage_proto", srcs = ["subpackage.proto"], + deps = ["@some_other_repo//proto:my_proto"], ) diff --git a/bazel/test/python_test_repo/in_subpackage/subpackage.proto b/bazel/test/python_test_repo/in_subpackage/subpackage.proto index a3325b0a122..12cc10314e2 100644 --- a/bazel/test/python_test_repo/in_subpackage/subpackage.proto +++ b/bazel/test/python_test_repo/in_subpackage/subpackage.proto @@ -21,7 +21,9 @@ option objc_class_prefix = "SP"; package subpackage; +import "proto/my.proto"; + message Subpackaged { string name = 1; + somewhere_else.MyMessage msg = 2; } -