rules_js icon indicating copy to clipboard operation
rules_js copied to clipboard

Demonstrate usage with protocol buffers and grpc

Open alexeagle opened this issue 1 year ago • 2 comments

Show https://rules-proto-grpc.com/en/latest/lang/js.html and/or https://github.com/stackb/rules_proto/tree/master/rules/nodejs examples in this repo.

alexeagle avatar Aug 19 '22 19:08 alexeagle

This would be great. I'm having some trouble getting this to work, where it did in the old rules_js repo. I defined a custom plugin:

tools/bazel/ts_proto_compile/BUILD.bazel:

load("@rules_proto_grpc//:defs.bzl", "proto_plugin")

proto_plugin(
    name = "ts_proto_compile",
    outputs = ["{protopath}.ts"],
    protoc_plugin_name = "ts_proto",
    tool = "@npm//ts-proto/bin:protoc-gen-ts_proto",
    visibility = ["//visibility:public"],
)

tools/bazel/ts_proto_compile/def.bzl:


load(
    "@rules_proto_grpc//:defs.bzl",
    "ProtoPluginInfo",
    "proto_compile_attrs",
    "proto_compile_impl",
)

ts_proto_compile = rule(
    implementation = proto_compile_impl,
    attrs = dict(
        proto_compile_attrs,
        _plugins = attr.label_list(
            providers = [ProtoPluginInfo],
            default = [
                Label("//tools/bazel/ts_proto_compile"),
            ],
            doc = "List of protoc plugins to apply",
        ),
    ),
    toolchains = [
        str(Label("@rules_proto_grpc//protobuf:toolchain_type")),
    ],
)

Then using it like:

load("//tools/bazel/ts_proto_compile:defs.bzl", "ts_proto_compile")

ts_proto_compile(
    name = "google_ts",
    protos = ["@com_google_protobuf//:any_proto"],
)

However, changing to the new aspect-build rules_js repo, and adding the new tool path:

   tool = "//:node_modules/ts-proto/protoc-gen-ts_proto",

gives the error /usr/bin/env: 'node': No such file or directory:

INFO: Invocation ID: 7955c9de-0498-43e7-868f-7b323d996ac2
INFO: Analyzed target //common/proto/google:google_lib (25 packages loaded, 186 targets configured).
INFO: Found 1 target...
ERROR: /home/sasha/paypa-stack/common/proto/google/BUILD.bazel:8:17: Compiling protoc outputs for ts_proto_compile plugin on target //common/proto/google:google_lib failed: (Exit 1): bash failed: error executing command /bin/bash -c 'mkdir -p '\''bazel-out/k8-fastbuild/bin/common/proto/google/_rpg_premerge_google_lib'\'' && external/com_google_protobuf_protoc_linux_x86_64/bin/protoc $@' '' ... (remaining 11 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
/usr/bin/env: 'node': No such file or directory
--ts_proto_out: protoc-gen-ts_proto: Plugin failed with status code 127.
Target //common/proto/google:google_lib failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.447s, Critical Path: 0.98s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully

So, it seems node is no longer available to the toolchain, despite it being the same binary?

codersasha avatar Sep 07 '22 04:09 codersasha

I think I can see part of the issue - the file is actually a shell script to run node. Setting the tool to the new form of js_binary should fix it:

load("@npm//:ts-proto/package_json.bzl", "bin")

bin.protoc_gen_ts_proto_binary(
    name = "protoc-gen-ts_proto",
)

proto_plugin(
    ...
    tool = ":protoc-gen-ts_proto",
)

except, this gives an error about $(BINDIR):

FATAL: aspect_rules_js[js_binary]: BAZEL_BINDIR must be set in environment to the makevar $(BINDIR) in js_binary build actions (which run in the execroot) so that build actions can change directories to always run out of the root of the Bazel output tree. See https://docs.bazel.build/versions/main/be/make-variables.html#predefined_variables. This is automatically set by 'js_run_binary' (https://github.com/aspect-build/rules_js/blob/main/docs/js_run_binary.md) which is the recommended rule to use for using a js_binary as the tool of a build action. If this is not a build action you can set the BAZEL_BINDIR to '.' instead to supress this error. For more context on this design decision, please read the aspect_rules_js README https://github.com/aspect-build/rules_js/tree/dbb5af0d2a9a2bb50e4cf4a96dbc582b27567155#running-nodejs-programs.
--ts_proto_out: protoc-gen-ts_proto: Plugin failed with status code 1.

Adding BAZEL_BINDIR=$(BINDIR) to the command sets it to <derived root>, since @rules_proto_grpc does proto generation in the output directory.

codersasha avatar Sep 08 '22 04:09 codersasha