bazelbuild / rules_nodejs

NodeJS toolchain for Bazel.

Home Page:https://bazelbuild.github.io/rules_nodejs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error running nodejs_binary from sh_binary

emspishak opened this issue · comments

🐞 bug report

Affected Rule

The issue is caused by the rule: nodejs_binary

Is this a regression?

Not that I know of

Description

Adding a nodejs_binary as a data dependency of a sh_binary target and then trying to run that nodejs_binary from the shell script results in an error:
ERROR: cannot find build_bazel_rules_nodejs/third_party/github.com/bazelbuild/bazel/tools/bash/runfiles/runfiles.bash

🔬 Minimal Reproduction

See https://github.com/emspishak/nodejs_sh_binary_bug . Clone that repository and run

bazel run //my_project:RunNode

to see the error.

🔥 Exception or Error

ERROR: cannot find build_bazel_rules_nodejs/third_party/github.com/bazelbuild/bazel/tools/bash/runfiles/runfiles.bash

🌍 Your Environment

Operating System:

macOS 12.4

Output of bazel version:

Bazelisk version: development
Build label: 5.2.0
Build target: bazel-out/darwin-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue Jun 7 16:11:03 2022 (1654618263)
Build timestamp: 1654618263
Build timestamp as int: 1654618263

Rules_nodejs version:

(Please check that you have matching versions between WORKSPACE file and @bazel/* npm packages.)

5.5.2

Anything else relevant?

The missing file (build_bazel_rules_nodejs/third_party/github.com/bazelbuild/bazel/tools/bash/runfiles/runfiles.bash) looks like it exists, but is just in a directory called external, so it exists at the same path, but you need to add external/ to the beginning: external/build_bazel_rules_nodejs/third_party/github.com/bazelbuild/bazel/tools/bash/runfiles/runfiles.bash

I have a workaround rule here: emspishak/nodejs_sh_binary_bug#1

I'm afraid I don't have a solution, but I am currently facing a similar issue--trying to run the protoc-gen-ts_proto wrapper script gives the same error. What's slightly odd is that I have successfully run the protoc-gen-ts_proto binary from a rule before, which makes me think that we're doing something wrong.

Thank you for adding your workaround.

This has been solved in rules_js (https://github.com/aspect-build/rules_js) where you can run a js_binary from an sh_test or a genrule. We just released 1.0.0 of the new rule set https://blog.aspect.dev/rulesjs-launch.

thanks @aptenodytes-forsteri
your workaround works great - this should be merged

I ran into this issue and the fix I found was kind of hacky:

Instead of running the nodejs_binary script directly, do bash -c "source \"${nodejs_binary_script}\"" "$0" "${additional_args[@]}"

the script was failing because it was inspecting $0, and $0 changes if you invoke the shell script directly. With bash -c, you can set $0 to be the same as the source script's

This issue has been automatically marked as stale because it has had no recent activity. It will be closed if no further activity occurs in 30 days. Note as of rules_nodejs v6 the rules_nodejs repository contains only the core nodejs toolchain and @bazel/runfiles package. All rulesets are removed and unmaintained. For alternate rulesets suggestions include https://github.com/aspect-build/rules_js, https://github.com/aspect-build/rules_ts Collaborators can add a "cleanup" or "need: discussion" label to keep it open indefinitely. Thanks for your contributions to rules_nodejs!

This issue was automatically closed because it went 30 days without any activity since it was labeled "Can Close?"