Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array subtraction unexpectedly modifies its LHS #2159

Closed
dsharp-pivotal opened this issue Sep 25, 2024 · 3 comments
Closed

Array subtraction unexpectedly modifies its LHS #2159

dsharp-pivotal opened this issue Sep 25, 2024 · 3 comments
Labels

Comments

@dsharp-pivotal
Copy link

Describe the bug

Array subtraction appears to modify its LHS. When the LHS is a variable that is used again later, this has unexpected results.

Version of yq:

$ yq --version
yq (https://github.com/mikefarah/yq/) version v4.44.3

Operating system: macOS
Installed via: homebrew

Input Yaml

["a", "b"]

Command
The command you ran:

$ <<<'["a", "b"]' yq e '["a","b"] as $f | {"0": $f, "1": $f - ., "2": $f}'

Actual behavior

The variable appears to be modified. .2 is empty.

"0":
  - a
  - b
"1": []
"2": []

Expected behavior

.2 should contain the assigned value of $f, ["a", "b"]

Compare to re-using a variable without doing the array subtraction:

$ <<<'["a", "b"]' yq e '["a","b"] as $f | {"0": $f, "2": $f}'
"0":
  - a
  - b
"2":
  - a
  - b

If the LHS is the context ., the same thing occurs:

$ <<<'["a", "b"]' yq e '["a","b"] as $f | {"0": ., "1": . - $f, "2": .}'
"0": ["a", "b"]
"1": []
"2": []

As a point of comparison, jq does not have this behavior:

$ <<<'["a", "b"]' jq '["a","b"] as $f | {"0": $f, "1": ($f - .) , "2": $f}'
{
  "0": [
    "a",
    "b"
  ],
  "1": [],
  "2": [
    "a",
    "b"
  ]
}

Additional context

I think this is due to this line:
https://github.com/mikefarah/yq/blob/master/pkg/yqlib/operator_subtract.go#L41

	// removing children from LHS, parent hasn't changed
	lhs.Content = newLHSArray

I don't understand the intent expressed by the comment.

@mikefarah
Copy link
Owner

Thanks for raising, will fix in the next release

@mikefarah
Copy link
Owner

Fixed in 4.44.5

tmeijn pushed a commit to tmeijn/dotfiles that referenced this issue Nov 20, 2024
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [mikefarah/yq](https://github.com/mikefarah/yq) | patch | `v4.44.3` -> `v4.44.5` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>mikefarah/yq (mikefarah/yq)</summary>

### [`v4.44.5`](https://github.com/mikefarah/yq/releases/tag/v4.44.5)

[Compare Source](mikefarah/yq@v4.44.3...v4.44.5)

-   Format comments with a gray foreground (Thanks [@&#8203;gabe565](https://github.com/gabe565))
-   Fixed handling of nulls with sort_by expressions [#&#8203;2164](mikefarah/yq#2164)
-   Force no color output when NO_COLOR env presents (Thanks [@&#8203;narqo](https://github.com/narqo))
-   Fixed array subtraction update bug [#&#8203;2159](mikefarah/yq#2159)
-   Fixed index out of range error
-   Can traverse straight from parent operator (parent.blah)
-   Bumped dependencies

Note: 4.44.4 was skipped as it had a release pipeline failure

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40NDAuNyIsInVwZGF0ZWRJblZlciI6IjM3LjQ0MC43IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
@dsharp-pivotal
Copy link
Author

Verified. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants