From 2809117a9261a5aee033b91c7d15fac72ac3d304 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Tue, 8 Dec 2020 15:22:40 +0000 Subject: [PATCH] Perform - to _ replacement in test output names Right now if you have a rust_test/benchmark which has -s in the name, you get an error that the output file wasn't created by the action. This renames a rust_test in examples to hit the bug, and then fixes it. --- cargo/cargo_build_script.bzl | 5 +++-- examples/hello_lib/BUILD | 2 +- proto/toolchain.bzl | 4 +++- rust/private/rust.bzl | 11 +++++++---- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/cargo/cargo_build_script.bzl b/cargo/cargo_build_script.bzl index 171faa2f11..55378a569e 100644 --- a/cargo/cargo_build_script.bzl +++ b/cargo/cargo_build_script.bzl @@ -1,6 +1,7 @@ # buildifier: disable=module-docstring load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "C_COMPILE_ACTION_NAME") load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") +load("@io_bazel_rules_rust//rust:private/rust.bzl", "name_to_crate_name") load("@io_bazel_rules_rust//rust:private/rustc.bzl", "BuildInfo", "DepInfo", "get_cc_toolchain", "get_compilation_mode_opts", "get_linker_and_args") load("@io_bazel_rules_rust//rust:private/utils.bzl", "expand_locations", "find_toolchain") load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary") @@ -51,7 +52,7 @@ def _build_script_impl(ctx): crate_name = ctx.label.name if crate_name.endswith("_build_script"): crate_name = crate_name.replace("_build_script", "") - crate_name = crate_name.replace("_", "-") + crate_name = name_to_crate_name(crate_name) toolchain_tools = [ # Needed for rustc to function. @@ -107,7 +108,7 @@ def _build_script_impl(ctx): env["AR"] = ar_executable for f in ctx.attr.crate_features: - env["CARGO_FEATURE_" + f.upper().replace("-", "_")] = "1" + env["CARGO_FEATURE_" + name_to_crate_name(f).upper()] = "1" env.update(expand_locations( ctx, diff --git a/examples/hello_lib/BUILD b/examples/hello_lib/BUILD index 52a6564a86..717c91e971 100644 --- a/examples/hello_lib/BUILD +++ b/examples/hello_lib/BUILD @@ -66,7 +66,7 @@ rust_library( ) rust_test( - name = "hello_lib_test", + name = "hello-lib-test", crate = ":hello_lib", ) diff --git a/proto/toolchain.bzl b/proto/toolchain.bzl index 46368e82ad..67ccbe56ef 100644 --- a/proto/toolchain.bzl +++ b/proto/toolchain.bzl @@ -14,9 +14,11 @@ """Toolchain for compiling rust stubs from protobuf and gRPC.""" +load("@io_bazel_rules_rust//rust:private/rust.bzl", "name_to_crate_name") + def generated_file_stem(f): basename = f.rsplit("/", 2)[-1] - basename = basename.replace("-", "_") + basename = name_to_crate_name(basename) return basename.rsplit(".", 2)[0] def rust_generate_proto( diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 01a3a39786..9f01f7d1ab 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -153,7 +153,7 @@ def _rust_library_impl(ctx): # Determine unique hash for this rlib output_hash = determine_output_hash(crate_root) - crate_name = ctx.label.name.replace("-", "_") + crate_name = name_to_crate_name(ctx.label.name) rust_lib_name = _determine_lib_name( crate_name, ctx.attr.crate_type, @@ -191,7 +191,7 @@ def _rust_binary_impl(ctx): list: A list of providers. See `rustc_compile_action` """ toolchain = find_toolchain(ctx) - crate_name = ctx.label.name.replace("-", "_") + crate_name = name_to_crate_name(ctx.label.name) output = ctx.actions.declare_file(ctx.label.name + toolchain.binary_ext) @@ -228,7 +228,7 @@ def _rust_test_common(ctx, toolchain, output): """ _assert_no_deprecated_attributes(ctx) - crate_name = ctx.label.name.replace("-", "_") + crate_name = name_to_crate_name(ctx.label.name) if ctx.attr.crate: # Target is building the crate in `test` config @@ -282,7 +282,7 @@ def _rust_test_impl(ctx): toolchain = find_toolchain(ctx) output = ctx.actions.declare_file( - ctx.label.name + toolchain.binary_ext, + name_to_crate_name(ctx.label.name) + toolchain.binary_ext, ) return _rust_test_common(ctx, toolchain, output) @@ -938,3 +938,6 @@ rust_benchmark( Run the benchmark test using: `bazel run //fibonacci:fibonacci_bench`. """, ) + +def name_to_crate_name(name): + return name.replace("-", "_")