Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starlark http_archive's build_file attribute is not relative to the main workspace #6225

Closed
benjaminp opened this issue Sep 24, 2018 · 5 comments
Labels
team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. untriaged

Comments

@benjaminp
Copy link
Collaborator

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?

@benjaminp benjaminp changed the title Starlark http_archive's build_file is not relative to the main workspace Starlark http_archive's build_file attribute is not relative to the main workspace Sep 24, 2018
@laurentlb laurentlb added team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. and removed team-Starlark labels Sep 25, 2018
@dslomov
Copy link
Contributor

dslomov commented Oct 22, 2018

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

@dslomov dslomov closed this as completed Oct 22, 2018
@benjaminp
Copy link
Collaborator Author

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.

@fmeum
Copy link
Collaborator

fmeum commented Feb 17, 2019

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?

@dslomov
Copy link
Contributor

dslomov commented Feb 18, 2019

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.

@xiaoliuzi
Copy link

xiaoliuzi commented Oct 15, 2019

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)

@philwo philwo added the team-OSS Issues for the Bazel OSS team: installation, release processBazel packaging, website label Jun 15, 2020
@philwo philwo removed the team-OSS Issues for the Bazel OSS team: installation, release processBazel packaging, website label Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. untriaged
Projects
None yet
Development

No branches or pull requests

7 participants