Skip to content

Commit

Permalink
Improve error message when operating_system.rb fails to load
Browse files Browse the repository at this point in the history
Show an absolute path instead of an unhelpful relative path.
  • Loading branch information
deivid-rodriguez committed Jun 26, 2022
1 parent 4fb1f11 commit f1eed36
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/rubygems.rb
Expand Up @@ -1323,8 +1323,9 @@ def default_gem_load_paths
rescue LoadError
# Ignored
rescue StandardError => e
path = e.backtrace_locations.reverse.find {|l| l.path.end_with?("rubygems/defaults/operating_system.rb") }.path
msg = "#{e.message}\n" \
"Loading the rubygems/defaults/operating_system.rb file caused an error. " \
"Loading the #{path} file caused an error. " \
"This file is owned by your OS, not by rubygems upstream. " \
"Please find out which OS package this file belongs to and follow the guidelines from your OS to report " \
"the problem and ask for help."
Expand Down
15 changes: 9 additions & 6 deletions test/rubygems/test_rubygems.rb
Expand Up @@ -16,7 +16,7 @@ def test_operating_system_other_exceptions
output = Gem::Util.popen(*ruby_with_rubygems_and_fake_operating_system_in_load_path(path), '-e', "'require \"rubygems\"'", { :err => [:child, :out] }).strip
assert !$?.success?
assert_includes output, "undefined local variable or method `intentionally_not_implemented_method'"
assert_includes output, "Loading the rubygems/defaults/operating_system.rb file caused an error. " \
assert_includes output, "Loading the #{operating_system_rb_at(path)} file caused an error. " \
"This file is owned by your OS, not by rubygems upstream. " \
"Please find out which OS package this file belongs to and follow the guidelines from your OS to report " \
"the problem and ask for help."
Expand Down Expand Up @@ -53,16 +53,19 @@ def self.default_dir

def util_install_operating_system_rb(content)
dir_lib = Dir.mktmpdir("test_operating_system_lib", @tempdir)
dir_lib_arg = File.join dir_lib
dir_lib_arg = File.join dir_lib, "lib"

dir_lib_rubygems_defaults_arg = File.join dir_lib_arg, "lib", "rubygems", "defaults"
FileUtils.mkdir_p dir_lib_rubygems_defaults_arg
operating_system_rb = operating_system_rb_at(dir_lib_arg)

operating_system_rb = File.join dir_lib_rubygems_defaults_arg, "operating_system.rb"
FileUtils.mkdir_p File.dirname(operating_system_rb)

File.open(operating_system_rb, 'w') {|f| f.write content }

File.join dir_lib_arg, "lib"
dir_lib_arg
end

def operating_system_rb_at(dir)
File.join dir, "rubygems", "defaults", "operating_system.rb"
end

def ruby_with_rubygems_and_fake_operating_system_in_load_path(operating_system_path)
Expand Down

0 comments on commit f1eed36

Please sign in to comment.