ubuntubuildbazel

How to make bazel to run "load" and "http_archive"


I created the following test files for testing bazel build on Ubuntu.

The rules are copied from angular: https://github.com/angular/angular

Here is the testing script:

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

then run:

bazel build

Here is the output:

/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

And then, try to find the http_archive downloaded file from cache:

# find $HOME/.cache/bazel -type f -name '*nodejs*' -exec echo {} \;

But there is nothing in the $HOME/.cache/bazel.

It seems that bazel didn't run "load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")", it also didn't run the urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.8.3/rules_nodejs-5.8.3.tar.gz"] defined in the "http_archive".

Here is the expected output:

The "rules_nodejs-5.8.3.tar.gz" and some other dependencies files 
should exist in the $HOME/.cache/bazel.

How to make bazel to run "load ..." and "http_archive ..." defined in BUILD and WORKSPACE?


Solution

  • Your hint is in the error message:

    Your request is correct, but requested an empty set of targets. Nothing will be built.
    

    Bazel pulls dependencies lazily, and because you're not telling Bazel what to build (i.e. your empty bazel build command, it doesn't do anything.

    Your BUILD file is also empty, from your example:

    cat > BUILD <<EOF
    load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
    EOF
    

    Consider adding a js_library target into that BUILD file, then run bazel build :target_name.