Skip to content

jsonnetfmt produces invalid output for object comprehensions with literal computed keys #879

@Lougarou

Description

@Lougarou

jsonnetfmt formats a valid object comprehension into one that no longer parses. It strips the
[...] brackets off a string/identifier-literal computed key, but object comprehensions require
the [expr] key syntax, so the formatted output is rejected by the evaluator.

Repro (v0.22.0):

comp.jsonnet:

{['0']: 1 for x in [1]}
$ jsonnet comp.jsonnet
{
   "0": 1
}

$ jsonnetfmt comp.jsonnet
{ '0': 1 for x in [1] }

$ jsonnetfmt comp.jsonnet | jsonnet -
<stdin>:1:10-13 Object comprehensions can only have [e] fields

So formatting a valid file yields a file that fails to evaluate.

The bug is specific to comprehension keys that are simple literals. Complex keys keep their
brackets and stay valid:

$ echo '{[std.toString(0)]: 1 for x in [1]}' | jsonnetfmt -
{ [std.toString(0)]: 1 for x in [1] }      # correct, still parses

Root cause looks like the ['literal'] -> literal field simplification (correct for normal
objects, since {['a']: 1} == {a: 1}) is being applied inside object comprehensions, where
that rewrite is not valid. The fix would be to skip the simplification when the field is part of
a comprehension.

Found via a formatter idempotence check and confirmed with the repro above on v0.22.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions