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

Keep new line at end of file in yamlprocessor #4033

Merged
merged 9 commits into from Jan 10, 2023

Conversation

kurochan
Copy link
Contributor

@kurochan kurochan commented Nov 30, 2022

What this PR does / why we need it:
Currently, yamlprocessor doesn't respect new line at end of file if the input file contains it or not.
This behavior causes unnecessary differences and should be corrected.

For example, EventWatcher generates these changes.
image

Which issue(s) this PR fixes:

Fixes #

Does this PR introduce a user-facing change?:

Keep new line at end of file in yamlprocessor

@@ -253,6 +253,16 @@ foo:
- baz`),
wantErr: false,
},
{
name: "new line at end of yml given",
yml: `foo: bar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if raw yaml has more than one \n at the end of file 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@khanhtc1202 It's just as you said...

How about simply always adding a new line at the EOF?

According to the POSIX Stndard, The definition of a LINE is

A sequence of zero or more non- characters plus a terminating character.

https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, I guess diff will be shown if the input file does not contain a new line at EOF, thus the idea of making no diff caused by a new line at EOF is not guaranteed.

I think we should update the logic to count new lines at EOF and add (if necessary) new line(s) to the end of parsed file, wdyt? 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion is to always add single newline to the end of the output file, whether or not there are some newlines at the end of the source file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your suggestion 👍 How about changing it to: add a new line to the EOF in case of there is no \n at EOF? If we just add a new line anyway (regardless there is a new line at the EOF or not), in case there is already one \n at EOF, we added an unnecessary new line and could cause a diff shown, wdyt?

Copy link
Contributor Author

@kurochan kurochan Dec 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is no \n at EOF in generated yaml. Because yamlprocessor calls here (https://github.com/goccy/go-yaml/blob/master/ast/ast.go#L610 ).

return strings.Join(doc, "\n")

Based on this, which of the following should I do?

  1. add a new line to the EOF
  2. add a new line to the EOF in case of there is no \n at EOF
  3. Make Pull Request to goccy/go-yaml to append \n at EOF
  4. Do 2 and 3

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (2) is good enough for pipecd, about (3) may need discuss on goccy/go-yaml repo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll fix it in the way of (2).

And (3) will discuss on goccy/go-yaml#329 .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed cd484f0

@khanhtc1202
Copy link
Member

@kurochan there is a failed test, pls re-check it 😄

@kurochan
Copy link
Contributor Author

kurochan commented Dec 8, 2022

@khanhtc1202
I'm struggling on test fail on pkg/app/piped/executor/kubernetes/kubernetes_test.go .
The following differences appear and I haven't come up with a good fix yet 🤔

Diff:                                
--- Expected                                
+++ Actual                                
@@ -2,3 +2,3 @@                                
 data:                                
-  envoy-config: |-                                
+  envoy-config: |                                
     admin:                                
TestPatchManifest/multi_ops_with_a_given_field  

Do you have any ideas to fix this differences?

@khanhtc1202
Copy link
Member

@kurochan Probably it's caused by this convention
https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-in-yaml-over-multiple-lines#:~:text=Block%20styles%20with%20block%20chomping%20indicator
So I think your change should support all YAML convention includes: >, | and its mix with -, + etc

@@ -224,12 +224,17 @@ func ParseManifests(data string) ([]Manifest, error) {
manifests = make([]Manifest, 0, len(parts))
)

for _, part := range parts {
newLineAtEOF := strings.HasSuffix(data, "\n")
for i, part := range parts {
// Ignore all the cases where no content between separator.
part = strings.TrimSpace(part)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strings.TrimSpace(str) removes trailing \n, so \n at EOF is removed unexpectedly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like in case of the yaml file contains more than one empty line at the end of file, only one empty line is remained thus the diff will be shown, isn't it? In that case, lets add test to demonstrate that and I will merge this PR 🙌

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out. I have fixed it and added a test case. 0e250b1

Copy link
Member

@khanhtc1202 khanhtc1202 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Thanks for the contribution 🙌

@knanao knanao merged commit d0bf92e into pipe-cd:master Jan 10, 2023
knanao pushed a commit that referenced this pull request Jan 13, 2023
* Keep new line at end of file in yamlprocessor

* add a new line to the EOF in case of there is no  at EOF

* update comment

* update eventwatcher_test

* consider new line at EOF on parsing kubernetes manifest

* update go-yaml v1.9.8

* remove TODO because PR of go-yaml was merged

* consider to handle multiple new line at EOF

* remove indent vioration of tab
khanhtc1202 added a commit that referenced this pull request Jan 13, 2023
… (#4128)

* Keep new line at end of file in yamlprocessor (#4033)

* Keep new line at end of file in yamlprocessor

* add a new line to the EOF in case of there is no  at EOF

* update comment

* update eventwatcher_test

* consider new line at EOF on parsing kubernetes manifest

* update go-yaml v1.9.8

* remove TODO because PR of go-yaml was merged

* consider to handle multiple new line at EOF

* remove indent vioration of tab

* support standalone ECS task (#4084)

Fixes #2734

* Small lint fix (#4097)

* fix null pointer (#4102)

* fix null pointer

* apply review

* Add example for ecs standalone task (#4104)

* add example for ecs standalone task

* add example for ecs standalone task

* wip

* add yaml example to docs.

* add example for ecs standalone task

* wip

* add yaml example to docs.

* apply review

* Update docs/content/en/docs-dev/user-guide/managing-application/defining-app-configuration/ecs.md

Co-authored-by: Khanh Tran <32532742+khanhtc1202@users.noreply.github.com>

Co-authored-by: Khanh Tran <32532742+khanhtc1202@users.noreply.github.com>

* Add update contributions command (#4114)

* Add update contributions command

* Update hack/gen-contributions.sh

* Run update go deps (#4117)

* feature: cli and grpc to disable application (#4119)

* Add new line in detailsFormat to fix plan preview format (#4122)

* add document how to disable (#4123)

Co-authored-by: Kurochan <kuro@kurochan.org>
Co-authored-by: Tomoki Hori <50762864+TonkyH@users.noreply.github.com>
Co-authored-by: Khanh Tran <32532742+khanhtc1202@users.noreply.github.com>
Co-authored-by: kevin55156 <68955641+kevin55156@users.noreply.github.com>
Co-authored-by: Yoshiaki Ishihara <39365493+yoiki@users.noreply.github.com>
This was referenced Feb 7, 2023
@github-actions github-actions bot mentioned this pull request Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants