diff --git a/src/install/npm.zig b/src/install/npm.zig index 4cf1c2b71af06c..c9964e3ba793b8 100644 --- a/src/install/npm.zig +++ b/src/install/npm.zig @@ -823,16 +823,26 @@ pub const PackageManifest = struct { { const releases = this.pkg.releases.keys.get(this.versions); + + var best_match_version: ?Semver.Version = null; + var best_match_package_index: usize = 0; var i = releases.len; // 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_index = i - 1; } } + + if (best_match_version != null) { + const packages = this.pkg.releases.values.get(this.package_versions); + return .{ .version = best_match_version.?, .package = &packages[best_match_package_index] }; + } } if (group.flags.isSet(Semver.Query.Group.Flags.pre)) { diff --git a/test/cli/install/migration/complex-workspace.test.ts b/test/cli/install/migration/complex-workspace.test.ts index ec4669a3b66c8f..fa9cfe80c2ebb5 100644 --- a/test/cli/install/migration/complex-workspace.test.ts +++ b/test/cli/install/migration/complex-workspace.test.ts @@ -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"); diff --git a/test/cli/install/migration/complex-workspace/package.json b/test/cli/install/migration/complex-workspace/package.json index 55ed302cbeaa03..68a17e643e3ebd 100644 --- a/test/cli/install/migration/complex-workspace/package.json +++ b/test/cli/install/migration/complex-workspace/package.json @@ -7,6 +7,7 @@ "hello": "file:hello-0.3.2.tgz", "install-test": "bitbucket:dylan-conway/public-install-test", "install-test1": "git+ssh://git@github.com/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" },