Skip to content

Commit

Permalink
Perform - to _ replacement in test output names
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
illicitonion committed Dec 8, 2020
1 parent 5448421 commit 2809117
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
5 changes: 3 additions & 2 deletions cargo/cargo_build_script.bzl
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ rust_library(
)

rust_test(
name = "hello_lib_test",
name = "hello-lib-test",
crate = ":hello_lib",
)

Expand Down
4 changes: 3 additions & 1 deletion proto/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 7 additions & 4 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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("-", "_")

0 comments on commit 2809117

Please sign in to comment.