Skip to content

Commit 9eead86

Browse files
Handle non-gnu libc on linux platforms in RubyGems
Attempting to install a gem published as both *-linux and *-linux-musl results in the incorrect gem being picked up, causing build failures due to binary incompatibility. This is caused by the `nil` wildcard swallowing the libc information upon version comparison. Handle the linux case by performing only non-wildcard equality on the version and asserting 'gnu' and nil equivalence, while preserving the current behaviour for other OSes. Co-authored-by: Loic Nageleisen <loic.nageleisen@gmail.com>
1 parent 6da35ee commit 9eead86

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

lib/rubygems/platform.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,17 @@ def hash # :nodoc:
151151
##
152152
# Does +other+ match this platform? Two platforms match if they have the
153153
# same CPU, or either has a CPU of 'universal', they have the same OS, and
154-
# they have the same version, or either has no version.
154+
# they have the same version, or either one has no version
155155
#
156156
# Additionally, the platform will match if the local CPU is 'arm' and the
157157
# other CPU starts with "arm" (for generic ARM family support).
158+
#
159+
# Of note, this method is not commutative. Indeed the OS 'linux' has a
160+
# special case: the version is the libc name, yet while "no version" stands
161+
# as a wildcard for a binary gem platform (as for other OSes), for the
162+
# runtime platform "no version" stands for 'gnu'. To be able to disinguish
163+
# these, the method receiver is the gem platform, while the argument is
164+
# the runtime platform.
158165

159166
def ===(other)
160167
return nil unless Gem::Platform === other
@@ -171,7 +178,11 @@ def ===(other)
171178
@os == other.os &&
172179

173180
# version
174-
(@version.nil? || other.version.nil? || @version == other.version)
181+
(
182+
(@os != "linux" && (@version.nil? || other.version.nil?)) ||
183+
(@os == "linux" && (@version.nil? && !other.version.nil?)) ||
184+
@version == other.version
185+
)
175186
end
176187

177188
##

test/rubygems/test_gem_platform.rb

+30
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ def test_initialize
135135
"i386-solaris2.8" => ["x86", "solaris", "2.8"],
136136
"mswin32" => ["x86", "mswin32", nil],
137137
"x86_64-linux" => ["x86_64", "linux", nil],
138+
"x86_64-linux-gnu" => ["x86_64", "linux", nil],
138139
"x86_64-linux-musl" => ["x86_64", "linux", "musl"],
140+
"x86_64-linux-uclibc" => ["x86_64", "linux", "uclibc"],
139141
"x86_64-openbsd3.9" => ["x86_64", "openbsd", "3.9"],
140142
"x86_64-openbsd4.0" => ["x86_64", "openbsd", "4.0"],
141143
"x86_64-openbsd" => ["x86_64", "openbsd", nil],
@@ -263,6 +265,34 @@ def test_nil_cpu_arch_is_treated_as_universal
263265
assert((with_x86_arch === with_nil_arch), "x86 =~ nil")
264266
end
265267

268+
def test_nil_version_is_treated_as_any_version
269+
x86_darwin_8 = Gem::Platform.new "i686-darwin8.0"
270+
x86_darwin_nil = Gem::Platform.new "i686-darwin"
271+
272+
assert((x86_darwin_8 === x86_darwin_nil), "8.0 =~ nil")
273+
assert((x86_darwin_nil === x86_darwin_8), "nil =~ 8.0")
274+
end
275+
276+
def test_nil_version_is_stricter_for_linux_os
277+
x86_linux = Gem::Platform.new "i686-linux"
278+
x86_linux_gnu = Gem::Platform.new "i686-linux-gnu"
279+
x86_linux_musl = Gem::Platform.new "i686-linux-musl"
280+
x86_linux_uclibc = Gem::Platform.new "i686-linux-uclibc"
281+
282+
# a naked linux runtime is implicit gnu, as it represents the common glibc-linked runtime
283+
assert(x86_linux === x86_linux_gnu, "linux =~ linux-gnu")
284+
assert(x86_linux_gnu === x86_linux, "linux-gnu =~ linux")
285+
286+
# explicit libc differ
287+
refute(x86_linux_uclibc === x86_linux_musl, "linux-uclibc =~ linux-musl")
288+
refute(x86_linux_musl === x86_linux_uclibc, "linux-musl =~ linux-uclibc")
289+
290+
# musl host runtime accepts libc-generic or statically linked gems...
291+
assert(x86_linux === x86_linux_musl, "linux =~ linux-musl")
292+
# ...but implicit gnu runtime generally does not accept musl-specific gems
293+
refute(x86_linux_musl === x86_linux, "linux-musl =~ linux")
294+
end
295+
266296
def test_equals3_cpu_arm
267297
arm = Gem::Platform.new "arm-linux"
268298
armv5 = Gem::Platform.new "armv5-linux"

0 commit comments

Comments
 (0)