Skip to content

Commit b220178

Browse files
lberkiejona86
authored andcommitted
java_grpc_library.bzl: Work with proto_library rules using strip_import_prefix / import_prefix
In addition to this welcome functionality, this change also makes it possible to fix http://github.com/bazelbuild/bazel/issues/7157 by tolerating proto_library rules using a virtual import directory.
1 parent cc13f74 commit b220178

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

java_grpc_library.bzl

+14-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,20 @@ java_rpc_toolchain = rule(
6262
def _path_ignoring_repository(f):
6363
if len(f.owner.workspace_root) == 0:
6464
return f.short_path
65-
return f.path[f.path.find(f.owner.workspace_root) + len(f.owner.workspace_root) + 1:]
65+
66+
# Bazel creates a _virtual_imports directory in case the .proto source files
67+
# need to a accessed at a path that's different from their source path:
68+
# https://github.com/bazelbuild/bazel/blob/0.27.1/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java#L289
69+
#
70+
# In that case, the import path of the .proto file is the path relative to
71+
# the virtual imports directory of the rule in question.
72+
virtual_imports = "/_virtual_imports/"
73+
if virtual_imports in f.path:
74+
return f.path.split(virtual_imports)[1].split("/", 1)[1]
75+
else:
76+
# If |f| is a generated file, it will have "bazel-out/*/genfiles" prefix
77+
# before "external/workspace", so we need to add the starting index of "external/workspace"
78+
return f.path[f.path.find(f.owner.workspace_root) + len(f.owner.workspace_root) + 1:]
6679

6780
def _java_rpc_library_impl(ctx):
6881
if len(ctx.attr.srcs) != 1:

0 commit comments

Comments
 (0)