diff --git a/java_grpc_library.bzl b/java_grpc_library.bzl index 7c1b1f2b89..2c00ee9567 100644 --- a/java_grpc_library.bzl +++ b/java_grpc_library.bzl @@ -62,7 +62,20 @@ java_rpc_toolchain = rule( def _path_ignoring_repository(f): if len(f.owner.workspace_root) == 0: return f.short_path - return f.path[f.path.find(f.owner.workspace_root) + len(f.owner.workspace_root) + 1:] + + # Bazel creates a _virtual_imports directory in case the .proto source files + # need to a accessed at a path that's different from their source path: + # https://github.com/bazelbuild/bazel/blob/0.27.1/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java#L289 + # + # In that case, the import path of the .proto file is the path relative to + # the virtual imports directory of the rule in question. + virtual_imports = "/_virtual_imports/" + if virtual_imports in f.path: + return f.path.split(virtual_imports)[1].split("/", 1)[1] + else: + # If |f| is a generated file, it will have "bazel-out/*/genfiles" prefix + # before "external/workspace", so we need to add the starting index of "external/workspace" + return f.path[f.path.find(f.owner.workspace_root) + len(f.owner.workspace_root) + 1:] def _java_rpc_library_impl(ctx): if len(ctx.attr.srcs) != 1: