我创建了以下测试文件用于在
bazel build
上测试 Ubuntu
。
规则是从 Angular 复制的: https://github.com/angular/angular
这是测试脚本:
mkdir -p /tmp/test
cd /tmp/test
cat > BUILD <<EOF
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
EOF
cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "5dd1e5dea1322174c57d3ca7b899da381d516220793d0adef3ba03b9d23baa8e",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.8.3/rules_nodejs-5.8.3.tar.gz"],
)
EOF
然后运行:
bazel build
这是输出:
/tmp/test# which bazel
/usr/bin/bazel
/tmp/test# bazel --version
bazel 7.0.0
/tmp/test# bazel build
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
WARNING: --enable_bzlmod is set, but no MODULE.bazel file was found at the workspace root. Bazel will create an empty MODULE.bazel file. Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel. For more details, please refer to https://github.com/bazelbuild/bazel/issues/18958.
WARNING: Usage: bazel build <options> <targets>.
Invoke `bazel help build` for full description of usage and options.
Your request is correct, but requested an empty set of targets. Nothing will be built.
INFO: Found 0 targets...
INFO: Elapsed time: 17.390s, Critical Path: 0.03s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
然后,尝试从缓存中找到http_archive下载的文件:
# find $HOME/.cache/bazel -type f -name '*nodejs*' -exec echo {} \;
但是
$HOME/.cache/bazel
里面什么都没有。
看起来
bazel
没有运行“load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
”,它也没有运行“urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.8.3/rules_nodejs-5.8.3.tar.gz"]
”中定义的http_archive
。
这是预期的输出:
The "rules_nodejs-5.8.3.tar.gz" and some other dependencies files
should exist in the $HOME/.cache/bazel.
如何让
bazel
运行load ...
和http_archive ...
中定义的“BUILD
”和“WORKSPACE
”?
您的提示在错误消息中:
Your request is correct, but requested an empty set of targets. Nothing will be built.
Bazel 懒惰地提取依赖项,并且因为您没有告诉 Bazel 要构建什么(即您的空
bazel build
命令,所以它不会执行任何操作。
从您的示例来看,您的 BUILD 文件也是空的:
cat > BUILD <<EOF
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
EOF
考虑将
js_library
目标添加到该 BUILD 文件中,然后运行 bazel build :target_name
。