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 findBestMatch so it finds the best match and not the first match #6611

Merged
merged 5 commits into from
Oct 20, 2023

Conversation

dylang
Copy link
Contributor

@dylang dylang commented Oct 20, 2023

What does this PR do?

Fixes findBestMatch.

Bug Explained

findBestMatch was picking the first version that satisfies semver range, but registries don't return versions in any particular order.

This was easily repeatable with lines-and-columns@^1.1.6, it would always pick the first version 1.2.0, which happens to be deprecated and broken, instead of the best match, which is 1.2.4.

This PR should help with a bunch of install-related bugs:

  • Code changes

How did you verify your code works?

This modifies zig code

  • I checked the lifetime of memory allocated to verify it's (1) freed and (2) only freed when it should be
  • I or my editor ran zig fmt on the changed files
  • I included a test for the new code, or an existing test covers it

First time Zig user

I've contributed to Bun before, but this is my first time contributing to the zig code. I installed Zig and its dependencies this morning. I'm happy to accept input to improve this PR!

@dylang
Copy link
Contributor Author

dylang commented Oct 20, 2023

Thanks again for the review @Jarred-Sumner. I've made the changes requested. It's a lot cleaner now thanks to your feedback, and I understand even more!

return .{ .version = version, .package = &packages[i - 1] };
// If we find one that matches, save it, but keep looping because we might find a newer match.
// The versions from the registry are not in any particular order.
if (group.satisfies(version) and (best_match_version == null or Semver.Version.order(version, best_match_version.?, "", "") == .gt)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to account for pre/post tags? Usually the way we do this is to get the string buffer from lockfile.buffers.string_bytes.items and pass that to the parent function

Suggested change
if (group.satisfies(version) and (best_match_version == null or Semver.Version.order(version, best_match_version.?, "", "") == .gt)) {
if (group.satisfies(version) and (best_match_version == null or Semver.Version.order(version, best_match_version.?, string_bytes, string_bytes) == .gt)) {

@Jarred-Sumner
Copy link
Collaborator

I have some changes I'd like to make to this PR but it's a great start so I'm going to go ahead and merge it and then apply my changes afterwards. Thank you.

@Jarred-Sumner Jarred-Sumner merged commit 184528e into oven-sh:main Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants