feat: add support for runfiles library in launcher script
diff --git a/js/private/js_binary.bzl b/js/private/js_binary.bzl
index 7a85ec3..e1e5122 100644
--- a/js/private/js_binary.bzl
+++ b/js/private/js_binary.bzl
@@ -276,6 +276,9 @@
         allow_single_file = True,
         default = Label("@aspect_rules_js//js/private/node-patches:register.cjs"),
     ),
+    "_bash_runfiles_lib": attr.label(
+        default = Label("@bazel_tools//tools/bash/runfiles"),
+    ),
 }
 
 _ENV_SET = """export {var}={quoted_value}"""
@@ -507,7 +510,7 @@
             include_transitive_types = ctx.attr.include_transitive_types,
             include_npm_sources = ctx.attr.include_npm_sources,
         ),
-    ))
+    )).merge(ctx.attr._bash_runfiles_lib[DefaultInfo].default_runfiles)
 
     return struct(
         executable = launcher,
diff --git a/js/private/js_binary.sh.tpl b/js/private/js_binary.sh.tpl
index 5d0234c..8396dfc 100644
--- a/js/private/js_binary.sh.tpl
+++ b/js/private/js_binary.sh.tpl
@@ -150,6 +150,18 @@
 JS_BINARY__RUNFILES="$RUNFILES"
 export JS_BINARY__RUNFILES
 
+# --- begin runfiles.bash initialization v3 ---
+# Copy-pasted from the Bazel Bash runfiles library v3.
+set -uo pipefail; set +e; 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 v3 ---
+
 # ==============================================================================
 # Prepare to run main program
 # ==============================================================================
diff --git a/js/private/js_helpers.bzl b/js/private/js_helpers.bzl
index d4dc530..1ad0bf8 100644
--- a/js/private/js_helpers.bzl
+++ b/js/private/js_helpers.bzl
@@ -191,7 +191,7 @@
     ])
 
 def expand_rlocation_refs(value):
-    """Pre-processes $(rlocation <label>) into $$RUNFILES_DIR/$(rlocationpath <label>).
+    """Pre-processes $(rlocation <label>) into $$(rlocation $(rlocationpath <label>)).
 
     After this transformation, the standard expand_locations/expand_variables chain
     resolves the rlocationpath and converts $$ to a literal $.
@@ -209,7 +209,7 @@
         if end == -1:
             fail("Unclosed $(rlocation ...) in: " + value)
         label_str = remaining[:end]
-        result.append("$$RUNFILES_DIR/$(rlocationpath " + label_str + ")")
+        result.append("$$(rlocation $(rlocationpath " + label_str + "))")
         remaining = remaining[end + 1:]
     result.append(remaining)
     return "".join(result)
diff --git a/js/private/test/image/custom_owner_test_app.listing b/js/private/test/image/custom_owner_test_app.listing
index 02c4032..c8a7f59 100644
--- a/js/private/test/image/custom_owner_test_app.listing
+++ b/js/private/test/image/custom_owner_test_app.listing
@@ -18,3 +18,8 @@
 drwxr-xr-x  0 100    0           0 Jan  1  1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/
 -r-xr-xr-x  0 100    0         133 Jan  1  1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/node
 -r-xr-xr-x  0 100    0          20 Jan  1  1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/main.js
+drwxr-xr-x  0 100    0           0 Jan  1  1970 ./js/private/test/image/bin.runfiles/bazel_tools/
+drwxr-xr-x  0 100    0           0 Jan  1  1970 ./js/private/test/image/bin.runfiles/bazel_tools/tools/
+drwxr-xr-x  0 100    0           0 Jan  1  1970 ./js/private/test/image/bin.runfiles/bazel_tools/tools/bash/
+drwxr-xr-x  0 100    0           0 Jan  1  1970 ./js/private/test/image/bin.runfiles/bazel_tools/tools/bash/runfiles/
+-r-xr-xr-x  0 100    0       21636 Jan  1  1970 ./js/private/test/image/bin.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash
diff --git a/js/private/test/image/default_test_app.listing b/js/private/test/image/default_test_app.listing
index 55c9c7a..0c26dc7 100644
--- a/js/private/test/image/default_test_app.listing
+++ b/js/private/test/image/default_test_app.listing
@@ -18,3 +18,8 @@
 drwxr-xr-x  0 0      0           0 Jan  1  1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/
 -r-xr-xr-x  0 0      0         133 Jan  1  1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/node
 -r-xr-xr-x  0 0      0          20 Jan  1  1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/main.js
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./js/private/test/image/bin.runfiles/bazel_tools/
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./js/private/test/image/bin.runfiles/bazel_tools/tools/
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./js/private/test/image/bin.runfiles/bazel_tools/tools/bash/
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./js/private/test/image/bin.runfiles/bazel_tools/tools/bash/runfiles/
+-r-xr-xr-x  0 0      0       21636 Jan  1  1970 ./js/private/test/image/bin.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash
diff --git a/js/private/test/image/non_ascii/custom_layer_groups_test_app.listing b/js/private/test/image/non_ascii/custom_layer_groups_test_app.listing
index 6418c3c..2c18aa4 100644
--- a/js/private/test/image/non_ascii/custom_layer_groups_test_app.listing
+++ b/js/private/test/image/non_ascii/custom_layer_groups_test_app.listing
@@ -19,3 +19,8 @@
 -r-xr-xr-x  0 0      0         133 Jan  1  1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/test/image/non_ascii/bin2_node_bin/node
 -r-xr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/test/image/non_ascii/empty empty.ㄴㅅ
 -r-xr-xr-x  0 0      0          20 Jan  1  1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/test/image/non_ascii/main.js
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/bazel_tools/
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/bazel_tools/tools/
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/bazel_tools/tools/bash/
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/bazel_tools/tools/bash/runfiles/
+-r-xr-xr-x  0 0      0       21636 Jan  1  1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash
diff --git a/js/private/test/image/platform_deps/rspack_linux_arm64_test_app.listing b/js/private/test/image/platform_deps/rspack_linux_arm64_test_app.listing
index 7b3a01c..af34b0d 100644
--- a/js/private/test/image/platform_deps/rspack_linux_arm64_test_app.listing
+++ b/js/private/test/image/platform_deps/rspack_linux_arm64_test_app.listing
@@ -17,3 +17,8 @@
 drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/platform_deps/bin.runfiles/_main/js/private/test/image/platform_deps/bin_node_bin/
 -r-xr-xr-x  0 0      0         133 Jan  1  1970 ./app/js/private/test/image/platform_deps/bin.runfiles/_main/js/private/test/image/platform_deps/bin_node_bin/node
 -r-xr-xr-x  0 0      0          20 Jan  1  1970 ./app/js/private/test/image/platform_deps/bin.runfiles/_main/js/private/test/image/platform_deps/main.js
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/platform_deps/bin.runfiles/bazel_tools/
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/platform_deps/bin.runfiles/bazel_tools/tools/
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/platform_deps/bin.runfiles/bazel_tools/tools/bash/
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/platform_deps/bin.runfiles/bazel_tools/tools/bash/runfiles/
+-r-xr-xr-x  0 0      0       21636 Jan  1  1970 ./app/js/private/test/image/platform_deps/bin.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash
diff --git a/js/private/test/image/regex_edge_cases_test_app.listing b/js/private/test/image/regex_edge_cases_test_app.listing
index ebd1a34..0e21d14 100644
--- a/js/private/test/image/regex_edge_cases_test_app.listing
+++ b/js/private/test/image/regex_edge_cases_test_app.listing
@@ -19,3 +19,8 @@
 drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/
 -r-xr-xr-x  0 0      0         133 Jan  1  1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/node
 -r-xr-xr-x  0 0      0          20 Jan  1  1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/main.js
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/bin.runfiles/bazel_tools/
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/bin.runfiles/bazel_tools/tools/
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/bin.runfiles/bazel_tools/tools/bash/
+drwxr-xr-x  0 0      0           0 Jan  1  1970 ./app/js/private/test/image/bin.runfiles/bazel_tools/tools/bash/runfiles/
+-r-xr-xr-x  0 0      0       21636 Jan  1  1970 ./app/js/private/test/image/bin.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash
diff --git a/js/private/test/js_helpers_test.bzl b/js/private/test/js_helpers_test.bzl
index ade3850..8312b2e 100644
--- a/js/private/test/js_helpers_test.bzl
+++ b/js/private/test/js_helpers_test.bzl
@@ -12,7 +12,7 @@
     env = unittest.begin(ctx)
     asserts.equals(
         env,
-        "$$RUNFILES_DIR/$(rlocationpath :rspack_config)",
+        "$$(rlocation $(rlocationpath :rspack_config))",
         expand_rlocation_refs("$(rlocation :rspack_config)"),
     )
     return unittest.end(env)
@@ -21,7 +21,7 @@
     env = unittest.begin(ctx)
     asserts.equals(
         env,
-        "--config=$$RUNFILES_DIR/$(rlocationpath :my_config)",
+        "--config=$$(rlocation $(rlocationpath :my_config))",
         expand_rlocation_refs("--config=$(rlocation :my_config)"),
     )
     return unittest.end(env)
@@ -30,7 +30,7 @@
     env = unittest.begin(ctx)
     asserts.equals(
         env,
-        "$$RUNFILES_DIR/$(rlocationpath :foo):$$RUNFILES_DIR/$(rlocationpath :bar)",
+        "$$(rlocation $(rlocationpath :foo)):$$(rlocation $(rlocationpath :bar))",
         expand_rlocation_refs("$(rlocation :foo):$(rlocation :bar)"),
     )
     return unittest.end(env)
@@ -39,7 +39,7 @@
     env = unittest.begin(ctx)
     asserts.equals(
         env,
-        "$$RUNFILES_DIR/$(rlocationpath //some/pkg:target)",
+        "$$(rlocation $(rlocationpath //some/pkg:target))",
         expand_rlocation_refs("$(rlocation //some/pkg:target)"),
     )
     return unittest.end(env)
diff --git a/js/private/test/snapshots/launcher.sh b/js/private/test/snapshots/launcher.sh
index 501c8ba..72a0e6f 100644
--- a/js/private/test/snapshots/launcher.sh
+++ b/js/private/test/snapshots/launcher.sh
@@ -270,6 +270,18 @@
 JS_BINARY__RUNFILES="$RUNFILES"
 export JS_BINARY__RUNFILES
 
+# --- begin runfiles.bash initialization v3 ---
+# Copy-pasted from the Bazel Bash runfiles library v3.
+set -uo pipefail; set +e; 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 v3 ---
+
 # ==============================================================================
 # Prepare to run main program
 # ==============================================================================