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

Sort dependencies before writing to pom.xml #1113

Merged
merged 5 commits into from
Jul 12, 2024
Merged

Sort dependencies before writing to pom.xml #1113

merged 5 commits into from
Jul 12, 2024

Conversation

cuixq
Copy link
Contributor

@cuixq cuixq commented Jul 11, 2024

Currently, the write test is a bit flaky because the dependencies to be added are not sorted and their order in pom.xml is not guaranteed.

This PR adds sorting of dependencies before they are going to be written.

@codecov-commenter
Copy link

codecov-commenter commented Jul 11, 2024

Codecov Report

Attention: Patch coverage is 58.53659% with 17 lines in your changes missing coverage. Please review.

Project coverage is 65.99%. Comparing base (86c6f40) to head (bb3976c).

Files Patch % Lines
internal/resolution/manifest/maven.go 58.53% 13 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1113      +/-   ##
==========================================
+ Coverage   65.96%   65.99%   +0.03%     
==========================================
  Files         160      160              
  Lines       12906    12910       +4     
==========================================
+ Hits         8513     8520       +7     
+ Misses       3919     3917       -2     
+ Partials      474      473       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cuixq cuixq marked this pull request as ready for review July 11, 2024 03:50
@cuixq cuixq requested a review from michaelkedar July 11, 2024 03:50
Copy link
Member

@michaelkedar michaelkedar left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 631 to 632
sort.Slice(dm.Dependencies, func(i, j int) bool {
di := dm.Dependencies[i]
dj := dm.Dependencies[j]
if di.GroupID != dj.GroupID {
return di.GroupID < dj.GroupID
}
if di.ArtifactID != dj.ArtifactID {
return di.ArtifactID < dj.ArtifactID
}
if di.Type != dj.Type {
return di.Type < dj.Type
}
if di.Classifier != dj.Classifier {
return di.Classifier < dj.Classifier
}

return di.Version < dj.Version
return compareDependency(dm.Dependencies[i], dm.Dependencies[j])
Copy link
Member

Choose a reason for hiding this comment

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

Not something to change, but something I've wondered about:
Is there a reason to use sort.Slice over slices.SortFunc, or is it just a preference thing?

I guess technically slices.SortFunc would make this one line instead of two:

slices.SortFunc(dm.Dependencies, compareDependency)

Copy link
Collaborator

@G-Rath G-Rath Jul 11, 2024

Choose a reason for hiding this comment

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

fwiw the docs technically recommend you do change it:

Note: in many situations, the newer slices.SortFunc function is more ergonomic and runs faster.

though I think when I looked into it, it felt like their comment was more based on what types you were using that lead to different compare functions rather than "we've spent more time optimizing slices.SortFunc than this one" kind of thing?

also kind of related, here's some prior art on how I implemented a chain of compares similar to what you've done with compareDependency - I don't think your function is bad or needs changing, but I figured you might be interested from a consistency POV

Also also, note that there's cmp.Or which might be useful here, but also might not be... 🤷

Copy link
Contributor Author

@cuixq cuixq Jul 11, 2024

Choose a reason for hiding this comment

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

Nice to know both slice.SortFunc and cmp.Or!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sadly cmp.Or only for Go 1.22 :(

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ek sorry I learned that the same hard way when I first discovered it 😬

}

func compareDependency(d1, d2 dependency) int {
return cmp.Or(cmp.Compare(d1.GroupID, d2.GroupID), cmp.Compare(d1.ArtifactID, d2.ArtifactID),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I think it'd be more readable if you put each condition on a newline

@cuixq cuixq requested a review from another-rex July 11, 2024 07:24
@cuixq cuixq merged commit 9ab83d0 into google:main Jul 12, 2024
13 checks passed
@cuixq cuixq deleted the fix branch July 12, 2024 01:16
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.

4 participants