carvel-dev / ytt

YAML templating tool that works on YAML structure instead of text
https://carvel.dev/ytt
Apache License 2.0
1.62k stars 136 forks source link

ytt fmt changes yaml structure #817

Open Zebradil opened 1 year ago

Zebradil commented 1 year ago

What steps did you take:

$ cat <<EOF > test.yaml
top-level list with empty map:
- {}
another top-level element: 42
EOF

$ diff -u test.yaml <(ytt fmt -f test.yaml)

What happened:

Notice how the empty map is replaced by another top-level element, which becomes a list element of top-level list with empty map.

--- test.yaml   2023-03-27 16:49:19.973100152 +0200
+++ /proc/self/fd/11    2023-03-27 16:49:51.349538852 +0200
@@ -1,3 +1,2 @@
 top-level list with empty map:
-- {}
-another top-level element: 42
+  - another top-level element: 42

What did you expect:

The output yaml is structurally identical to the input yaml.

Environment:


Vote on this request

This is an invitation to the community to vote on issues, to help us prioritize our backlog. Use the "smiley face" up to the right of this comment to vote.

πŸ‘ "I would like to see this addressed as soon as possible" πŸ‘Ž "There are other more important things to focus on right now"

We are also happy to receive and review Pull Requests if you want to help working on this issue.

github-actions[bot] commented 1 year ago

This issue is being marked as stale due to a long period of inactivity and will be closed in 5 days if there is no response.

Zebradil commented 1 year ago

Nah, not stale.

vmunishwar commented 1 year ago

@Zebradil Sorry for the delay in response. This seems to be a bug and we are able to reproduce this. We would like to know if this is blocking your use of ytt and also understand more about your use case. Are you using ytt fmt to get the output well formatted?

Zebradil commented 1 year ago

Hey @vmunishwar, no worries. I'll provide more context.

I have a workflow in which I want to format yaml files in a consistent way. I was using yq for that but noticed that there is ytt fmt, which would've been a good opportunity to eliminate yq in my workflow (it is not used for anything else). It's a pity that ytt fmt didn't work, so I decided to contribute to the project with this bug report :-)

However, this is not blocking me. In golang code, I format yaml files with the yaml package and outside I still use yq.

mbbush commented 4 months ago

I've run into this same issue. This seems like it renders ytt fmt basically unusable, unless I happen to know that none of the yaml files I'm going to process with it contain empty objects, or empty arrays, or perhaps other things that trigger this bug.

What good is a code formatting tool that changes the meaning of your code?

prembhaskal commented 4 months ago

will check this issue this week.