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

Empty docs break sort_by() #2164

Open
matthiasvegh opened this issue Oct 3, 2024 · 0 comments
Open

Empty docs break sort_by() #2164

matthiasvegh opened this issue Oct 3, 2024 · 0 comments
Labels

Comments

@matthiasvegh
Copy link

matthiasvegh commented Oct 3, 2024

Describe the bug

If there are empty documents passed into eval-all, sorting only works before and after the empty document.

Version of yq: 4.43.1
Operating system: Ubuntu 20.04
Installed via: wget https://github.com/mikefarah/yq/releases/download/v4.43.1/yq_linux_amd64

Input Yaml
data1.yml:

---
kind: Service
metadata:
  name: b
---
kind: Deployment
metadata:
  name: a
---
# Empty document, meaningful
---
kind: ConfigMap
metadata:
  name: e
---
kind: ConfigMap
metadata:
  name: d

Command
The command you ran:

yq eval-all '[.] | sort_by(.kind, .metadata.name) | .[] | split_doc'

Actual behavior

kind: Deployment
metadata:
  name: a
---
kind: Service
metadata:
  name: b
---

---
kind: ConfigMap
metadata:
  name: d
---
kind: ConfigMap
metadata:
  name: e

Expected behavior

---
kind: ConfigMap
metadata:
  name: d
---
kind: ConfigMap
metadata:
  name: e
---
kind: Deployment
metadata:
  name: a
---
kind: Service
metadata:
  name: b

Additional context
It appears that empty documents behave like an ordering boundary, where everything before it is sorted correctly, as is everything after, but not across. Curiously, if instead of sort_by(.kind, .metadata.name) I do sort_by("\(.kind), \(.metadata.name)") everything is sorted correctly.

My actual motivation is to compare Kubernetes manifests as in #1101. In my case, the manifests are produced by Helm, which can sometimes emit empty documents if the manifest is not needed.

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

1 participant