Skip to content

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

Open
@mark-rushakoff

Description

@mark-rushakoff

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

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:

{
  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:

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

Which results in

{
   "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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    error reportingHow the errors are handled (e.g. stack trace quality)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions