diff --git a/js/private/js_binary.sh.tpl b/js/private/js_binary.sh.tpl index ad99a00047..5d0234cb5c 100644 --- a/js/private/js_binary.sh.tpl +++ b/js/private/js_binary.sh.tpl @@ -328,7 +328,10 @@ fi # Change directory to user specified package if set if [ "${JS_BINARY__CHDIR:-}" ]; then logf_debug "changing directory to user specified package %s" "$JS_BINARY__CHDIR" - cd "$JS_BINARY__EXECROOT/${BAZEL_BINDIR:-$JS_BINARY__BINDIR}/$JS_BINARY__CHDIR" + case "$JS_BINARY__CHDIR" in + external/*) cd "$(resolve_execroot_bin_path "$JS_BINARY__CHDIR")" ;; + *) cd "$JS_BINARY__CHDIR" ;; + esac fi # Gather node options diff --git a/js/private/test/data/BUILD.bazel b/js/private/test/data/BUILD.bazel index 814afb8ed9..9c49407b0d 100644 --- a/js/private/test/data/BUILD.bazel +++ b/js/private/test/data/BUILD.bazel @@ -88,6 +88,42 @@ js_test( entry_point = "test.js", ) +# TEST: js_test(data = source file, chdir = package_name()) +# This test and the following one verify that with `chdir = package_name()` we +# are changing to the correct directory, by making sure data.json is present in +# the working directory. +js_test( + name = "chdir-source-data", + args = ["data.json"], + chdir = package_name(), + data = [":data.json"], + entry_point = "open_arg.mjs", +) + +js_test( + name = "chdir-source-data-relative", + args = ["./data.json"], + chdir = package_name(), + data = [":data.json"], + entry_point = "open_arg.mjs", +) + +# TEST: js_test(data = source file, chdir = None) with rootpath +js_test( + name = "source-data-rootpath", + args = ["$(rootpath :data.json)"], + data = [":data.json"], + entry_point = "open_arg.mjs", +) + +# TEST: js_test(data = source file, chdir = None) with execpath +js_test( + name = "source-data-execpath", + args = ["$(execpath :data.json)"], + data = [":data.json"], + entry_point = "open_arg.mjs", +) + # TEST: js_test(data = genrule()) ------------------------- js_test( name = "from-genrule", diff --git a/js/private/test/data/open_arg.mjs b/js/private/test/data/open_arg.mjs new file mode 100644 index 0000000000..6b086ffdd5 --- /dev/null +++ b/js/private/test/data/open_arg.mjs @@ -0,0 +1,2 @@ +import { readFileSync } from 'fs'; +readFileSync(process.argv[2]); diff --git a/js/private/test/snapshots/launcher.sh b/js/private/test/snapshots/launcher.sh index 7fdf331a76..ed10a9739e 100644 --- a/js/private/test/snapshots/launcher.sh +++ b/js/private/test/snapshots/launcher.sh @@ -448,7 +448,10 @@ fi # Change directory to user specified package if set if [ "${JS_BINARY__CHDIR:-}" ]; then logf_debug "changing directory to user specified package %s" "$JS_BINARY__CHDIR" - cd "$JS_BINARY__EXECROOT/${BAZEL_BINDIR:-$JS_BINARY__BINDIR}/$JS_BINARY__CHDIR" + case "$JS_BINARY__CHDIR" in + external/*) cd "$(resolve_execroot_bin_path "$JS_BINARY__CHDIR")" ;; + *) cd "$JS_BINARY__CHDIR" ;; + esac fi # Gather node options