Give better guidelines for generating files using js_run_binary
The current working directory for js_run_binary is within bazel-out/k8-fastbuild/bin. This is all well and good, but it conflicts with how bazel expands output paths when a user inputs an argument of the form --output-path=$@
When the tool being run tries to create a file it will try to create the file at the path bazel-out/k8-fastbuild/bin/bazel-out/k8-fastbuild/bin/PATH-TO-FILE
User's need to know that they need to strip off the prefix from the expanded $@ argument in order to run a successful action. Usually this will look something like ../../../$@
For example:
load("@npm//:sass/package_json.bzl", sass_bin = "bin")
sass_bin.sass(
name = "css",
srcs = ["styles.scss"],
outs = ["styles.css"],
args = [
"$(execpath {})".format("styles.scss"),
"../../../$@",
],
visibility = ["//visibility:private"],
)
This seems problematic and unintuitive.
It is mentioned here, but I agree with you it is very unintuitive.
Another gotcha not currently mentioned in the docs, is that if you pass $(execpath :a) where a is a source file included in srcs or data, then the execpath for a will not include the bazel-bin segments, and therefore not need ../../../, and work just fine because aspect-bazel-lib will copy it to the bin-dir.
Can the rules (either in starlark or the .sh wrapper) re-root these paths for us so developers don't need to care? This seems like an implementation detail leaking out.
Ah, thanks @matthewjh , I completely missed that section of the docs. Maybe the answer here is just to add this bit of documentation to more places. If it was also contained within https://github.com/aspect-build/rules_js/blob/main/docs/js_run_binary.md#js_run_binary-args I wouldn't have missed it
We will review both options at the next team meeting.
I have found it easiest NOT to set chdir = package_name(), and to use $(rootpath :src_target) to reference inputs. So, the js_binary executes at the root of the Bazel output directory, and paths are relative to that root.