Skip to content

Commit eb48418

Browse files
segiddinshsbt
authored andcommitted
[rubygems/rubygems] Ensure that Gem::Platform parses strings to a fix point
The issue was that the property that ```ruby platform = Gem::Platform.new $string platform == Gem::Platform.new(platform.to_s) ``` was not always true. This property (of acchieving a fix point) is important, since `Gem::Platform` gets serialized to a string and then deserialized back to a `Gem::Platform` object. If it doesn't deserialize to the same object, then different platforms are used for the initial serialization than subsequent runs. I used https://github.com/segiddins/Scratch/blob/main/2025/03/rubygems-platform.rb to find the failing cases and then fixed them. With this patch, the prop check test now passes. ruby/rubygems@313fb4bcec
1 parent 72387eb commit eb48418

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

lib/rubygems/platform.rb

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,32 @@ def initialize(arch)
8888
when Array then
8989
@cpu, @os, @version = arch
9090
when String then
91+
arch_str = arch
9192
arch = arch.split "-"
9293

93-
if arch.length > 2 && !arch.last.match?(/\d+(\.\d+)?$/) # reassemble x86-linux-{libc}
94+
if arch.length > 2 && !arch.last.match?(/^\d+(\.\d+)?$/) # reassemble x86-linux-{libc}
9495
extra = arch.pop
9596
arch.last << "-#{extra}"
9697
end
9798

9899
cpu = arch.shift
99-
100-
@cpu = case cpu
101-
when /i\d86/ then "x86"
102-
else cpu
100+
if cpu.nil? || "" == cpu
101+
raise ArgumentError, "empty cpu in platform #{arch_str.inspect}"
103102
end
104103

104+
105+
@cpu = if cpu.match?(/i\d86/)
106+
"x86"
107+
else
108+
cpu
109+
end
110+
105111
if arch.length == 2 && arch.last.match?(/^\d+(\.\d+)?$/) # for command-line
106112
@os, @version = arch
107113
return
108114
end
109115

116+
# discard the version element, it didn't match the version pattern (\d+(\.\d+)?)
110117
os, = arch
111118
if os.nil?
112119
@cpu = nil
@@ -120,17 +127,17 @@ def initialize(arch)
120127
when /^macruby$/ then ["macruby", nil]
121128
when /freebsd(\d+)?/ then ["freebsd", $1]
122129
when /^java$/, /^jruby$/ then ["java", nil]
123-
when /^java([\d.]*)/ then ["java", $1]
130+
when /^java(\d+(?:\.\d+)*)?/ then ["java", $1]
124131
when /^dalvik(\d+)?$/ then ["dalvik", $1]
125132
when /^dotnet$/ then ["dotnet", nil]
126-
when /^dotnet([\d.]*)/ then ["dotnet", $1]
133+
when /^dotnet(\d+(?:\.\d+)*)?/ then ["dotnet", $1]
127134
when /linux-?(\w+)?/ then ["linux", $1]
128135
when /mingw32/ then ["mingw32", nil]
129136
when /mingw-?(\w+)?/ then ["mingw", $1]
130-
when /(mswin\d+)(\_(\d+))?/ then
137+
when /(mswin\d+)(?:\_(\d+))?/ then
131138
os = $1
132-
version = $3
133-
@cpu = "x86" if @cpu.nil? && os =~ /32$/
139+
version = $2
140+
@cpu = "x86" if @cpu.nil? && os.end_with?("32")
134141
[os, version]
135142
when /netbsdelf/ then ["netbsdelf", nil]
136143
when /openbsd(\d+\.\d+)?/ then ["openbsd", $1]
@@ -154,6 +161,9 @@ def to_a
154161
end
155162

156163
def to_s
164+
if @cpu.nil? && @os && @version
165+
return "#{@os}#{@version}"
166+
end
157167
to_a.compact.join "-"
158168
end
159169

test/rubygems/test_gem_platform.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,23 @@ def test_initialize
148148
"wasm32-wasi" => ["wasm32", "wasi", nil],
149149
"wasm32-wasip1" => ["wasm32", "wasi", nil],
150150
"wasm32-wasip2" => ["wasm32", "wasi", nil],
151+
152+
"darwin-java-java" => ["darwin", "java", nil],
153+
"linux-linux-linux" => ["linux", "linux", "linux"],
154+
"linux-linux-linux1.0" => ["linux", "linux", "linux1"],
155+
"x86x86-1x86x86x86x861linuxx86x86" => ["x86x86", "linux", "x86x86"],
156+
"freebsd0" => [nil, "freebsd", "0"],
157+
"darwin0" => [nil, "darwin", "0"],
158+
"darwin0---" => [nil, "darwin", "0"],
159+
"x86-linux-x8611.0l" => ["x86", "linux", "x8611"],
160+
"0-x86linuxx86---" => ["0", "linux", "x86"],
151161
}
152162

153163
test_cases.each do |arch, expected|
154164
platform = Gem::Platform.new arch
155165
assert_equal expected, platform.to_a, arch.inspect
156-
assert_equal expected, Gem::Platform.new(platform.to_s).to_a, arch.inspect
166+
platform2 = Gem::Platform.new platform.to_s
167+
assert_equal expected, platform2.to_a, "#{arch.inspect} => #{platform2.inspect}"
157168
end
158169
end
159170

0 commit comments

Comments
 (0)