Skip to content

Expressions inline with a slice are ignored or cause parse errors #2280

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

Open
n4zukker opened this issue Feb 8, 2025 · 2 comments
Open

Expressions inline with a slice are ignored or cause parse errors #2280

n4zukker opened this issue Feb 8, 2025 · 2 comments
Labels

Comments

@n4zukker
Copy link
Contributor

n4zukker commented Feb 8, 2025

Describe the bug
The slice array operator does not work as expected when used after an expression.

  1. Since ["cat", "dog", "frog", "cow"][1] evaluates to "dog",
    I would expect ["cat", "dog", "frog", "cow"][1:] to evaluate to ["dog", "frog", "cow"].
    But it acts as .[1:] was written and returns the slice of the input.

  2. An error is returned in the case of the "without the first number" array slice.

yq should give an error in both cases or, better yet, support both. In the first case, it's a bug to accept the syntax and ignore the left hand side. In the second case, the error message is not intuitive.

Version of yq: 4.45.1
Operating system: ubuntu
Installed via: direct download from https://github.com/mikefarah/yq/releases/download/v4.45.1/yq_linux_amd64

Input Yaml
Concise yaml document(s) (as simple as possible to show the bug, please keep it to 10 lines or less)
data1.yml:

- 'entry 1'
- 'entry 2'
- 'entry 3'

Command
The command you ran:

./yq_linux_amd64  '["cat", "dog", "frog", "cow"][1:]' data1.yml

Actual behavior

- 'entry 2'
- 'entry 3'

Expected behavior

- dog
- frog
- cow

Command
The command you ran:

./yq_linux_amd64  '["cat", "dog", "frog", "cow"][1:2]' data1.yml

Actual behavior

- 'entry 2'

Expected behavior

- dog

Command
The command you ran:

yq '["cat", "dog", "frog", "cow"][:2]' data1.yml ;
yq '"expr" | ["cat", "dog", "frog", "cow"][:2]' data1.yml

Actual behavior

Error: '' expects 2 args but there is 1
Error: '|' expects 2 args but there is 1

Expected behavior

- cat
- dog

- cat
- dog

Additional context
.b[1:] acts similarly, as does $arr[1:].

Once you are aware of this bug, you can work around it by moving the expression to input and using a dot, .[n:m]. E.g. instead of
["cat", "dog", "frog", "cow"][1:] write
["cat", "dog", "frog", "cow"] | .[1:]
but until then it's a head scratcher for either of the cases.

Ref: Slice array man page: https://mikefarah.gitbook.io/yq/operators/slice-array

@n4zukker
Copy link
Contributor Author

n4zukker commented Feb 8, 2025

Found this discussion topic about the same issue: #1554

@erhhung
Copy link

erhhung commented Apr 25, 2025

I found the same behavior using the same version (v4.45.1 on yq_darwin_amd64) but with a slightly different example.
It appears that yq doesn't like any array reference other than . in front of the slicing operator [$i : $j]:

Input doc test.yaml:

- name: amy
  age: 20
- name: bob
  age: 22
- name: cal
  age: 24
- name: dan
  age: 26
- name: eve
  age: 28

Return all elements starting from "dan":

# first, perform sanity check, showing index of starting element
$ yq '. as $d | .[] | select(.name == "dan") | path[0] as $i | $i' test.yaml
3

# this work, returning the starting element
$ yq '. as $d | .[] | select(.name == "dan") | path[0] as $i | $d[$i]' test.yaml
name: dan
age: 26

# this doesn't work!
$ yq '. as $d | .[] | select(.name == "dan") | path[0] as $i | $d[$i:]' test.yaml
!!map []

# but this does!
$ yq '. as $d | .[] | select(.name == "dan") | path[0] as $i | $d | .[$i:]' test.yaml
- name: dan
  age: 26
- name: eve
  age: 28

Some more bizzare query output:

# also doesn't work, explicitly specifying ending index
$ yq '. as $d | .[] | select(.name == "dan") | path[0] as $i | $d[$i : $i+1]' test.yaml
!!map
- 26

# huh? what's the difference from the previous example other than the element
$ yq '. as $d | .[] | select(.name == "bob") | path[0] as $i | $d[$i : $i+1]' test.yaml
!!map
- bob

# wtf! now I'm really confused
$ yq '. as $d | .[] | select(.name == "cal") | path[0] as $i | $d[$i : $i+1]' test.yaml
!!map
- age

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