Skip to content

Update-assignment //= always evaluates right-hand-side #2410

Open
@wader

Description

@wader

Describe the bug

RHS of a update-assignment //= expression is always evaluated even if LHS is null or false. As jq has some non-pure functions like input and debug it might cause unexpected behaviour. For example I think it should be possible to use debug so see if RHS gets evaluated or not.

To Reproduce

$ jq -n '{a:1} | .a //= (debug | 2)'
["DEBUG:",{"a":1}]
{
  "a": 1
}
$ jq -n '{b:1} | .a //= (debug | 2)'
["DEBUG:",{"b":1}]
{
  "b": 1,
  "a": 2
}

Expected behavior

That RHS is only evaluated if LHS is null or false.

Environment:

macOS 12.1

$ jq --version
jq-1.6-139-ga9ce724

Was discovered when discussing #2407

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions