How to ask git_repository to use BUILD file which exists inside imported repository

44 views
Skip to first unread message

Alexander Pivovarov

unread,
Mar 26, 2024, 7:31:42 PMMar 26
to bazel-discuss
my project A has dependency to another project B

project B provides BUILD.bazel file

I use git_repository in project A WORKSPACE to bring project B targets to project A
But I need to specify build_file path (local file) which defines project B targets.

Can I ask git_repository to use BUILD.bazel file which already exists in the root of project B?

project B examples:

Alex Humesky

unread,
Mar 26, 2024, 7:44:15 PMMar 26
to Alexander Pivovarov, bazel-discuss
The existing BUILD files should be available to use as-is

For example if you have a git_repository named "json" for https://github.com/nlohmann/json, then the build target here: https://github.com/nlohmann/json/blob/develop/BUILD.bazel#L2 will be available as "@json//:json"


--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/edefbb7d-4464-4c7e-be4e-368cdc5bc389n%40googlegroups.com.

Alex Humesky

unread,
Mar 26, 2024, 7:45:53 PMMar 26
to Alexander Pivovarov, bazel-discuss
Also note the advice here about using http_repository instead of git_repository: https://bazel.build/rules/lib/repo/git#git_repository
(The build targets will be available the same as with git_repository)

Alexander Pivovarov

unread,
Mar 26, 2024, 8:21:43 PMMar 26
to bazel-discuss
Currently I have to explicitly specify local path to BUILD file which should be used for nlohmann_json http_archive

http_archive(
    name = "nlohmann_json",
    sha256 = "a22461d13119ac5c78f205d3df1db13403e58ce1bb1794edc9313677313f4a9d",
    urls = [
        "https://github.com/nlohmann/json/releases/download/v3.11.3/include.zip",
    ],
    build_file = "//:BUILD.nlohmann_json",
)

BUILD.nlohmann_json is a local copy of https://github.com/nlohmann/json/blob/v3.11.3/BUILD.bazel
Seems like I need to have local copy of BUILD.nlohmann_json file in my project despite fact that include.zip has BUILD.bazel file internally

Can I ask http_archive to use BUILD.bazel file which exist inside include.zip?

Filip Filmar

unread,
Mar 26, 2024, 8:47:57 PMMar 26
to Alexander Pivovarov, bazel-discuss
On Tue, Mar 26, 2024 at 5:21 PM Alexander Pivovarov <apivo...@gmail.com> wrote:
Seems like I need to have local copy of BUILD.nlohmann_json file in my project despite fact that include.zip has BUILD.bazel file internally

No, you should not need to do that at all.  Specifying `build_file = ...` is optional

Again, a minimal example would be helpful.

F

Alexander Pivovarov

unread,
Mar 27, 2024, 6:59:38 PMMar 27
to bazel-discuss
ok, I was able to solve the issue. 

If I specify just name and url for http_archive then my build failed with
no such package '@nlohmann_json//': BUILD file not found in directory '' of external repository @nlohmann_json. Add a BUILD file to a directory to mark it as a package. and referenced by

http_archive also needs strip_prefix to be defined in order to find BUILD.bazel file inside the archive)

Full working example of nlohmann_json for Bazel:

# WORKSPACE
http_archive(
  name = "nlohmann_json",
  url = "https://github.com/nlohmann/json/archive/v3.11.3.tar.gz",
  strip_prefix = "json-3.11.3",
)

# BUILD
cc_library(
  name = "foo",
  hdrs = ["foo.h"],
  srcs = ["foo.cc"],
  deps = [
    "@nlohmann_json//:json",
  ],
)
Hope this resource is searchable and Google will index it

Filip Filmar

unread,
Mar 27, 2024, 7:07:56 PMMar 27
to Alexander Pivovarov, bazel-discuss
On Wed, Mar 27, 2024 at 3:59 PM Alexander Pivovarov <apivo...@gmail.com> wrote:
http_archive also needs strip_prefix to be defined in order to find BUILD.bazel file inside the archive)

Yes, in many cases you need to strip the unneeded bits of path from the uncompressed contents of the archive, in order to get to the correct external repository layout. 
This is not obvious and I can see how one could be tripped up by it.

Bazel folks, do you have ideas how this could be made more apparent to would-be users?

F

Reply all
Reply to author
Forward
0 new messages