rules_js icon indicating copy to clipboard operation
rules_js copied to clipboard

generated bin rule expects all files to be in the same root folder?

Open longlho opened this issue 2 years ago • 3 comments

I'm trying to declare a simple binary that runs prettier

My directory looks like this

.prettierrc.json
BUILD
packages
|- foo
|- foo/bar.ts
|- foo/BUILD

root BUILD file:

exports_files(
    [  
        ".prettierrc.json",
    ],
    visibility = ["//:__subpackages__"],
)

foo/BUILD file

load("@npm//prettier:package_json.bzl", prettier_bin = "bin")
prettier_bin.prettier(
        name ="prettier",
        srcs = [
            "bar.ts"
            "//:.prettierrc.json",
        ],
        args = [
            "--config",
            "$(rootpath //:.prettierrc.json)",
            "--loglevel",
            "warn",
            "--write",
            "$(rootpaths bar.ts)",
        ],
        visibility = [
            "//:__pkg__",
        ],
    )

and I got

Error in fail:
Expected to find file .prettierrc.json in //foo, but instead it is in //.

Looks like it attempted to copy_to_bin .prettierrc.json, if I set copy_srcs_to_bin = False then I got

Traceback (most recent call last):
	File "/private/var/tmp/_bazel_long.ho/a2216e06a77a9425f6dcd5fd195f64d5/external/aspect_bazel_lib/lib/private/run_binary.bzl", line 46, column 20, in _impl
		ctx.actions.run(
Error in run: param 'outputs' may not be empty

What's the recommendation to get past this?

longlho avatar Jun 27 '22 01:06 longlho

you have two different problems

  1. We copy all inputs to the output tree, but Bazel only lets us copy within the same Bazel package. So the prettierrc.json file should have a separate copy_to_bin target in the root package where it lives. I'd expect you would have gotten this useful error message though: https://github.com/aspect-build/bazel-lib/commit/2b50f99ab34a66775d017b45b4a57f0fac8314e3 - could you check if you're using a version of aspect_bazel_lib that includes that fix?
  2. param 'outputs' may not be empty is because you used prettier which is a build action, and has to produce some outputs. I'll improve the error message in that case. you probably meant to use prettier_bin.prettier_binary which is meant for use with bazel run.

alexeagle avatar Jun 27 '22 19:06 alexeagle

https://github.com/aspect-build/bazel-lib/pull/175 and https://github.com/aspect-build/rules_js/pull/253 provide better error messages.

alexeagle avatar Jun 27 '22 19:06 alexeagle

gotcha thanks a lot!

longlho avatar Jul 04 '22 01:07 longlho