Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I call a rule impl explicitly? #714

Open
cbarrete opened this issue Jul 9, 2024 · 1 comment
Open

How do I call a rule impl explicitly? #714

cbarrete opened this issue Jul 9, 2024 · 1 comment

Comments

@cbarrete
Copy link
Contributor

cbarrete commented Jul 9, 2024

I've heard in multiple contexts that calling a rule's implementation from another rule could make sense.

I have previously wanted to do that to get the providers from cxx_library and add my own on top, and I now want to do it to create a wrapper around cxx_library that has a transition applied to it (I wish I could just use target transitions from a macro, but they don't exist yet!). In those cases, a macro just doesn't cut it.

So my question is: how? Rule implementations take a single AnalysisContext parameter, which I cannot mutate at will, nor create myself. If I need to amend the attrs passed to the underlying impl, I am stuck; but even without that, I don't think that forwarding the context as is would be correct: for example ctx.label would likely be incorrect.

Is there something I'm missing here?

@cbarrete
Copy link
Contributor Author

For anyone also interested in this, getting the providers from another rule can be done with something like:

my_wrapper_rule = rule(
    impl = lambda ctx: ctx.attrs.dep.providers,
    attrs = {
        "dep": attrs.dep(),
    },
)

It's not quite the same as it requires 2 targets to be defined. It can be abstracted away behind a macro, which is less than ideal, but better than nothing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant