Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use absolute value of hash function to determine crate name #1064

Merged
merged 3 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 22 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,15 @@ def _invalid_custom_crate_name_test_impl(ctx):
asserts.expect_failure(env, "contains invalid character(s): -")
return analysistest.end(env)

# Regression test to check 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).
def _slib_library_name_test_impl(ctx):
krasimirgg marked this conversation as resolved.
Show resolved Hide resolved
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 +91,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 +142,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 @@