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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/install/npm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -824,15 +824,26 @@ pub const PackageManifest = struct {
{
const releases = this.pkg.releases.keys.get(this.versions);
var i = releases.len;

var best_match_version: ?Semver.Version = null;
var best_match_package: ?PackageVersion = null;
// For now, this is the dumb way
while (i > 0) : (i -= 1) {
const version = releases[i - 1];
const packages = this.pkg.releases.values.get(this.package_versions);

if (group.satisfies(version)) {
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)))
{
best_match_version = version;
best_match_package = this.pkg.releases.values.get(this.package_versions)[i - 1];
}
}

if (best_match_version != null and best_match_package != null) {
return .{ .version = best_match_version.?, .package = &best_match_package.? };
}
}

if (group.flags.isSet(Semver.Query.Group.Flags.pre)) {
Expand Down
3 changes: 3 additions & 0 deletions test/cli/install/migration/complex-workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,6 @@ mustNotExist("packages/second/node_modules/body-parser/node_modules/body-parser/
mustNotExist("packages/second/node_modules/body-parser/node_modules/iconv-lite");
mustNotExist("packages/second/node_modules/iconv-lite");
mustNotExist("node_modules/iconv-lite");

// Should pick 1.2.4, not 1.2.0.
validate("node_modules/lines-and-columns", "1.2.4");
1 change: 1 addition & 0 deletions test/cli/install/migration/complex-workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"hello": "file:hello-0.3.2.tgz",
"install-test": "bitbucket:dylan-conway/public-install-test",
"install-test1": "git+ssh://[email protected]/dylan-conway/install-test.git#596234dab30564f37adae1e5c4d7123bcffce537",
"lines-and-columns": "^1.1.6",
"public-install-test": "gitlab:dylan-conway/public-install-test",
"svelte": "4.1.2"
},
Expand Down