Skip to content

Commit 011f526

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

3 files changed

Lines changed: 5 additions & 43 deletions

File tree

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,11 @@ export class RubyVM {
331331
initialize(args: string[] = ["ruby.wasm", "-EUTF-8", "-e_=0"]) {
332332
const c_args = args.map((arg) => arg + "\0");
333333
this.guest.rubyInit(c_args);
334-
try {
335-
this.eval(`
334+
try {
335+
this.eval(`
336+
# Ruby can expose Gem before rubygems.rb finishes setting up its API.
337+
# Load RubyGems explicitly so Bundler standalone does not see a partial Gem module.
338+
require "rubygems" if defined?(Gem) && !Gem.respond_to?(:ruby_api_version)
336339
# Require Bundler standalone setup
337340
if File.exist?("/bundle/bundler/setup.rb")
338341
require "/bundle/bundler/setup.rb"

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)