Support proto_library targets that contain '-'

Crate names must not contain '-'. So we replace any '-' by a '_' to form a valid crate name.

Bazel supports many special characters as target names: !%-@^_"#$&'()*-+,;<=>?[]{|}~/. We don't have to support all of them in Protobuf Rust, but '-' seems used widely enough and is a common alternative to '_' in naming.

PiperOrigin-RevId: 602963206
pull/15657/head
Jakob Buchgraber 10 months ago committed by Copybara-Service
parent 58b582ba31
commit 6f1d88107f
  1. 3
      rust/aspects.bzl
  2. 7
      rust/test/BUILD
  3. 7
      rust/test/rust_proto_library_unit_test/rust_proto_library_unit_test.bzl

@ -68,6 +68,9 @@ def _render_text_crate_mapping(mapping):
<one import path per line>\n <one import path per line>\n
""" """
crate_name = mapping.crate_name crate_name = mapping.crate_name
# proto_library targets may contain '-', but rust crates don't.
crate_name = crate_name.replace("-", "_")
import_paths = mapping.import_paths import_paths = mapping.import_paths
return "\n".join(([crate_name, str(len(import_paths))] + list(import_paths))) return "\n".join(([crate_name, str(len(import_paths))] + list(import_paths)))

@ -472,7 +472,8 @@ rust_upb_proto_library(
) )
proto_library( proto_library(
name = "fields_with_imported_types_proto", # Test '-' occurrences are replaced with '_'.
name = "fields-with-imported-types_proto",
testonly = True, testonly = True,
srcs = ["fields_with_imported_types.proto"], srcs = ["fields_with_imported_types.proto"],
deps = [":imported_types_proto"], deps = [":imported_types_proto"],
@ -481,7 +482,7 @@ proto_library(
cc_proto_library( cc_proto_library(
name = "fields_with_imported_types_cc_proto", name = "fields_with_imported_types_cc_proto",
testonly = True, testonly = True,
deps = [":fields_with_imported_types_proto"], deps = [":fields-with-imported-types_proto"],
) )
rust_cc_proto_library( rust_cc_proto_library(
@ -499,5 +500,5 @@ rust_upb_proto_library(
visibility = [ visibility = [
"//rust/test/shared:__subpackages__", "//rust/test/shared:__subpackages__",
], ],
deps = [":fields_with_imported_types_proto"], deps = [":fields-with-imported-types_proto"],
) )

@ -27,7 +27,7 @@ def _check_crate_mapping(actions, target_name):
target_name, target_name,
fw_actions, fw_actions,
)) ))
expected_content = """grandparent_proto expected_content = """grand_parent_proto
2 2
rust/test/rust_proto_library_unit_test/grandparent1.proto rust/test/rust_proto_library_unit_test/grandparent1.proto
rust/test/rust_proto_library_unit_test/grandparent2.proto rust/test/rust_proto_library_unit_test/grandparent2.proto
@ -174,14 +174,15 @@ def rust_proto_library_unit_test(name):
Args: Args:
name: name of the test suite""" name: name of the test suite"""
native.proto_library( native.proto_library(
name = "grandparent_proto", # Use a '-' in the target name to test that its replaced by a '_' in the crate name.
name = "grand-parent_proto",
srcs = ["grandparent1.proto", "grandparent2.proto"], srcs = ["grandparent1.proto", "grandparent2.proto"],
) )
native.proto_library( native.proto_library(
name = "parent_proto", name = "parent_proto",
srcs = ["parent.proto"], srcs = ["parent.proto"],
deps = [":grandparent_proto"], deps = [":grand-parent_proto"],
) )
native.proto_library(name = "parent2_proto", srcs = ["parent2.proto"]) native.proto_library(name = "parent2_proto", srcs = ["parent2.proto"])

Loading…
Cancel
Save