Don't count an empty start path as a directory in relativize() (#4503)
`_path_parts("")` returns `[""]`, because `"".split("/")` is `[""]`. So
`relativize(path, "")` counts the empty start as one directory and
returns `../path` instead of `path`.
`_compute_rpaths` passes the dirname of the output's `short_path` as the
start. For a shared library in the root package that dirname is `""`, so
the runfiles RPATH it computes points one level above the runfiles tree,
and the binary can't find its `_solib_*` libraries through it.
This drops empty components along with `.` and adds unit tests for
`relativize()`. Without the fix the new test fails with `Expected
"_solib_k8", but got "../_solib_k8"`.
An end-to-end reproduction is in [this
comment](https://github.com/bazelbuild/rules_rust/pull/4503#issuecomment-7324953997):
a root-package binary that links a shared library built in another
configuration fails to load it when run from its runfiles tree with
symlinks resolved.
diff --git a/rust/private/utils.bzl b/rust/private/utils.bzl
index 476651a..c007da4 100644
--- a/rust/private/utils.bzl
+++ b/rust/private/utils.bzl
@@ -126,7 +126,7 @@
return relative_path
def _path_parts(path):
- """Takes a path and returns a list of its parts with all "." elements removed.
+ """Takes a path and returns a list of its parts with all "." and empty elements removed.
The main use case of this function is if one of the inputs to relativize()
is a relative path, such as "./foo".
@@ -135,10 +135,13 @@
path (str): A string representing a unix path
Returns:
- list: A list containing the path parts with all "." elements removed.
+ list: A list containing the path parts with all "." and empty elements removed.
"""
path_parts = path.split("/")
- return [part for part in path_parts if part != "."]
+
+ # "".split("/") is [""], so dropping only "." would count an empty path
+ # (the root package) as one directory.
+ return [part for part in path_parts if part not in (".", "")]
def get_lib_name_default(lib):
"""Returns the name of a library artifact.
diff --git a/test/unit/utils/utils_test.bzl b/test/unit/utils/utils_test.bzl
index 7e995a1..72c98e1 100644
--- a/test/unit/utils/utils_test.bzl
+++ b/test/unit/utils/utils_test.bzl
@@ -3,7 +3,7 @@
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
# buildifier: disable=bzl-visibility
-load("//rust/private:utils.bzl", "decode_crate_name_as_label_for_testing", "encode_label_as_crate_name", "encode_raw_string_for_testing", "should_encode_label_in_crate_name", "substitutions_for_testing")
+load("//rust/private:utils.bzl", "decode_crate_name_as_label_for_testing", "encode_label_as_crate_name", "encode_raw_string_for_testing", "relativize", "should_encode_label_in_crate_name", "substitutions_for_testing")
def _encode_label_as_crate_name_test_impl(ctx):
env = unittest.begin(ctx)
@@ -121,6 +121,22 @@
encode_raw_string_test = unittest.make(_encode_raw_string_test_impl)
decode_test = unittest.make(_decode_test_impl)
+def _relativize_test_impl(ctx):
+ env = unittest.begin(ctx)
+
+ asserts.equals(env, "baz", relativize("foo/bar/baz", "foo/bar"))
+ asserts.equals(env, "../qux", relativize("foo/qux", "foo/bar"))
+ asserts.equals(env, "baz", relativize("./foo/bar/baz", "foo/bar"))
+
+ # An empty start is the root package: no "../" for it.
+ asserts.equals(env, "_solib_k8", relativize("_solib_k8", ""))
+ asserts.equals(env, "foo/bar", relativize("foo/bar", ""))
+ asserts.equals(env, "../../", relativize("", "foo/bar"))
+
+ return unittest.end(env)
+
+relativize_test = unittest.make(_relativize_test_impl)
+
def utils_test_suite(name):
unittest.suite(
name,
@@ -129,4 +145,5 @@
substitutions_concatenate_test,
encode_raw_string_test,
decode_test,
+ relativize_test,
)