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

fix(package parser): only drop fully commented files #548

Merged
merged 2 commits into from
Sep 21, 2023

Conversation

phisco
Copy link
Contributor

@phisco phisco commented Sep 15, 2023

Description of your changes

Fixes crossplane/crossplane#4631

I have:

  • Read and followed Crossplane's contribution process.
  • Run make reviewable test to ensure this PR is ready for review.

How has this code been tested

Added unit tests and updated existing tests.

Signed-off-by: Philippe Scorsolini <p.scorsolini@gmail.com>
@phisco phisco requested a review from turkenh September 15, 2023 16:13
@phisco phisco requested review from a team as code owners September 15, 2023 16:13
Copy link
Member

@jbw976 jbw976 left a comment

Choose a reason for hiding this comment

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

Thanks for a very quick turnaroud on this @phisco!!! 💪 - a couple small comments for your consideration, but I've already added an approval ✅

@@ -130,19 +130,16 @@ func (p *PackageParser) Parse(_ context.Context, reader io.ReadCloser) (*Package
return pkg, nil
}

// cleanYAML cleans up YAML by removing empty and commented out lines which
// cleanYAML cleans up YAML only drops fully commented inputs which would
Copy link
Member

@jbw976 jbw976 Sep 15, 2023

Choose a reason for hiding this comment

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

This is reading a little off now - is it correct to say the following? I'm trying to be more complete about what this function is doing and returning - feel free to disregard the completeness aspect if its wrong or you don't think its needed, but the sentence as is is missing some flow 🤓

Suggested change
// cleanYAML cleans up YAML only drops fully commented inputs which would
// cleanYAML cleans up the input YAML by removing all leading and trailing white
// space from each line. If the input yaml is completely commented, then nil
// will be returned to avoid issues with decoding.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nono, I'm not dropping commented lines, i am ONLY dropping the whole file if it's content is only composed of commented lines (or separators).

The current implementation was dropping all lines starting with a # considering them as comments, but that's not always true, for example that's not the case if a line is part of a multiline string. Properly removing commented lines would require a context-aware parser, but given that the change was made just to avoid fully empty files that would crash the decoder, there is no need to get into so much complexity.

args: args{in: []byte("")},
want: want{out: nil},
},
"CommentedOut": {
Copy link
Member

Choose a reason for hiding this comment

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

good test cases! one thing that is nagging my mind here is if there's a scenario @muvaf was handling with the original function that really needed all commented lines removed. It seems like the conclusion here is that that was too aggressive, and its OK to leave in commented lines if the full block isn't completely commented.

You feel good about that too? There is not a scenario where commented lines should actually be removed? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From the description of the original PR my understanding was that only fully commented files were an issue, so being less aggressive should be fine. The current behavior in released versions of Crossplane is to not drop any line at all, so I guess there should be no issue only dropping fully commented files.

Copy link
Member

@muvaf muvaf Sep 19, 2023

Choose a reason for hiding this comment

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

@phisco is right, my motivation was to have the parser skip fully-commented documents so we don't have errors in the upstream marshal/unmarshal functions. That's why I added only fully-commented doc cases to the unit tests in the original PR. It was essentially extending the definition of empty document from having no characters at all to having all lines commented out. Not sure why I ended up removing any lines, maybe while trying to reduce the number of loops?

I think this function can just be a check like isEmpty and not make any changes to the document - the problem isn't about having comments but more about having the whole document not have any useful data hence failing the functions trying to cast it onto a struct.

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 @muvaf for the feedback, I'll rename the function to IsEmpty and make sure it doesn't have any side effect 👍

// We don't want to return an empty document with only separators that
// have nothing in-between.
if empty && trimmed != "---" && trimmed != "..." {
if empty && trimmed != "" && trimmed != "---" && trimmed != "..." && !strings.HasPrefix(trimmed, "#") {
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we do anything like that on the text level? Can't we just decode and see that a null object (no keys) comes out? Would be much safer than any complicated risky logic here.

Copy link
Member

Choose a reason for hiding this comment

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

We use a high level Decode function from upstream which does not have a specific error type for empty documents as far as I remember (@phisco might know better now) and checking only for null would not be specific enough due to number of reasons it can be null.

Signed-off-by: Philippe Scorsolini <p.scorsolini@gmail.com>
Copy link
Member

@turkenh turkenh left a comment

Choose a reason for hiding this comment

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

Thanks @phisco 🙌

@phisco phisco merged commit 2d96b39 into crossplane:master Sep 21, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shebang lines removed in patches during compositions installation with configurations Master/RC-1.14
5 participants