bazelbuild / bazelbuild/bazel

Invoking a rule should return a label.

Open
#23,161 8 comments 0 reactions 0 assignees View on GitHub
team-Loading-API type: feature request untriaged
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

### Description of the feature request:

I'd like for invoking a rule to return a label. This should be relatively trivial, but may be complicated by the fact that this is exactly what you want in a .bzl file, but probably not what you want in a BUILD file most of the time.

This may need to be accompanied by some kind of lint to ensure you don't use the return value in BUILD files, only in .bzl files.

### Which category does this issue belong to?

Rules API

### What underlying problem are you trying to solve with this feature?

When writing a macro, typically you write it like so:

```
def my_macro(name):
rule_foo(
name = "_%s_foo" % name,
...
)

rule_bar(
name = name,
foo = "_%s_foo" % name
)
```

This can, of course, be written instead as:

```
def my_macro(name):
foo_name = "_%s_foo" % name
rule_foo(
name = foo_name,
...
)

rule_bar(
name = name,
foo = foo_name
)
```

However, the former is prone to bugs, since you have to rewrite a string, and the latter is more verbose and less clear, as bazel users expect to be able to just look at the name field directly.

I think that the simplest and nicest API would simply be the following:
```
def my_macro(name):
foo_label = rule_foo(
name = "_%s_foo" % name,
...
)

rule_bar(
name = name,
foo = foo_label
)
```

### Which operating system are you running Bazel on?

Linux

### What is the output of `bazel info release`?

7.2.0

### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.

_No response_

### What's the output of `git remote get-url origin; git rev-parse HEAD` ?

_No response_

### Have you found anything relevant by searching the web?

_No response_

### Any other information, logs, or outputs that you want to share?

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the Rules API entry points for invoking rules, then inspect how .bzl and BUILD files are distinguished. Use the rule_foo/rule_bar macro examples as the behavioral case; done means a rule invocation can supply its label to a following rule in .bzl without changing ordinary BUILD-file usage, with any needed lint behavior covered.

Written by the indexing model from the issue text.

Assessment

Domain
build-system, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.