Skip to content

Commit

Permalink
Load operating_system.rb customizations before setting up default gems
Browse files Browse the repository at this point in the history
It's very common for packagers to configure gem paths in this file, for
example, `Gem.default_dir`. Also, setting up default gems requires these
paths to be set, so that we know which default gems need to be setup.

If we setup default gems before loading `operatin_system.rb`
customizations, the wrong default gems will be setup.

Unfortunately, default gems loaded by `operating_system.rb` can't be
upgraded if we do this, but it seems much of a smaller issue. I wasn't
even fully sure it was the right thing to do when I added that, and it
was not the culprit of the end user issue that led to making that
change.
  • Loading branch information
deivid-rodriguez committed Dec 8, 2021
1 parent 59e9466 commit 3d9f18a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/install-rubygems.yml
Expand Up @@ -105,10 +105,5 @@ jobs:
- name: Check we can install a Gemfile with git sources
run: bundle init && bundle add fileutils --git https://github.com/ruby/fileutils
shell: bash
- name: Check the latest available fiddle version gets activated
run: |
gem install fiddle:1.1.0
ruby -rfiddle -e "exit Fiddle::VERSION == '1.1.0'"
if: matrix.ruby.name != 'jruby-9.3'

timeout-minutes: 10
32 changes: 16 additions & 16 deletions lib/rubygems.rb
Expand Up @@ -1323,22 +1323,6 @@ def default_gem_load_paths
require_relative 'rubygems/specification'

# REFACTOR: This should be pulled out into some kind of hacks file.
begin
##
# Defaults the Ruby implementation wants to provide for RubyGems

require "rubygems/defaults/#{RUBY_ENGINE}"
rescue LoadError
end

##
# Loads the default specs.
Gem::Specification.load_defaults

require_relative 'rubygems/core_ext/kernel_gem'
require_relative 'rubygems/core_ext/kernel_require'
require_relative 'rubygems/core_ext/kernel_warn'

begin
##
# Defaults the operating system (or packager) wants to provide for RubyGems.
Expand All @@ -1354,3 +1338,19 @@ def default_gem_load_paths
"the problem and ask for help."
raise e.class, msg
end

begin
##
# Defaults the Ruby implementation wants to provide for RubyGems

require "rubygems/defaults/#{RUBY_ENGINE}"
rescue LoadError
end

##
# Loads the default specs.
Gem::Specification.load_defaults

require_relative 'rubygems/core_ext/kernel_gem'
require_relative 'rubygems/core_ext/kernel_require'
require_relative 'rubygems/core_ext/kernel_warn'
17 changes: 17 additions & 0 deletions test/rubygems/test_rubygems.rb
Expand Up @@ -22,6 +22,23 @@ def test_operating_system_other_exceptions
"the problem and ask for help."
end

def test_operating_system_customizing_default_dir
pend "does not apply to truffleruby" if RUBY_ENGINE == 'truffleruby'

# On a non existing default dir, there should be no gems

path = util_install_operating_system_rb <<-RUBY
module Gem
def self.default_dir
@homebrew_path ||= File.expand_path("foo")
end
end
RUBY

output = Gem::Util.popen(*ruby_with_rubygems_and_fake_operating_system_in_load_path(path), '-S', "gem", "list", {:err => [:child, :out]}).strip
assert_empty output
end

private

def util_install_operating_system_rb(content)
Expand Down

0 comments on commit 3d9f18a

Please sign in to comment.