Skip to content

Commit 4701731

Browse files
committed
Preload RubyGems before Bundler standalone setup
1 parent 3f4d006 commit 4701731

File tree

3 files changed

+5
-41
lines changed

3 files changed

+5
-41
lines changed

lib/ruby_wasm/packager/core.rb

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -221,49 +221,9 @@ def _build_gem_exts(executor, build, gem_home)
221221
]
222222

223223
executor.system(*args, env: env)
224-
patch_bundler_standalone_setup(local_path)
225224
executor.cp_r(local_path, gem_home)
226225
end
227226

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-
267227
def cache_key(digest)
268228
derive_build.cache_key(digest)
269229
end

packages/npm-packages/ruby-wasm-wasi/src/vm.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ export class RubyVM {
335335
this.eval(`
336336
# Require Bundler standalone setup
337337
if File.exist?("/bundle/bundler/setup.rb")
338+
# Some ruby-head + ruby.wasm revisions return from rubyInit with a partial
339+
# Gem module: Gem is defined, but RubyGems is not loaded yet and its API
340+
# helpers are still missing. Bundler standalone treats defined?(Gem) as
341+
# sufficient, so load RubyGems explicitly before requiring its setup file.
342+
require "rubygems" if defined?(Gem) && !Gem.respond_to?(:ruby_api_version)
338343
require "/bundle/bundler/setup.rb"
339344
elsif File.exist?("/bundle/setup.rb")
340345
# For non-CM builds, which doesn't use Bundler's standalone mode

sig/ruby_wasm/packager.rbs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class RubyWasm::Packager
8181
def name: () -> string
8282

8383
private def _build_gem_exts: (RubyWasm::BuildExecutor, RubyWasm::Build, string gem_home) -> void
84-
private def patch_bundler_standalone_setup: (string local_path) -> void
8584
private def _link_gem_exts: (RubyWasm::BuildExecutor, RubyWasm::Build, string ruby_root, string gem_home, bytes module_bytes) -> bytes
8685
end
8786

0 commit comments

Comments
 (0)