Skip to content

Commit

Permalink
Fix regression with src-less proto_library with 0 srcs.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 713316678
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 8, 2025
1 parent 871c434 commit a86b65d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 35 deletions.
74 changes: 39 additions & 35 deletions rust/aspects.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,26 @@ def _rust_proto_aspect_common(target, ctx, is_upb):
if RustProtoInfo in target:
return []

proto_srcs = target[ProtoInfo].direct_sources
proto_deps = getattr(ctx.rule.attr, "deps", [])
transitive_crate_mappings = []
for dep in proto_deps:
rust_proto_info = dep[RustProtoInfo]
transitive_crate_mappings.append(rust_proto_info.crate_mapping)

dep_variant_infos = []
for info in [d[RustProtoInfo].dep_variant_infos for d in proto_deps]:
dep_variant_infos += info

# If there are no srcs, then this is an alias library (which in Rust acts as a middle
# library in a dependency chain). Don't generate any Rust code for it, but do propagate the
# crate mappings.
if not proto_srcs:
return [RustProtoInfo(
dep_variant_infos = dep_variant_infos,
crate_mapping = depset(transitive = transitive_crate_mappings),
)]

proto_lang_toolchain = ctx.attr._proto_lang_toolchain[proto_common.ProtoLangToolchainInfo]
cc_toolchain = find_cpp_toolchain(ctx)
toolchain = ctx.toolchains["@rules_rust//rust:toolchain_type"]
Expand All @@ -309,13 +329,6 @@ def _rust_proto_aspect_common(target, ctx, is_upb):
unsupported_features = ctx.disabled_features,
)

proto_srcs = target[ProtoInfo].direct_sources
proto_deps = getattr(ctx.rule.attr, "deps", [])
transitive_crate_mappings = []
for dep in proto_deps:
rust_proto_info = dep[RustProtoInfo]
transitive_crate_mappings.append(rust_proto_info.crate_mapping)

mapping_for_current_target = depset(transitive = transitive_crate_mappings)
crate_mapping_file = _register_crate_mapping_write_action(
target.label.name,
Expand Down Expand Up @@ -354,36 +367,27 @@ def _rust_proto_aspect_common(target, ctx, is_upb):
cc_info = runtime[CcInfo] if CcInfo in runtime else None,
build_info = None,
)
dep_variant_info_for_native_gencode = DepVariantInfo(cc_info = thunks_cc_info)

dep_variant_infos = []
for info in [d[RustProtoInfo].dep_variant_infos for d in proto_deps]:
dep_variant_infos += info
dep_variant_info_for_native_gencode = DepVariantInfo(cc_info = thunks_cc_info)

if proto_srcs:
dep_variant_info = _compile_rust(
ctx = ctx,
attr = ctx.rule.attr,
src = entry_point_rs_output,
extra_srcs = rs_gencode,
deps = [dep_variant_info_for_runtime, dep_variant_info_for_native_gencode] + dep_variant_infos,
runtime = runtime,
)
return [RustProtoInfo(
dep_variant_infos = [dep_variant_info],
crate_mapping = depset(
direct = [CrateMappingInfo(
crate_name = label_to_crate_name(ctx, target.label, toolchain),
import_paths = tuple([get_import_path(f) for f in proto_srcs]),
)],
transitive = transitive_crate_mappings,
),
)]
else:
return [RustProtoInfo(
dep_variant_infos = dep_variant_infos,
crate_mapping = depset(transitive = transitive_crate_mappings),
)]
dep_variant_info = _compile_rust(
ctx = ctx,
attr = ctx.rule.attr,
src = entry_point_rs_output,
extra_srcs = rs_gencode,
deps = [dep_variant_info_for_runtime, dep_variant_info_for_native_gencode] + dep_variant_infos,
runtime = runtime,
)
return [RustProtoInfo(
dep_variant_infos = [dep_variant_info],
crate_mapping = depset(
direct = [CrateMappingInfo(
crate_name = label_to_crate_name(ctx, target.label, toolchain),
import_paths = tuple([get_import_path(f) for f in proto_srcs]),
)],
transitive = transitive_crate_mappings,
),
)]

def _make_proto_library_aspect(is_upb):
return aspect(
Expand Down
31 changes: 31 additions & 0 deletions rust/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,34 @@ rust_cc_proto_library(
testonly = True,
deps = ["unittest_proto3_optional_proto"],
)

proto_library(
name = "srcsless_library_test_child_proto",
testonly = True,
srcs = ["srcsless_library_test_child.proto"],
)

proto_library(
name = "srcsless_library_test_alias_proto",
testonly = True,
deps = [":srcsless_library_test_child_proto"],
)

proto_library(
name = "srcsless_library_test_parent_proto",
testonly = True,
srcs = ["srcsless_library_test_parent.proto"],
deps = [":srcsless_library_test_alias_proto"],
)

rust_cc_proto_library(
name = "srcsless_library_test_parent_cpp_rust_proto",
testonly = True,
deps = [":srcsless_library_test_parent_proto"],
)

rust_upb_proto_library(
name = "srcsless_library_test_parent_upb_rust_proto",
testonly = True,
deps = [":srcsless_library_test_parent_proto"],
)
7 changes: 7 additions & 0 deletions rust/test/srcsless_library_test_child.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
edition = "2023";

package srcsless_library_test;

option java_multiple_files = true;

message Child {}
14 changes: 14 additions & 0 deletions rust/test/srcsless_library_test_parent.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
edition = "2023";

package srcsless_library_test;

// The purpose of this proto is for a test where a proto_library target that has
// no srcs (an "alias library") can be used in the middle between this parent
// target and this imported file.
import "rust/test/srcsless_library_test_child.proto";

option java_multiple_files = true;

message Parent {
Child child = 1;
}

0 comments on commit a86b65d

Please sign in to comment.