rules_js icon indicating copy to clipboard operation
rules_js copied to clipboard

Give better guidelines for generating files using js_run_binary

Open mrmeku opened this issue 3 years ago • 3 comments

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.

mrmeku avatar Oct 04 '22 00:10 mrmeku

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.

matthewjh avatar Oct 04 '22 11:10 matthewjh

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

mrmeku avatar Oct 04 '22 22:10 mrmeku

We will review both options at the next team meeting.

cgrindel avatar Oct 05 '22 17:10 cgrindel

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.

jgiles avatar Jul 06 '23 14:07 jgiles