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

Different starlark functions generated from the same source calling each other triggers recursion check #17236

Closed
torgil opened this issue Jan 17, 2023 · 1 comment
Labels
team-Starlark-Interpreter Issues involving the Starlark interpreter used by Bazel type: bug untriaged

Comments

@torgil
Copy link
Contributor

torgil commented Jan 17, 2023

Description of the bug:

If two functions that are generated from the same function calls each other, bazel triggers recursion check. Below example results in (current dir replaced with $PWD):

ERROR: /Users/torgil/src/bazel/lambda_calling_lambda/BUILD.bazel:2:12: in return_srcs rule //:fail: 
Traceback (most recent call last):
        File "$PWD/rules.bzl", line 13, column 44, in lambda
                implementation = lambda ctx: result(ctx = ctx),
        File "$PWD/rules.bzl", line 4, column 29, in lambda
                return lambda *, ctx: fn(ctx = ctx, **kw)
        File "$PWD/rules.bzl", line 7, column 69, in lambda
                default_info = lambda *, ctx, files_cb: DefaultInfo(files = files_cb(ctx = ctx))
        File "$PWD/rules.bzl", line 4, column 12, in lambda
                return lambda *, ctx: fn(ctx = ctx, **kw)
Error: function 'lambda' called recursively

This is unexpected as they are two separate functions and not the same function calling itself as in #8921 which needs recursion allowed ( #9163 ).

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

  1. Download bazel-6.0.0 (make sure it has that name, is executable and available in $PATH)
  2. Open a terminal with zsh or bash
  3. Create a directory, cd into it, paste below script into the terminal and press enter
touch WORKSPACE

cat <<EOF > rules.bzl
def partial(fn, **kw):
    # Different instances of this lambda is treated as the same function
    # resulting in "Error: function 'lambda' called recursively"
    return lambda *, ctx: fn(ctx = ctx, **kw)

get_files = lambda *, ctx, attribute: depset(getattr(ctx.files, attribute))
default_info = lambda *, ctx, files_cb: DefaultInfo(files = files_cb(ctx = ctx))

def create_return_files_rule(attribute):
    files = partial(get_files, attribute = attribute)
    result = partial(default_info, files_cb = files)
    return rule(
        implementation = lambda ctx: result(ctx = ctx),
        attrs = {attribute: attr.label_list(allow_files = True)},
    )

return_srcs = create_return_files_rule("srcs")
EOF

cat <<EOF > BUILD.bazel
load("rules.bzl", "return_srcs")
return_srcs(
    name = "fail",
    srcs = ["WORKSPACE", "rules.bzl", "BUILD.bazel"],
)
EOF

bazel-6.0.0 build //:fail

Which operating system are you running Bazel on?

Seen on Ubuntu 22.04 and Mac OS Ventura 13.1

What is the output of bazel info release?

release 6.0.0-homebrew

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 master; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

#8921 and #9163 are related to the same error message.

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

No response

@torgil torgil changed the title Separate generated starlark functions calling each other triggers recursion check Different starlark functions generated from the same source calling each other triggers recursion check Jan 17, 2023
@ShreeM01 ShreeM01 added type: bug untriaged team-Starlark-Interpreter Issues involving the Starlark interpreter used by Bazel labels Jan 17, 2023
@brandjon
Copy link
Member

Thanks for the report.

This is WAI. The recursion check is based on the static function definition text, not the dynamic function value. This is to prevent arbitrary unbounded computation (i.e. circumventing the recursion check) by dynamically creating new function values. (If we decided to allow that, at that point we may as well disable the recursion check entirely.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-Starlark-Interpreter Issues involving the Starlark interpreter used by Bazel type: bug untriaged
Projects
None yet
Development

No branches or pull requests

3 participants