Skip to content

Commit 7e3d271

Browse files
segiddinshsbt
authored andcommitted
[rubygems/rubygems] Install the best matching gem for the current platform in gem install
Instead of picking essentially a random matching platform Signed-off-by: Samuel Giddins <[email protected]> ruby/rubygems@3727096297
1 parent b5beb19 commit 7e3d271

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/rubygems/resolver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def search_for(dependency)
241241

242242
sources.each do |source|
243243
groups[source].
244-
sort_by {|spec| [spec.version, spec.platform =~ Gem::Platform.local ? 1 : 0] }. # rubocop:disable Performance/RegexpMatch
244+
sort_by {|spec| [spec.version, -Gem::Platform.platform_specificity_match(spec.platform, Gem::Platform.local)] }.
245245
map {|spec| ActivationRequest.new spec, dependency }.
246246
each {|activation_request| activation_requests << activation_request }
247247
end

test/rubygems/test_gem_commands_install_command.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,38 @@ def test_install_gem_ignore_dependencies_remote_platform_local
10051005
assert_equal %W[a-3-#{local}], @cmd.installed_specs.map(&:full_name)
10061006
end
10071007

1008+
def test_install_gem_platform_specificity_match
1009+
util_set_arch "arm64-darwin-20"
1010+
1011+
spec_fetcher do |fetcher|
1012+
%w[ruby universal-darwin universal-darwin-20 x64-darwin-20 arm64-darwin-20].each do |platform|
1013+
fetcher.download "a", 3 do |s|
1014+
s.platform = platform
1015+
end
1016+
end
1017+
end
1018+
1019+
@cmd.install_gem "a", ">= 0"
1020+
1021+
assert_equal %w[a-3-arm64-darwin-20], @cmd.installed_specs.map(&:full_name)
1022+
end
1023+
1024+
def test_install_gem_platform_specificity_match_reverse_order
1025+
util_set_arch "arm64-darwin-20"
1026+
1027+
spec_fetcher do |fetcher|
1028+
%w[ruby universal-darwin universal-darwin-20 x64-darwin-20 arm64-darwin-20].reverse_each do |platform|
1029+
fetcher.download "a", 3 do |s|
1030+
s.platform = platform
1031+
end
1032+
end
1033+
end
1034+
1035+
@cmd.install_gem "a", ">= 0"
1036+
1037+
assert_equal %w[a-3-arm64-darwin-20], @cmd.installed_specs.map(&:full_name)
1038+
end
1039+
10081040
def test_install_gem_ignore_dependencies_specific_file
10091041
spec = util_spec "a", 2
10101042

0 commit comments

Comments
 (0)