google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.26k stars 204 forks source link

How to get variable name in builtin function? #407

Closed jathu closed 2 years ago

jathu commented 2 years ago

In Bazel, we define a rule as:

fun_rule = rule(...)

Notice that the name of the rule is fun_rule and it is only named via the variable. We don't provide the rule a name attribute. Now, if we invoke the rule with a non-existent attribute, Bazel throws an error:

fun_rule(name = "example", fake_attr = "hello")

$ bazel build //:example

ERROR: /app/BUILD:3:10: //:example: no such attribute 'fake_attr' in 'fun_rule' rule

As you can see, Bazel was able to understand the left hand variable name of the rule. If we were to transfer to over to starlark-go, how can we get the left-hand variable name given the builtin method?

func go_rule(thread *starlark.Thread, module string) (starlark.StringDict, error) { ... }