diff --git a/proto/proto.bzl b/proto/proto.bzl index 23a1113de3..87d8d2a184 100644 --- a/proto/proto.bzl +++ b/proto/proto.bzl @@ -44,46 +44,16 @@ load("//rust:private/utils.bzl", "find_toolchain") RustProtoProvider = provider( fields = { - "proto_sources": "List[string]: list of source paths of protos", - "transitive_proto_sources": "depset[string]", + "proto_sources": "List[File]: list of source paths of protos", + "transitive_proto_sources": "depset[File]", }, ) -def _compute_proto_source_path(file, source_root_attr): - """Take the short path of file and make it suitable for protoc.""" - - # For proto, they need to be requested with their absolute name to be - # compatible with the descriptor_set passed by proto_library. - # I.e. if you compile a protobuf at @repo1//package:file.proto, the proto - # compiler would generate a file descriptor with the path - # `package/file.proto`. Since we compile from the proto descriptor, we need - # to pass the list of descriptors and the list of path to compile. - # For the precedent example, the file (noted `f`) would have - # `f.short_path` returns `external/repo1/package/file.proto`. - # In addition, proto_library can provide a proto_source_path to change the base - # path, which should a be a prefix. - path = file.short_path - - # Strip external prefix. - path = path.split("/", 2)[2] if path.startswith("../") else path - - # Strip source_root. - if path.startswith(source_root_attr): - return path[len(source_root_attr):] - else: - return path - def _rust_proto_aspect_impl(target, ctx): if ProtoInfo not in target: return None - source_root = ctx.rule.attr.proto_source_root - if source_root and source_root[-1] != "/": - source_root += "/" - sources = [ - _compute_proto_source_path(f, source_root) - for f in target[ProtoInfo].direct_sources - ] + sources = target[ProtoInfo].direct_sources transitive_sources = [ f[RustProtoProvider].transitive_proto_sources for f in ctx.rule.attr.deps @@ -113,9 +83,6 @@ def _gen_lib(ctx, grpc, srcs, lib): content.append("pub use %s_grpc::*;" % _generated_file_stem(f)) ctx.actions.write(lib, "\n".join(content)) -def _expand_provider(lst, provider): - return [getattr(el, provider) for el in lst if hasattr(el, provider)] - def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, grpc, compile_deps): # Create all the source in a specific folder proto_toolchain = ctx.toolchains["@io_bazel_rules_rust//proto:toolchain"] @@ -162,7 +129,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, grpc, def _rust_protogrpc_library_impl(ctx, grpc): """Implementation of the rust_(proto|grpc)_library.""" - proto = _expand_provider(ctx.attr.deps, "proto") + proto = [d[ProtoInfo] for d in ctx.attr.deps if d[ProtoInfo]] transitive_sources = [ f[RustProtoProvider].transitive_proto_sources for f in ctx.attr.deps @@ -260,7 +227,7 @@ rust_grpc_library = rule( ), "rust_deps": attr.label_list( doc = "The crates the generated library depends on.", - default = GRPC_COMPILE_DEPS + default = GRPC_COMPILE_DEPS, ), "_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"), "_optional_output_wrapper": attr.label( diff --git a/proto/toolchain.bzl b/proto/toolchain.bzl index 2214bb5013..1b3ec7233d 100644 --- a/proto/toolchain.bzl +++ b/proto/toolchain.bzl @@ -15,10 +15,13 @@ """Toolchain for compiling rust stubs from protobug and gRPC.""" def generated_file_stem(f): - basename = f.rsplit("/", 2)[-1] + basename = f.short_path.rsplit("/", 2)[-1] basename = basename.replace("-", "_") return basename.rsplit(".", 2)[0] +def _to_basename(a): + return a.basename + def rust_generate_proto( ctx, transitive_descriptor_sets, @@ -83,7 +86,7 @@ def rust_generate_proto( format_joined = "--descriptor_set_in=%s", ) - args.add_all(protos) + args.add_all(imports, map_each = _to_basename) ctx.actions.run( inputs = depset( transitive = [ diff --git a/test/proto/BUILD b/test/proto/BUILD index bcb51d2dc2..ec9bbe904b 100644 --- a/test/proto/BUILD +++ b/test/proto/BUILD @@ -3,14 +3,14 @@ load("@io_bazel_rules_rust//proto:proto.bzl", "rust_proto_library") proto_library( name = "a_proto", srcs = ["a.proto"], - proto_source_root = "test/proto", + strip_import_prefix = "/test/proto", ) proto_library( name = "b_proto", srcs = ["b.proto"], deps = [":a_proto"], - proto_source_root = "test/proto", + strip_import_prefix = "/test/proto", ) rust_proto_library(