Paul Draper
Paul Draper

Reputation: 83255

How to resolve paths relative to workspace in Bazel?

local_repository and new_local_repository both take paths as arguments, and these paths are resolved relative to the workspace.

local_repository(
    name = "my-ssl",
    path = "../ssl", # relative to workspace
)

I am trying to get similar behavior for a custom repository rule, but I can't figure it out.

It seems that all the repository_ctx functions operate relative to the repository, not the workspace.

my_repository(
    name = "my-ssl",
    path = "../ssl", # how can my rule resolve that path
)

How can I resolve path arguments relative to the workspace, like the built-in repository rules do?

Upvotes: 0

Views: 1712

Answers (1)

dms
dms

Reputation: 1390

One option could be to use a Label("//:WORKSPACE") to get the workspace dir and compose it with your relative path:

def _impl(repository_ctx):
    workspace_dir = repository_ctx.path(Label("//:WORKSPACE")).dirname
    repo_dir_str = '/'.join([str(workspace_dir), repository_ctx.attr.path])
    print(repo_dir_str)

    repo_dir = repository_ctx.path(repo_dir_str)
    print(repo_dir)
    print(repo_dir.exists)


my_repository = repository_rule(
    implementation = _impl,
    attrs = {
        "path": attr.string(mandatory = True),
    }
)

The workspace could also be an attribute, if needed:

my_repository = repository_rule(
    implementation = _impl,
    attrs = {
        "path": attr.string(mandatory = True),
        "workspace": attr.label(default = Label("//:WORKSPACE")),
    }
)

Upvotes: 1

Related Questions