rules-proto-grpc / rules_proto_grpc

Bazel rules for building Protobuf and gRPC code and libraries from proto_library targets
https://rules-proto-grpc.com
Apache License 2.0
250 stars 157 forks source link

How to use `js_grpc_node_library` with custom repo #255

Open ghost opened 1 year ago

ghost commented 1 year ago

Description

Hi, I'm using js_grpc_node_library, and I'm trying to use the deps_repo= parameter to point to a custom named repo.

I'm using rules_js, my WORKSPACE looks like this.

http_archive(
    name = "aspect_rules_js",
    sha256 = "7c50475662810ce9635fa45d718220285a8adef32b2febd8631aae62e5816353",
    strip_prefix = "rules_js-1.23.2",
    url = "https://github.com/aspect-build/rules_js/releases/download/v1.23.2/rules_js-v1.23.2.tar.gz",
)

load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")

rules_js_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(
    name = "nodejs",
    node_version = "16.15.1",
)

load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock")

npm_translate_lock(
    name = "custom_npm",
)

and then my BUILD file looks like this:

proto_library(
    name = "api_proto",
    srcs = ["api.proto"],
)

js_grpc_node_library(
    name = "protos_js",
    protos = [":api_proto"],
)

Adding deps_repo="@custom_npm" doesn't work because the dep is looked up at @custom_npm//@grpc/grpc-js which I believe is correct when using the old and now unmaintained rules_nodejs, but when using the newer rules_js the dep is placed instead at @custom_npm//:node_modules/@grpc/grpc-js (per docs https://docs-legacy.aspect.build/aspect-build/rules_js/v1.6.4/docs/npm_import-docgen.html#npm_translate_lock-link_workspace). Is there a way to make this work?

I looked at the code a bit, and there's a string replacement taking place https://github.com/rules-proto-grpc/rules_proto_grpc/blob/bbb81931a510290127471b25d31b97e8211fa401/js/js_grpc_node_library.bzl#L22 that doesn't seem ameanable, unless there is some other trick I am missing.

cfife-btig commented 1 year ago

Hey there,

I'm running into pretty much the exact same issue, albeit I'm using js_grpc_web_library. I'm still underway moving our existing project from rules_nodejs to rules_js (so I don't fully understand what solutions or methods might exist). However, it seems like the main problem is simply that node_modules is a target of the workspace now (i.e. //:node_modules), whereas before it was an external repository (i.e. @npm//grpc-web).

setting deps_repo to an empty string, at least seems to point it in the workspace, but I suspect there are other issues that haven't been uncovered. For example, even if I could point to the correct target path ( i.e. //:node_modules). Is it still going to try to package it into node_modules using the js_library rule which comes from rules_nodejs (which I don't have anymore)?

I think the main issue is just that rules_js isn't like rules_nodejs, so it's just fundamentally broken compatibility. Hopefully it's an easy patch-file fix, but I would guess there isn't an existing work around.

Kinda surprised to see only one issue about this, but maybe many people are still holding off on upgrading out of rules_nodejs, or the universe of js and grpc and bazel people is pretty small.