google / google/go-jsonnet

Improve error when using object locals in field name expressions or for specs

Open
#385 3 comments 0 reactions 0 assignees View on GitHub
error reporting
Dominant language
Go
Stars
1.8k
Forks
263
PR merge metrics
No merged PRs in 30d

Description

I was actually trying to use an object comprehension with a top level function, like:

```jsonnet
function(j) {
local obj = std.parseJson(j),

[key]: someOtherFunction(key)
for key in std.objectFields(obj)
}
```

But then I was able to minimize the reproducer to:

```jsonnet
{
local x = ['a'],

[key]: key
for key in x
}
```

Which errors with
```
/tmp/f.jsonnet:5:14-15 Unknown variable: x

for key in x
```

I expected this form to work.

If this is behaving as intended, perhaps there is a way the error message could be improved?

Eventually I figured out that I can move the local variable declaration outside of the scope of the object comprehension:

```jsonnet
(
local x = ['a'];
{
[key]: key
for key in x
}
)
```

Which results in

```json
{
"a": "a"
}
```

as expected. But this approach was very unintuitive and took me around 45 minutes to debug, even though I've been working with Jsonnet on a near-daily basis for 6+ months.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.