From 6f1d88107f268b8ebdad6690d116e74c403e366e Mon Sep 17 00:00:00 2001 From: Jakob Buchgraber Date: Wed, 31 Jan 2024 00:37:25 -0800 Subject: [PATCH] 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 --- rust/aspects.bzl | 3 +++ rust/test/BUILD | 7 ++++--- .../rust_proto_library_unit_test.bzl | 7 ++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/rust/aspects.bzl b/rust/aspects.bzl index 71196ee855..4f3fc3823d 100644 --- a/rust/aspects.bzl +++ b/rust/aspects.bzl @@ -68,6 +68,9 @@ def _render_text_crate_mapping(mapping): \n """ 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 return "\n".join(([crate_name, str(len(import_paths))] + list(import_paths))) diff --git a/rust/test/BUILD b/rust/test/BUILD index 2b3285de3d..a9b8fd083c 100644 --- a/rust/test/BUILD +++ b/rust/test/BUILD @@ -472,7 +472,8 @@ rust_upb_proto_library( ) proto_library( - name = "fields_with_imported_types_proto", + # Test '-' occurrences are replaced with '_'. + name = "fields-with-imported-types_proto", testonly = True, srcs = ["fields_with_imported_types.proto"], deps = [":imported_types_proto"], @@ -481,7 +482,7 @@ proto_library( cc_proto_library( name = "fields_with_imported_types_cc_proto", testonly = True, - deps = [":fields_with_imported_types_proto"], + deps = [":fields-with-imported-types_proto"], ) rust_cc_proto_library( @@ -499,5 +500,5 @@ rust_upb_proto_library( visibility = [ "//rust/test/shared:__subpackages__", ], - deps = [":fields_with_imported_types_proto"], + deps = [":fields-with-imported-types_proto"], ) diff --git a/rust/test/rust_proto_library_unit_test/rust_proto_library_unit_test.bzl b/rust/test/rust_proto_library_unit_test/rust_proto_library_unit_test.bzl index 360f3c9e4e..592c0f4268 100644 --- a/rust/test/rust_proto_library_unit_test/rust_proto_library_unit_test.bzl +++ b/rust/test/rust_proto_library_unit_test/rust_proto_library_unit_test.bzl @@ -27,7 +27,7 @@ def _check_crate_mapping(actions, target_name): target_name, fw_actions, )) - expected_content = """grandparent_proto + expected_content = """grand_parent_proto 2 rust/test/rust_proto_library_unit_test/grandparent1.proto rust/test/rust_proto_library_unit_test/grandparent2.proto @@ -174,14 +174,15 @@ def rust_proto_library_unit_test(name): Args: name: name of the test suite""" 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"], ) native.proto_library( name = "parent_proto", srcs = ["parent.proto"], - deps = [":grandparent_proto"], + deps = [":grand-parent_proto"], ) native.proto_library(name = "parent2_proto", srcs = ["parent2.proto"])