Skip to content

Commit

Permalink
use absolute value of hash function to determine crate name (#1064)
Browse files Browse the repository at this point in the history
  • Loading branch information
krasimirgg committed Dec 14, 2021
1 parent c1274b6 commit 5a79d72
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
7 changes: 6 additions & 1 deletion rust/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ def determine_output_hash(crate_root):
Returns:
str: A string representation of the hash.
"""
return repr(hash(crate_root.path))

# Take the absolute value of hash() since it could be negative.
h = hash(crate_root.path)
if h < 0:
h = -h
return repr(h)

def get_preferred_artifact(library_to_link):
"""Get the first available library to link from a LibraryToLink object.
Expand Down
2 changes: 1 addition & 1 deletion test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load(

rule_test(
name = "hello_lib_rule_test",
generates = ["libhello_lib--145651613.rlib"],
generates = ["libhello_lib-145651613.rlib"],
rule = "//test/rust:hello_lib",
)

Expand Down
28 changes: 28 additions & 0 deletions test/unit/crate_name/crate_name_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ def _invalid_custom_crate_name_test_impl(ctx):
asserts.expect_failure(env, "contains invalid character(s): -")
return analysistest.end(env)

def _slib_library_name_test_impl(ctx):
"""Regression test for extra-filename.
Checks that the extra hash value appended to the library filename only
contains one dash. Previously, the hash for `slib` was negative,
resulting in an extra dash in the filename (--codegen_extra_filename=--517943325).
Args:
ctx: rule context.
"""
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
assert_argv_contains(env, tut.actions[0], "--codegen=extra-filename=-517943325")
return analysistest.end(env)

default_crate_name_library_test = analysistest.make(
_default_crate_name_library_test_impl,
)
Expand All @@ -82,6 +97,9 @@ invalid_custom_crate_name_test = analysistest.make(
_invalid_custom_crate_name_test_impl,
expect_failure = True,
)
slib_library_name_test = analysistest.make(
_slib_library_name_test_impl,
)

def _crate_name_test():
rust_library(
Expand Down Expand Up @@ -130,6 +148,16 @@ def _crate_name_test():
tags = ["manual", "norustfmt"],
)

rust_library(
name = "slib",
srcs = ["slib.rs"],
)

slib_library_name_test(
name = "slib_library_name_test",
target_under_test = ":slib",
)

default_crate_name_library_test(
name = "default_crate_name_library_test",
target_under_test = ":default-crate-name-library",
Expand Down
1 change: 1 addition & 0 deletions test/unit/crate_name/slib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 5a79d72

Please sign in to comment.