bazelbuild / bazelbuild/bazel

Allow to use recursion in Starlark

Open
#9,163 6 comments 9 reactions 0 assignees View on GitHub
not stale P4 team-Starlark-Interpreter type: feature request
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 18h
Merged PRs (30d)
75

Description

### Description of the problem / feature request:
Allow to use recursion in Starlark. Instead of just detecting recursion maybe its better to limit Starlark stacksize.

### Feature requests: what underlying problem are you trying to solve with this feature?
I trying to write my own .bzl files for building C++ code, but native functions is not [well documented](https://github.com/bazelbuild/bazel/issues/9147), because of that I'm just printing variables in my rules for exploration. But I've got an problem with writing generic function, which can dump object content in Starlark, because of lack of recursion.

My implementation of printing function for debug puproses:
**my_rule.bzl**:
```
def dump(obj, indent):
for field in dir(obj):
attr_value = getattr(obj, field)
attr_value_type = type(getattr(obj, field))
print(indent, attr_value_type, field, ": ", )
dump(attr_value, indent + " ")

def _my_rule(ctx):
dump(ctx.attr.input.output_groups, "")

my_rule = rule(
implementation = _my_rule,
attrs = {
"input": attr.label(allow_single_file = True),
},
)
```
**BUILD**:
```
package(default_visibility = ["//visibility:public"])

load("//:my_rule.bzl", "my_rule")

cc_library(
name = "bar",
srcs = ["main.cc"],
)

my_rule(
name = "dump",
input = ":bar",
)
```

With this code I've get an error:
```
Recursion was detected when calling 'dump' from 'dump'
```

### What operating system are you running Bazel on?
Windows 10 x64

### What's the output of `bazel info release`?
release 0.29.0rc5

### Any other information, logs, or outputs that you want to share?
[Bazel-discuss group thread](https://groups.google.com/forum/#!topic/bazel-discuss/USIiULBXfOE)

Contributor guide

Open the contributing guide

Research direction

Start by reviewing Starlark's current recursion-detection behavior and the linked Bazel-discuss thread. Clarify whether the intended change is to permit recursion with a stack-size limit or to replace the current detection, then define how completion and safety should be verified.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.