slai-nick
slai-nick

Reputation: 145

bzlmod: dealing with external dependencies with no MODULE.bazel

I would like to use bzlmod as I am in the process of splitting a project into different modules and thought it would be a great way to solve the transitive dependencies.

However, not all project have yet a MODULE.bazel file (e.g. grpc), so the question is: is it possible to add an external dependency that doesn't have a MODULE.bazel to the module system so that everything works fine if I reuse the new module?

Upvotes: 5

Views: 974

Answers (1)

Mikhail Zinov
Mikhail Zinov

Reputation: 324

If I undetand questions correctly you can try to use module extension:

  1. Define deps without MODULE.bazel using http_archive, https_file or https://bazel.build/rules/lib/repo/local depends on the type of the dependencies. I will take https://github.com/Alamofire/Alamofire just as example, as it does not contain anything related to bazel. In some file, like //custom_deps.bzl you can create
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive",)

def custom_deps_repos():
   http_archive(
     name = "Alamofire",
     build_file = "//:Alamofire.BUILD",
     ...
     url = "https://github.com/....",
   )
  1. There is no Alamofire.BUILD, so we need to create one:
...
swift_library(
   name = "Alamofire",
   scrs = glob(["Source/**/*.swift"]),
   ...
)
  1. Now in //extensions.bzl you can create module extension:
load("//:custom_deps.bzl", "custom_deps_repos")

def _non_module_custom_deps_repos_impl(_ctx):
    custom_deps_repos()

non_module_custom_deps_repos = module_extension(
    implementation = _non_module_custom_deps_repos_impl,
)

  1. Last part is to load this extension in MODULE.bazel file
custom_deps_repos = use_extension("//:extensions.bzl", "non_module_custom_deps_repos")
use_repo(
    custom_deps_repos,
    "Alamofire",
)

after, we can use @Alamofire anywhere in the project.

Some example here:

PS: instead of http_archive or any remote files it possible to use some local paths as well https://bazel.build/rules/lib/repo/local

Upvotes: 0

Related Questions