Enabling unexported ("lambda") rules
- Dominant language
- Java
- Stars
- 25.8k
- Forks
- 4.6k
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 72
Description
Hello, wonderful Bazel folks! Thanks for all you do.
This is a build language feature request.
With the advent of nested functions and lambdas, it's sometimes useful to create rules without a public name. For example, here's (an abbreviated) one that I wrote to be able to easily configure other rules to add platform transitions in my graph.
[Maybe a higher order function isn't the most readable example for an issue, but that's also kind of the point: This is logic that's really nice to write once and then hide away.]
```
def configure_macro_creator(settings_to_override_dict={}, settings_to_add_dict={}):
transitioning_rule = rule(<...>)
def configure_wrapper(rule_to_configure):
def macro_wrapper(name, **kwargs):
wrapped_name = name + "-configured"
transitioning_rule(name=name, exports=wrapped_name)
rule_to_configure(name=wrapped_name, **kwargs)
return macro_wrapper
return configure
```
Anyway, conceptually, it can be quite useful to create a rule without an exported name if it's part of the internals of some macro.
Right now, this fails with `Error in unexported rule: Invalid rule class hasn't been exported by a bzl file`. But it strikes me that there might be a good way to support this inside, given it's useful. As an example, rules could be registered on creation with some ID, and kept usable in the graph that way.
The current workaround is to also return and store the rule that's intended to be hidden, private implementation, but it feels like it might be possible to support this use case better than that.
Thanks for your consideration,
Chris
(ex-Googler)
P.S. Related issue, but for repositories and in the other direction https://github.com/bazelbuild/bazel/issues/10441
Contributor guide
Research direction
Start by tracing the rule(...) call in the provided example and the reported "Error in unexported rule" message. Review the related repository issue #10441 for context before determining the relevant Bazel build-language entry points. Done should be defined as a supported way to create and use a rule without exporting it from a bzl file, with the existing private-implementation workaround no longer required.
Written by the indexing model from the issue text.
Assessment
- Domain
- build-system
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100