Skip to content

Select comparison falling back to zero used to return zero but returns empty string #2419

Open
@samrocketman

Description

@samrocketman

Affected yq versions

  • Expected behavior works in yq 4.45.1 and yq 4.45.2
  • Bug experienced on 4.45.3 and later
  • Bug experienced on latest 4.46.1

Bug description

YAML file sample.yaml

checksums:
  download-utilities.sh: "a string but if this is a map then it should return length"

Command

yq -r 'select(.checksums."download-utilities.sh" | type == "!!map").checksums."download-utilities.sh" | keys | length // 0' misc/download-utilities.yml

results an in empty string.

Expected behavior

It used to result in 0. I am going to fix my script but not falling back to zero // 0 feels like a bug since this used to work in older versions of yq.

Shell script verifying the bug

# set up testing

cat > sample.yaml <<'EOF'
checksums:
  download-utilities.sh: "a string but if this is a map then it should return length"
EOF

curl -sSfLo yq_4.45.2 https://github.com/mikefarah/yq/releases/download/v4.45.2/yq_linux_amd64

curl -sSfLo yq_4.45.3 https://github.com/mikefarah/yq/releases/download/v4.45.3/yq_linux_amd64

chmod 755 yq_4.45.2 yq_4.45.3

# 4.45.2 returns zero i.e. "0"

./yq_4.45.2 -r 'select(.checksums."download-utilities.sh" | type == "!!map").checksums."download-utilities.sh" | keys | length // 0' sample.yaml

# 4.45.3 returns zero length string or empty string in shell

./yq_4.45.3 -r 'select(.checksums."download-utilities.sh" | type == "!!map").checksums."download-utilities.sh" | keys | length // 0' sample.yaml

Real world example usage

This is currently broken on my project. I plan to fix this script to account for empty string or zero.

https://github.com/samrocketman/yml-install-files/blob/33f873043002ef1923859046652b284581b988b6/download-utilities.sh#L643-L644

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions