bazel - How to write default for label attribute of a rule such that it will be searched in calling package? - Stack Overflow

admin2025-04-17  3

Say I have a rule like this, defined in a package //rules:

myrule = rule(
    implementation = _impl,
    attrs = {
        "label": attr.label(default = ":default.txt", mandatory = True),
    },
)

Then I call it from another package, //caller, like this:

myrule(name = "mytarget")

Bazel will complain that /package/default.txt does not exist, but I want it to use /caller/default.txt. I can accomplish this using a macro instead:

def mymacro(label = ":default.txt", **kwargs):
    myrule(label = label, **kwargs)

But is there a way I can accomplish it without the macro, by writing the default value in a different way?

转载请注明原文地址:http://anycun.com/QandA/1744853826a88546.html