nodejs_binary does not support yarn pnp
🐞 bug report
Affected Rule
The issue is caused by the rule: nodejs_binary
Is this a regression?
Not that I know of.
Description
Using yarn Plug'n'Play (PnP), a monorepo with child projects utilizes the parent "./.pnp.cjs" file for all projects to resolve NodeJS require(...) statements.
A workspace with a single child project of "./app1/"...
A simple, NodeJS console application "./app1/index.js" script setup with a "./app1/BUILD.bazel" file...
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
nodejs_binary(
name = "app",
entry_point = ":index.js",
# BUG: label '//:.pnp.cjs' in $(location) expression is not a declared prerequisite of this rule
# BUG: $(rootpath) not defined.
# templated_args = ["--node_options=--require=$$(rlocation $(rootpath //:.pnp.cjs))"],
# BUG: hard-coded works on Git BASH for this specific path, but will fail any other path and fails Windows `cmd`
# *Bazel explicitly recommends NOT using Git BASH or other Windows-emulated BASH environments:
# https://bazel.build/docs/windows#running-bazel-shells
templated_args = ["--node_options=--require=/c/Users/.../src/github/me/labs-bazel/.pnp.cjs"],
# BUG: hard-coded for Windows `cmd` fails with the message below:
# *it's odd to see /bin/bash reported when using `cmd`...
# An error occurred mounting one of your file systems. Please run 'dmesg' for more details.
# /bin/bash: C:/users/.../_bazel_.../hfnv65ee/execroot/__main__/bazel-out/x64_windows-fastbuild/bin/app1/app.sh: No such file or directory
# templated_args = ["--node_options=--require=C:/Users/.../src/github/me/labs-bazel/.pnp.cjs"],
)
👆🏽 This fails of course, because nodejs_binary does not appear to have the same support for rootpath and rlocation that nodejs_test does.
😕 Confusingly, the nodejs_binary documentation for templated_args refers to this exact scenario of using rootpath and rlocation, but uses the nodejs_test as the example.
🔬 Minimal Reproduction
See "./README.md" notes for more details. *NOTE: the commit will matter as I continue to investigate by using containerized execution of Bazel instead of platform installs of Bazel.
🔥 Exception or Error
>bazel build --define=VERBOSE_LOGS=1 //src/node/console-basic:app
ERROR: C:/users/.../src/github/me/labs-bazel/src/node/console-basic/BUILD.bazel:2:14: in nodejs_binary rule //src/node/console-basic:app: label '//:.pnp.cjs' in $(location) expression is not a declared prerequisite of this rule
ERROR: C:/users/.../src/github/me/labs-bazel/src/node/console-basic/BUILD.bazel:2:14: in templated_args attribute of nodejs_binary rule //src/node/console-basic:app: $(rootpath) not defined. Since this rule was created by the macro 'nodejs_binary_macro', the error might have been caused by the macro implementation
ERROR: C:/users/.../src/github/me/labs-bazel/src/node/console-basic/BUILD.bazel:2:14: Analysis of target '//src/node/console-basic:app' failed
ERROR: Analysis of target '//src/node/console-basic:app' failed; build aborted:
INFO: Elapsed time: 0.159s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)
🌍 Your Environment
Operating System:
Windows 11 Pro
Output of bazel version:
Bazelisk version: v1.12.0
Build label: 5.2.0
Build target: bazel-out/x64_windows-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue Jun 7 16:03:57 2022 (1654617837)
Build timestamp: 1654617837
Build timestamp as int: 1654617837
Rules_nodejs version:
(Please check that you have matching versions between WORKSPACE file and @bazel/* npm packages.)
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "f10a3a12894fc3c9bf578ee5a5691769f6805c4be84359681a785a0c12e8d2b6",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.5.3/rules_nodejs-5.5.3.tar.gz"],
)
node_repositories(
node_version = "16.16.0",
yarn_version = "3.2.2",
)
Anything else relevant?
See "./README.md" notes for more details. *NOTE: the commit will matter as I continue to investigate by using containerized execution of Bazel instead of platform installs of Bazel.