Bazel: Starlark http_archive's build_file attribute is not relative to the main workspace

Created on 25 Sep 2018  路  5Comments  路  Source: bazelbuild/bazel

new_http_archive's build_file attribute is documented to be dereferenced relative to the main workspace root. Thus, the following example using the builtin new_http_archive rule works fine:

$ bazel version
Build label: 0.17.2- (@non-git)
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Fri Sep 21 15:14:04 2018 (1537542844)
Build timestamp: 1537542844
Build timestamp as int: 1537542844
$ touch BUILD.repo
$ cat > WORKSPACE
new_http_archive(
    name = 'repo',
    build_file = 'BUILD.repo',
    urls = ['https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.18.9.tar.xz'],
    sha256 = '4c995351e57902a04a94e43796407b4ba295c8eae070c27e99f8f99c321e917a',
)
$ bazel build @repo//:*
INFO: Analysed target @repo//:BUILD.bazel (3 packages loaded).
INFO: Found 1 target...
INFO: Elapsed time: 19.030s, Critical Path: 0.01s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action

However, it doesn't work with the Starlark http_archive:

$ cat > WORKSPACE
load('@bazel_tools//tools/build_defs/repo:http.bzl', 'http_archive')
http_archive(
    name = 'repo',
    build_file = 'BUILD.repo',
    urls = ['https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.18.9.tar.xz'],
    sha256 = '4c995351e57902a04a94e43796407b4ba295c8eae070c27e99f8f99c321e917a',
)
$ bazel build @repo//:*
ERROR: no such package '@repo//': Traceback (most recent call last):
    File "/home/benjamin/.cache/bazel/_bazel_benjamin/87d771599bb2e31ca7198e21b8b9bb06/external/bazel_tools/tools/build_defs/repo/http.bzl", line 56
        workspace_and_buildfile(ctx)
    File "/home/benjamin/.cache/bazel/_bazel_benjamin/87d771599bb2e31ca7198e21b8b9bb06/external/bazel_tools/tools/build_defs/repo/utils.bzl", line 60, in workspace_and_buildfile
        ctx.symlink(ctx.attr.build_file, "BUILD.bazel")
Not a regular file: external/BUILD.repo
ERROR: no such package '@repo//': Traceback (most recent call last):
    File "/home/benjamin/.cache/bazel/_bazel_benjamin/87d771599bb2e31ca7198e21b8b9bb06/external/bazel_tools/tools/build_defs/repo/http.bzl", line 56
        workspace_and_buildfile(ctx)
    File "/home/benjamin/.cache/bazel/_bazel_benjamin/87d771599bb2e31ca7198e21b8b9bb06/external/bazel_tools/tools/build_defs/repo/utils.bzl", line 60, in workspace_and_buildfile
        ctx.symlink(ctx.attr.build_file, "BUILD.bazel")
Not a regular file: external/BUILD.repo
INFO: Elapsed time: 14.084s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)

Is this difference intentional?

area-ExternalDeps team-XProduct untriaged

Most helpful comment

Okay, but I note that like #5633, this behavior difference makes it wrong to advertise the new_* Starlark rules as "drop-in" replacements for the native rules.

It would be nice if the Starlark rules' documentation was online like the native rules they deprecate.

All 5 comments

Yes, the reference should be @//:BUILD.repo to resolve in main workspace.

Okay, but I note that like #5633, this behavior difference makes it wrong to advertise the new_* Starlark rules as "drop-in" replacements for the native rules.

It would be nice if the Starlark rules' documentation was online like the native rules they deprecate.

I just gave this a try because I ran into the same issue. Even with build_file = '@//:BUILD.repo', this will not compile and instead give the following error message:

ERROR: no such package '@repo//': Traceback (most recent call last):
    File "/home/fhenneke/.cache/bazel/_bazel_fhenneke/b30f7ed91334c5ffe57994cdd00414cf/external/bazel_tools/tools/build_defs/repo/http.bzl", line 56
        workspace_and_buildfile(ctx)
    File "/home/fhenneke/.cache/bazel/_bazel_fhenneke/b30f7ed91334c5ffe57994cdd00414cf/external/bazel_tools/tools/build_defs/repo/utils.bzl", line 62, in workspace_and_buildfile
        ctx.symlink(ctx.attr.build_file, "BUILD.bazel")
Unable to load package for //:BUILD.repo: not found.
ERROR: no such package '@repo//': Traceback (most recent call last):
    File "/home/fhenneke/.cache/bazel/_bazel_fhenneke/b30f7ed91334c5ffe57994cdd00414cf/external/bazel_tools/tools/build_defs/repo/http.bzl", line 56
        workspace_and_buildfile(ctx)
    File "/home/fhenneke/.cache/bazel/_bazel_fhenneke/b30f7ed91334c5ffe57994cdd00414cf/external/bazel_tools/tools/build_defs/repo/utils.bzl", line 62, in workspace_and_buildfile
        ctx.symlink(ctx.attr.build_file, "BUILD.bazel")
Unable to load package for //:BUILD.repo: not found.

The only thing that works for me is to use build_file_content, which is certainly not optimal. Is there another way to get build_file to work as before?

Agreed that this is not a great error message @FabianHenneke, but what this line

Unable to load package for //:BUILD.repo: not found.

tells you is that your root package (//) does not have a BUILD file.

$ touch BUILD

fixes this. Filed #7457 for the error message.

Thank you very much @dslomov .

$ touch BUILD

It works!!

I learned gtest example of bazel with cpp (https://docs.bazel.build/versions/master/cpp-use-cases.html) and encountered the following error:

ERROR: An error occurred during the fetch of repository 'gtest':
   Traceback (most recent call last):
    File "/root/.cache/bazel/_bazel_root/f85ff975689f12813b75367d2f317669/external/bazel_tools/tools/build_defs/repo/http.bzl", line 60
        workspace_and_buildfile(ctx)
    File "/root/.cache/bazel/_bazel_root/f85ff975689f12813b75367d2f317669/external/bazel_tools/tools/build_defs/repo/utils.bzl", line 61, in workspace_and_buildfile
        ctx.symlink(ctx.attr.build_file, "BUILD.bazel")
Unable to load package for //:gtest.BUILD: BUILD file not found in any of the following directories.
 - /root/wkdir/learn/bazel/examples/cpp-tutorial/http
ERROR: /root/wkdir/learn/bazel/examples/cpp-tutorial/http/test/BUILD:1:1: //test:hello-test depends on @gtest//:main in repository @gtest which failed to fetch. no such package '@gtest//': Traceback (most recent call last):
    File "/root/.cache/bazel/_bazel_root/f85ff975689f12813b75367d2f317669/external/bazel_tools/tools/build_defs/repo/http.bzl", line 60
        workspace_and_buildfile(ctx)
    File "/root/.cache/bazel/_bazel_root/f85ff975689f12813b75367d2f317669/external/bazel_tools/tools/build_defs/repo/utils.bzl", line 61, in workspace_and_buildfile
        ctx.symlink(ctx.attr.build_file, "BUILD.bazel")
Unable to load package for //:gtest.BUILD: BUILD file not found in any of the following directories.
 - /root/wkdir/learn/bazel/examples/cpp-tutorial/http
ERROR: Analysis of target '//test:hello-test' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.065s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)

Was this page helpful?
0 / 5 - 0 ratings