bazelskylark

Using bazel macros across repositories with labels


I've got two repositories, Client and Library.

Inside of Client's WORKSPACE file Client imports Library as a http_archive with the name "foo".

Inside of Client, I want to use Library macros that reference targets inside Library. My problem is that the Library macros don't know that were imported as "foo", so when the macro is expanded the targets are not found.


Solution

  • library/WORKSPACE:
    workspace(name = "library") library/some.bzl:

    def my_macro():
      native.java_library(name = "my_macro_lib",
        deps = ["@library//:my_macro_lib_dependnecy"]
      )
    

    library/BUILD.bazel:

    java_library(name = "my_macro_lib_dependnecy",
      ...
    )
    

    client/WORKSPACE:

    workspace(name = "client")
    http_archive(
                 name = "library",
                 urls = [...],
                 strip_prefix = ...,
                 sha256 = ...,
    )
    

    Because both workspaces use the same name for library workspace (name = "library") and because the macro refers to the workspace name in its dependencies (@library//:my_macro_lib_dependnecy) this works. Note this works but has some quirks which will be resolved in 0.17.0