Repository: kythe/kythe (go)

Issue:
Rust extractor cannot be run on macOS
The Rust extractor dynamically links to libstd and librustc_driver in the Rust compiler. When the Bazel Rust rules compile the extractor, the search paths for these libraries are prefixed with `@rpath`, which causes them to not be found when attempting to run the extractor.

Pull request description:
fix(rust_extractor): fix running the Rust extractor on macOS
Fixes #5012 

Patch:
diff --git a/kythe/rust/extractor/BUILD b/kythe/rust/extractor/BUILD
index 8ec9a20aa..37596f513 100644
--- a/kythe/rust/extractor/BUILD
+++ b/kythe/rust/extractor/BUILD
@@ -34,10 +34,9 @@ rust_binary(
 
 sh_binary(
     name = "extractor_script",
-    srcs = [":extractor_script.sh"],
+    srcs = ["extractor_script.sh"],
     data = [
         ":extractor",
-        "@bazel_tools//tools/bash/runfiles",
     ] + select({
         "@platforms//os:linux": ["@rust_linux_x86_64//:rustc_lib"],
         "@platforms//os:osx": ["@rust_darwin_x86_64//:rustc_lib"],
diff --git a/kythe/rust/extractor/extractor_script.sh b/kythe/rust/extractor/extractor_script.sh
index 2f0fe762f..5a7149a00 100755
--- a/kythe/rust/extractor/extractor_script.sh
+++ b/kythe/rust/extractor/extractor_script.sh
@@ -13,24 +13,18 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# --- begin runfiles.bash initialization ---
-set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
-# shellcheck disable=SC1090
-source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
-source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
-source "$0.runfiles/$f" 2>/dev/null || \
-source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
-source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
-{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
-# --- end runfiles.bash initialization ---
+RUNFILES_DIR="$0.runfiles"
 
 # We have to call find ourselves because rlocation doesn't support wildcards
-PATTERN="$RUNFILES_DIR/rust_*_x86_64/lib/rustlib*lib/librustc_driver-*.so"
+PATTERN="$RUNFILES_DIR/rust_*_x86_64/lib/rustlib*lib/librustc_driver-*.*"
 # Find the Rust directory first so that our next find doesn't search the
 # entire runfiles directory
 RUST_RUNFILES_LOCATION="$(find "$RUNFILES_DIR" -type d -wholename "$RUNFILES_DIR/rust_*_x86_64")"
 LOCATION="$(find "$RUST_RUNFILES_LOCATION" -wholename "$PATTERN")"
 LIB_DIRNAME="$(dirname "$LOCATION")"
 
+# Set the dylib search paths for Linux and macOS, respectively
 export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$LIB_DIRNAME"
-exec "$(rlocation io_kythe/kythe/rust/extractor/extractor)" "$@"
+export DYLD_FALLBACK_LIBRARY_PATH="${DYLD_FALLBACK_LIBRARY_PATH:+$DYLD_FALLBACK_LIBRARY_PATH:}$LIB_DIRNAME"
+
+exec "$RUNFILES_DIR/io_kythe/kythe/rust/extractor/extractor" "$@"


Test patch:
diff --git a/kythe/rust/extractor/extractor_test.bzl b/kythe/rust/extractor/extractor_test.bzl
index 15d9c5189..f94fde749 100644
--- a/kythe/rust/extractor/extractor_test.bzl
+++ b/kythe/rust/extractor/extractor_test.bzl
@@ -47,7 +47,8 @@ def _rust_extractor_test_impl(ctx):
     rustc_lib_path = paths.dirname(rustc_lib[0].short_path)
     rust_lib_path = paths.dirname(rust_lib[0].short_path)
     script = "\n".join(
-        ["export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:%s:%s" % (rustc_lib_path, rust_lib_path)] +
+        ["export DYLD_FALLBACK_LIBRARY_PATH=${DYLD_FALLBACK_LIBRARY_PATH:+$DYLD_FALLBACK_LIBRARY_PATH:}%s:%s" % (rustc_lib_path, rust_lib_path)] +
+        ["export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}%s:%s" % (rustc_lib_path, rust_lib_path)] +
         ["export SYSROOT=%s" % rust_lib_path] +
         ["export TEST_FILE=%s" % source_file.short_path] +
         ["export EXTRACTOR_PATH=%s" % extractor.short_path] +
diff --git a/tools/build_rules/verifier_test/rust_indexer_test.bzl b/tools/build_rules/verifier_test/rust_indexer_test.bzl
index 13f9daae4..7d1e2c13c 100644
--- a/tools/build_rules/verifier_test/rust_indexer_test.bzl
+++ b/tools/build_rules/verifier_test/rust_indexer_test.bzl
@@ -66,7 +66,6 @@ def _rust_extract_impl(ctx):
         outputs = [output],
         env = {
             "KYTHE_CORPUS": "test_corpus",
-            "LD_LIBRARY_PATH": paths.dirname(rustc_lib[0].path),
         },
     )
 
@@ -90,7 +89,7 @@ rust_extract = rule(
             default = "test_crate",
         ),
         "_extractor": attr.label(
-            default = Label("//kythe/rust/extractor"),
+            default = Label("//kythe/rust/extractor:extractor_script"),
             executable = True,
             cfg = "exec",
         ),