Skip to content

Commit

Permalink
Fix label struct in protobuf Rust starlark code.
Browse files Browse the repository at this point in the history
This fixes a starlark eval failure in rules_rust when the 1P crate renaming setting https://bazelbuild.github.io/rules_rust/rust_settings.html#rename_first_party_crates is enabled. 1P crate renaming encodes the package path into the crate name, to prevent name collisions in large monorepos.

PiperOrigin-RevId: 715396886
  • Loading branch information
Burak Emir authored and copybara-github committed Jan 14, 2025
1 parent ff5bcf5 commit 7d1b040
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions rust/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _rust_proto_library_impl(ctx):
dep_variant_info = rust_proto_info.dep_variant_infos[0]
crate_info = dep_variant_info.crate_info

# Change the crate name from the hame of the proto_library to the name of the rust_proto_library.
# Change the crate name from the name of the proto_library to the name of the rust_proto_library.
#
# When the aspect visits proto_libraries, it doesn't know and cannot deduce the name of the
# rust_proto_library (although the name of rust_proto_libraries is consistently ending with
Expand All @@ -109,7 +109,12 @@ def _rust_proto_library_impl(ctx):
toolchain = ctx.toolchains["@rules_rust//rust:toolchain_type"]
fields = {field: getattr(crate_info, field) for field in dir(crate_info)}
pkg, name = _user_visible_label(ctx).rsplit(":")
label = struct(**{"name": name, "pkg": pkg})

# Construct a label and compute the crate name.
# Package and workspace root are only relevant when 1P crate renaming is enabled.
# The current implementation of crate renaming supports only monorepos which
# means that it will only rename wen label.workspace_root is empty.
label = struct(**{"name": name, "package": pkg, "workspace_root": ""})
fields["name"] = label_to_crate_name(ctx, label, toolchain)

# These two fields present on the dir(crate_info) but break on some versions of Bazel when
Expand Down

0 comments on commit 7d1b040

Please sign in to comment.