0

I want to implement a starlark repository rule that takes a tar.gz from the WORKSPACE directory (I'm putting files in git LFS there) and extracts it, then uses the extracted contents as an external repository (i.e., extracts the file, then does the equivalent of local_repository() on that directory).

I've tried the following:

load("@bazel_tools//tools/build_defs/repo:utils.bzl", "workspace_and_buildfile", "patch")
def _local_archive_impl(repository_ctx):
    if repository_ctx.attr.build_file and repository_ctx.attr.build_file_content:
        fail("Only one of build_file and build_file_content can be provided.")

    repository_ctx.extract(repository_ctx.path(repository_ctx.attr.archive).basename, "", repository_ctx.attr.strip_prefix)
    patch(repository_ctx)
    workspace_and_buildfile(repository_ctx)

but it appears that this approach won't work since Starlark repository rules don't appear to be able to access the WORKSPACE directory(?!?).

I also tried the approach referred to in https://groups.google.com/forum/m/#!topic/bazel-discuss/UXvp0rksRMM, namely:

def _impl(ctx):
  ctx.execute(["tar", "zxf", ctx.attr.archive)

local_archive = repository_rule(
    implementation = _impl,
    local = True,
    attrs = {'archive': attr.string()}
)

but that doesn't work either :(.

Silas Snider
  • 1,460
  • 1
  • 14
  • 23

1 Answers1

0

You can get the path of the workspace by declaring an attribute who label points to a file in the workspace root. For example, consider:

my_local_repository = repository_rule(
implementation = _local_repository_impl,
attrs = {
    "_workspace": attr.label(
        default = Label("//:WORKSPACE"),
    ),
}

Where:

def _local_repository_impl(repository_ctx):
    ws_dir = repository_ctx.path(repository_ctx.attr._workspace).dirname

Then list/find your archive and go from there.

However, this looks pretty similar to your first attempt. Perhaps you can update your question to include the repository_rule definition where you declare the "archive" attribute.

Disclaimer: I just typed out this as pseudocode, did not actually test it, but I've used this technique before. HTH.

Paul
  • 595
  • 6
  • 6