Bazel sh_test 找不到节点

Bazel sh_test doesn't find node

我正在尝试 运行 需要 node 的脚本。我的机器上安装了节点。 我可以通过 bazel run //:sh_bin 运行 sh_binary 和脚本 运行s 节点就好了:

sh_binary(
    name = "sh_bin",
    data = [
    ],
    srcs = [":script.sh"],
)

script.sh:

node -v

bazel run //:sh_bin:

v14.17.6

现在我想将其转换为 sh_test:

sh_test(
    name = "sh_bin",
    data = [
    ],
    srcs = [":script.sh"],
)

但现在bazel test //:sh_bin找不到节点:

node: command not found

我也尝试将 local = True 添加到测试中,但仍然是同样的问题。

Bazel 测试 运行 在比应用程序 运行 通过 bazel run 更受控制的环境中。 测试 运行ner 建立的初始条件之一是 $PATH 的值:https://docs.bazel.build/versions/main/test-encyclopedia.html#initial-conditions

如果您正在使用远程执行,另一个问题可能是您的测试是在没有安装节点的机器上执行的。

努力实现独立于主机状态的 运行 和测试的密封构建始终是一个好主意。这意味着您需要使 node 程序可用于您的二进制文件或作为 data dep.

进行测试

一个很好的选择是建立在现有工作的基础上,例如 https://github.com/bazelbuild/rules_nodejs

话虽这么说,你的例子对我来说确实有效。

cd `mktemp -d`
touch WORKSPACE
echo "node -v" > script.sh
chmod +x script.sh
cat <<EOF > BUILD
sh_test(
    name = "sh_bin",
    srcs = [":script.sh"],
)
EOF
bazel test --test_output=all -- //:sh_bin
Starting local Bazel server and connecting to it...
INFO: Analyzed target //:sh_bin (24 packages loaded, 282 targets configured).
INFO: Found 1 test target...
INFO: From Testing //:sh_bin:
==================== Test output for //:sh_bin:
v17.1.0
================================================================================
Target //:sh_bin up-to-date:
  bazel-bin/sh_bin
INFO: Elapsed time: 6.895s, Critical Path: 0.10s
INFO: 5 processes: 3 internal, 2 linux-sandbox.
INFO: Build completed successfully, 5 total actions
//:sh_bin                                                                PASSED in 0.0s

Executed 1 out of 1 test: 1 test passes.
INFO: Build completed successfully, 5 total actions