dependenciesbazelexternal-dependencies

bazel fetch //... does nothing for BUILD and WORKSPACE files


I can't get bazel to download and build external dependencies.

Maybe I'm missing something obvious here,
but let's take gtest as an example.
At the root of my workspace I have a gtest.BUILD and WORKSPACE.

They are defined as:

gtest.BUILD:

cc_library(
    name = "main",
    srcs = glob(
        ["src/*.cc"],
        exclude = ["src/gtest-all.cc"]
    ),
    hdrs = glob([
        "include/**/*.h",
        "src/*.h"
    ]),
    copts = ["-Iexternal/gtest/include"],
    linkopts = ["-pthread"],
    visibility = ["//visibility:public"],
)

WORKSPACE:

new_http_archive(
    name = "gtest",
    url = "https://github.com/google/googletest/archive/release-1.7.0.zip",
    build_file = "gtest.BUILD",
    strip_prefix = "googletest-release-1.7.0",
)

I try to use gtest in my other code, but the external dependency does not exist.
When I run bazel fetch //... it does nothing.

I'm trying to follow bazel's documentation on external dependencies,
and I can't figure out what I'm leaving out. Any thoughts?

I looked inside $(bazel info output_base)/external,
but I don't see any of m built dependencies there.


Solution

  • It's possible that nothing in your current workspace //... depends on any target in @gtest//..., so running bazel fetch //... doesn't tell Bazel that it needs to download @gtest.

    $ bazel fetch //...
    Extracting Bazel installation...
    Starting local Bazel server and connecting to it...
    ...........
    $
    

    If you try to run bazel fetch @gtest//... directly, it'll download the archive as expected:

    $ bazel fetch @gtest//...
    INFO: SHA256 (https://github.com/google/googletest/archive/release-1.7.0.zip) = b58cb7547a28b2c718d1e38aee18a3659c9e3ff52440297e965f5edffe34b6d0
    Building: no action
    

    Now, if you add a target that depends on @gtest//:main and run bazel fetch //... again, it'll download @gtest:

    $ bazel clean --expunge
    INFO: Starting clean (this may take a while). Consider using --async if the clean takes more than several minutes.
    
    $ cat BUILD
    filegroup(
        name = "gtest",
        srcs = ["@gtest//:main"],
    )
    
    $ bazel fetch //...
    Starting local Bazel server and connecting to it...
    ..........
    INFO: SHA256 (https://github.com/google/googletest/archive/release-1.7.0.zip) = b58cb7547a28b2c718d1e38aee18a3659c9e3ff52440297e965f5edffe34b6d0
    Building: no action
    
    $ ls $(bazel info output_base)/external
    bazel_tools  @bazel_tools.marker  gtest  @gtest.marker  local_config_cc  @local_config_cc.marker