Repository: ruby/rubygems (Ruby)

Issue:
Serving gem with multiple platforms from s3 bucket doesn't work
### Describe the problem as clearly as you can

Using s3 bucket to serve gem with multiple platforms leads to "Could not find a valid gem" error.

### Did you try upgrading RubyGems?

Yes

### Post steps to reproduce the problem

1. Build multiple gem versions for different platforms (`mygem-0.0.1-x86_64-linux.gem`, `mygem-0.0.1-x86_64-darwin.gem`, `mygem-0.0.1-arm64-darwin.gem` in my case)
4. Create a bucket on s3 
5. Generate index with `gem generate_index` and upload it to s3 bucket
6. Add s3 bucket as a source to `~/.gemrc`
7. Try to install gem from s3 bucket to a machine with compatible platform (`arm64-darwin-21` in my case) with `gem install mygem`

### Which command did you run?

`gem install mygem`

### What were you expecting to happen?

Gem with corresponding platform is found and installed successfully.

### What actually happened?

ERROR:  Could not find a valid gem 'mygem' (>= 0) in any repository

### What I think is the reason

This seems to be caused by [`Gem::Platform.local === s.platform`](https://github.com/rubygems/rubygems/blob/master/lib/rubygems/resolver/installer_set.rb#L70)  check run on `Gem::Resolver::IndexSpecification`:
1. `Gem::Resolver::IndexSpecification` has `String` for platform ([source](https://github.com/rubygems/rubygems/blob/master/lib/rubygems/resolver/index_specification.rb#L24)) 
2. `===` method of `Gem::Platform` expects `other` to be of class `Gem::Platform` ([source](https://github.com/rubygems/rubygems/blob/master/lib/rubygems/platform.rb#L160)) so that check fails for `String` platform.

Making `Gem::Resolver::IndexSpecification` initializer set platform to `Gem::Platform.new(platform.to_s)` [here](https://github.com/rubygems/rubygems/blob/master/lib/rubygems/resolver/index_specification.rb#L24) fixes the problem.

### Run `gem env` and paste the output below

```
RubyGems Environment:
  - RUBYGEMS VERSION: 3.3.18
  - RUBY VERSION: 3.0.3 (2021-11-24 patchlevel 157) [arm64-darwin21]
  - INSTALLATION DIRECTORY: /Users/user/.rvm/gems/ruby-3.0.3
  - USER INSTALLATION DIRECTORY: /Users/user/.gem/ruby/3.0.0
  - RUBY EXECUTABLE: /Users/user/.rvm/rubies/ruby-3.0.3/bin/ruby
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /Users/user/.rvm/gems/ruby-3.0.3/bin
  - SPEC CACHE DIRECTORY: /Users/user/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/user/.rvm/rubies/ruby-3.0.3/etc
  - RUBYGEMS PLATFORMS:
     - ruby
     - arm64-darwin-21
  - GEM PATHS:
     - /Users/user/.rvm/gems/ruby-3.0.3
     - /Users/user/.rvm/rubies/ruby-3.0.3/lib/ruby/gems/3.0.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
     - :sources => ["s3://mygem/", "https://rubygems.org/"]
     - "s3_source" => {"mygem"=>{"id"=>"x", "secret"=>"x", "region"=>"eu-west-1"}}
  - REMOTE SOURCES:
     - s3://mygem/
     - https://rubygems.org/
  - SHELL PATH:
     - /Users/user/.rvm/gems/ruby-3.0.3/bin
     - /Users/user/.rvm/gems/ruby-3.0.3@global/bin
     - /Users/user/.rvm/rubies/ruby-3.0.3/bin
     - /Users/user/.rvm/bin
     - /Users/user/.pyenv/shims
     - /Users/user/.nvm/versions/node/v16.13.0/bin
     - /Users/user/Library/Android/sdk/platform-tools
     - /opt/homebrew/bin
     - /opt/homebrew/sbin
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /usr/local/go/bin
     - /Library/Apple/usr/bin
```



Discussion:
Can you send us a PR? :)
@deivid-rodriguez sure, please see #5795



Patch:
diff --git a/lib/rubygems/resolver/index_specification.rb b/lib/rubygems/resolver/index_specification.rb
index e8c3b35f7e63..0fc758dfd3ac 100644
--- a/lib/rubygems/resolver/index_specification.rb
+++ b/lib/rubygems/resolver/index_specification.rb
@@ -21,7 +21,8 @@ def initialize(set, name, version, source, platform)
     @name = name
     @version = version
     @source = source
-    @platform = platform.to_s
+    @platform = Gem::Platform.new(platform.to_s)
+    @original_platform = platform.to_s
 
     @spec = nil
   end
@@ -91,7 +92,7 @@ def pretty_print(q) # :nodoc:
   def spec # :nodoc:
     @spec ||=
       begin
-        tuple = Gem::NameTuple.new @name, @version, @platform
+        tuple = Gem::NameTuple.new @name, @version, @original_platform
 
         @source.fetch_spec tuple
       end


Test patch:
diff --git a/test/rubygems/test_gem_resolver_index_specification.rb b/test/rubygems/test_gem_resolver_index_specification.rb
index 339445cb4458..b479757bd557 100644
--- a/test/rubygems/test_gem_resolver_index_specification.rb
+++ b/test/rubygems/test_gem_resolver_index_specification.rb
@@ -26,7 +26,7 @@ def test_initialize_platform
     spec = Gem::Resolver::IndexSpecification.new(
       set, "rails", version, source, Gem::Platform.local)
 
-    assert_equal Gem::Platform.local.to_s, spec.platform
+    assert_equal Gem::Platform.local, spec.platform
   end
 
   def test_install
diff --git a/test/rubygems/test_gem_resolver_installer_set.rb b/test/rubygems/test_gem_resolver_installer_set.rb
index 8d84c28172a7..32e1faa28d43 100644
--- a/test/rubygems/test_gem_resolver_installer_set.rb
+++ b/test/rubygems/test_gem_resolver_installer_set.rb
@@ -51,6 +51,19 @@ def test_add_always_install_platform
     assert_equal %w[a-1], set.always_install.map {|s| s.full_name }
   end
 
+  def test_add_always_install_index_spec_platform
+    a_1_local, a_1_local_gem = util_gem "a", 1 do |s|
+      s.platform = Gem::Platform.local
+    end
+
+    FileUtils.mv a_1_local_gem, @tempdir
+
+    set = Gem::Resolver::InstallerSet.new :both
+    set.add_always_install dep("a")
+
+    assert_equal [Gem::Platform.local], set.always_install.map {|s| s.platform }
+  end
+
   def test_add_always_install_prerelease
     spec_fetcher do |fetcher|
       fetcher.gem "a", 1