[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.
pull/28361/head
Thomas Köppe 3 years ago committed by GitHub
parent 835afc0fcf
commit 324ea5a9c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      bazel/protobuf.bzl
  2. 2
      bazel/python_rules.bzl
  3. 1
      bazel/test/python_second_test_repo/WORKSPACE
  4. 29
      bazel/test/python_second_test_repo/proto/BUILD
  5. 5
      bazel/test/python_test_repo/WORKSPACE
  6. 1
      bazel/test/python_test_repo/in_subpackage/BUILD
  7. 4
      bazel/test/python_test_repo/in_subpackage/subpackage.proto

@ -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

@ -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(

@ -0,0 +1 @@
workspace(name = "my_messages")

@ -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 {};" > $@
""",
)

@ -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",
)

@ -24,4 +24,5 @@ package(
proto_library(
name = "subpackage_proto",
srcs = ["subpackage.proto"],
deps = ["@some_other_repo//proto:my_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;
}

Loading…
Cancel
Save