@@ -221,9 +221,49 @@ def _build_gem_exts(executor, build, gem_home)
221221 ]
222222
223223 executor . system ( *args , env : env )
224+ patch_bundler_standalone_setup ( local_path )
224225 executor . cp_r ( local_path , gem_home )
225226 end
226227
228+ # Bundler standalone setup.rb assumes that once `Gem` is defined,
229+ # Gem.ruby_api_version and Gem.extension_api_version are also available.
230+ # In ruby-head packaging this assumption can fail (Gem exists, but these
231+ # API-version helpers are missing), so we inject a compatibility shim
232+ # before Bundler's load-path setup runs.
233+ def patch_bundler_standalone_setup ( local_path )
234+ setup_rb = File . join ( local_path , "bundler" , "setup.rb" )
235+ return unless File . file? ( setup_rb )
236+
237+ content = File . read ( setup_rb )
238+ marker = "# ruby.wasm compatibility shim for missing Gem API version methods"
239+ return if content . include? ( marker )
240+
241+ shim = <<~RUBY
242+ #{ marker }
243+ if defined?(Gem)
244+ module Gem
245+ def self.ruby_api_version
246+ RbConfig::CONFIG["ruby_version"]
247+ end unless respond_to?(:ruby_api_version)
248+
249+ def self.extension_api_version
250+ if "no" == RbConfig::CONFIG["ENABLE_SHARED"]
251+ "\# {ruby_api_version}-static"
252+ else
253+ ruby_api_version
254+ end
255+ end unless respond_to?(:extension_api_version)
256+ end
257+ end
258+ RUBY
259+
260+ patched = content . sub ( /^require 'rbconfig'\n / , "require 'rbconfig'\n #{ shim } \n " )
261+ return if patched == content
262+
263+ RubyWasm . logger . info ( "Patching #{ setup_rb } for RubyGems API compatibility" )
264+ File . write ( setup_rb , patched )
265+ end
266+
227267 def cache_key ( digest )
228268 derive_build . cache_key ( digest )
229269 end
0 commit comments