bazelbuild / bazelbuild/bazel

Expose information about Target's rule to Starlark

Closed
#21,646 2 comments 0 reactions 0 assignees View on GitHub
team-Starlark-Integration 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:

Apparently, `Target` object [doesn't expose](https://bazel.build/rules/lib/builtins/Target) information about the rule that created it. That information might be quite useful in some cases (like aspects). I'd imagine it would look like a `.rule` attribute with either `str` or `None` object. Implementation might be quite simple: ConfiguredTargets objects already [have](https://github.com/bazelbuild/bazel/blob/1f2d57631df9c61f0a15775cad72dc415a037ee4/src/main/java/com/google/devtools/build/lib/analysis/configuredtargets/AbstractConfiguredTarget.java#L263C17-L263C35) interfaces to retrieve rule name, one would only need to make them available to Starlark.

### Which category does this issue belong to?

Starlark Integration

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

Feature would improve user experience in two scenarios:
* When writing an aspect which aggregates some data about build tree, it can be useful to filter `Target` objects (like ones aggregated from attributes) by their rule. Right now user would have to guess about target's rule based on label/providers.
* It can occasionally be useful to differentiate between Rule-generated and File-generated `Target` objects. Right now it, as far as I know, can only be done by looking at `repr(target)`. While it does work, it is probably not the best possible way to do this.

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

_No response_

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

_No response_

### 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?

If this feature is accepted, I could give a shot at implementing it myself

Contributor guide

Open the contributing guide

Research direction

Start with AbstractConfiguredTarget.java at the linked interface references and trace how rule information reaches Starlark Target objects. Check the Target builtins documentation and relevant Starlark integration code. Done means a Target can expose the creating rule as a string or None, allowing aspects to distinguish rule-generated and file-generated targets.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
build-system
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.