Skip to content

Try/catch in update assignment #2220

@odnoletkov

Description

@odnoletkov

Describe the bug

try/catch produce unexpected results when used in the rhs of the update assignment |=

To Reproduce

$ jq -n '1 | . |= 2' # OK
2

$ jq -n '1 | . |= try 2' # BUG, expected: 2 
null

$ jq -n '1 | . |= try 2 catch 3' # BUG, expected: 2
3

$ jq -n '1 | . |= try 2 catch .' # BUG, expected: 2
{
  "__jq": 0
}

$ jq -n '["1", "2a", "3", 4] | .[] |= try tonumber' # BUG, expected: [1, null, 3, 4]
[
  "2a",
  4
]

Plain assignment = and surprisingly even arithmetic update-assignments such as += work as expected:

$ jq -n '1 | . = try 2' # OK
2

$ jq -n '1 | . += try 2' # OK
3

Throwing the error also works as expected

$ jq -n '1 | . |= try error("err")' # OK
null

$ jq -n '1 | . |= try error("err") catch 2' # OK
2

Environment

% jq --version; uname; sw_vers -productVersion
jq-1.6
Darwin
10.15.7
$ jq --version; uname
jq-1.6
Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions