-
Notifications
You must be signed in to change notification settings - Fork 163
Keep new line at end of file in yamlprocessor #4033
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
Conversation
@@ -253,6 +253,16 @@ foo: | |||
- baz`), | |||
wantErr: false, | |||
}, | |||
{ | |||
name: "new line at end of yml given", | |||
yml: `foo: bar |
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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? 👀
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
- add a new line to the EOF
- add a new line to the EOF in case of there is no
\n
at EOF - Make Pull Request to
goccy/go-yaml
to append\n
at EOF - Do 2 and 3
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed cd484f0
@kurochan there is a failed test, pls re-check it 😄 |
@khanhtc1202
Do you have any ideas to fix this differences? |
@kurochan Probably it's caused by this convention |
fabf67e
to
092ba53
Compare
@@ -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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🙌
There was a problem hiding this comment.
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
092ba53
to
5cf9831
Compare
5cf9831
to
040f9e2
Compare
7cf5204
to
10b776b
Compare
10b776b
to
d0cae68
Compare
There was a problem hiding this 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 🙌
* 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
… (#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>
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.

Which issue(s) this PR fixes:
Fixes #
Does this PR introduce a user-facing change?: