[rubygems/rubygems] Fix require issue with file extension priority
If `require "a"` is run when two folders have been specified in the -I
option including a "a.rb" file and a "a.so" file respectively, the ruby
spec says that the ".rb" file should always be preferred. However, the
logic we added in https://github.com/rubygems/rubygems/commit/6b81076d9
to make the -I option always beat default gems does not respect this
spec, creating a difference from the original ruby-core's require.
[the ruby spec says]: d80a6e2b22/core/kernel/shared/require.rb (L234-L246)
https://github.com/rubygems/rubygems/commit/b3944384f4
This commit is contained in:
parent
c6b5881eae
commit
d767da428c
@ -43,18 +43,18 @@ module Kernel
|
|||||||
# https://github.com/rubygems/rubygems/pull/1868
|
# https://github.com/rubygems/rubygems/pull/1868
|
||||||
resolved_path = begin
|
resolved_path = begin
|
||||||
rp = nil
|
rp = nil
|
||||||
$LOAD_PATH[0...Gem.load_path_insert_index || -1].each do |lp|
|
Gem.suffixes.each do |s|
|
||||||
safe_lp = lp.dup.tap(&Gem::UNTAINT)
|
$LOAD_PATH[0...Gem.load_path_insert_index || -1].each do |lp|
|
||||||
begin
|
safe_lp = lp.dup.tap(&Gem::UNTAINT)
|
||||||
if File.symlink? safe_lp # for backward compatibility
|
begin
|
||||||
next
|
if File.symlink? safe_lp # for backward compatibility
|
||||||
|
next
|
||||||
|
end
|
||||||
|
rescue SecurityError
|
||||||
|
RUBYGEMS_ACTIVATION_MONITOR.exit
|
||||||
|
raise
|
||||||
end
|
end
|
||||||
rescue SecurityError
|
|
||||||
RUBYGEMS_ACTIVATION_MONITOR.exit
|
|
||||||
raise
|
|
||||||
end
|
|
||||||
|
|
||||||
Gem.suffixes.each do |s|
|
|
||||||
full_path = File.expand_path(File.join(safe_lp, "#{path}#{s}"))
|
full_path = File.expand_path(File.join(safe_lp, "#{path}#{s}"))
|
||||||
if File.file?(full_path)
|
if File.file?(full_path)
|
||||||
rp = full_path
|
rp = full_path
|
||||||
@ -67,12 +67,8 @@ module Kernel
|
|||||||
end
|
end
|
||||||
|
|
||||||
if resolved_path
|
if resolved_path
|
||||||
begin
|
RUBYGEMS_ACTIVATION_MONITOR.exit
|
||||||
RUBYGEMS_ACTIVATION_MONITOR.exit
|
return gem_original_require(resolved_path)
|
||||||
return gem_original_require(resolved_path)
|
|
||||||
rescue LoadError
|
|
||||||
RUBYGEMS_ACTIVATION_MONITOR.enter
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if spec = Gem.find_unresolved_default_spec(path)
|
if spec = Gem.find_unresolved_default_spec(path)
|
||||||
|
@ -120,6 +120,24 @@ class TestGemRequire < Gem::TestCase
|
|||||||
Object.send :remove_const, :HELLO if Object.const_defined? :HELLO
|
Object.send :remove_const, :HELLO if Object.const_defined? :HELLO
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_dash_i_respects_default_library_extension_priority
|
||||||
|
skip "extensions don't quite work on jruby" if Gem.java_platform?
|
||||||
|
|
||||||
|
dash_i_ext_arg = util_install_extension_file('a')
|
||||||
|
dash_i_lib_arg = util_install_ruby_file('a')
|
||||||
|
|
||||||
|
lp = $LOAD_PATH.dup
|
||||||
|
|
||||||
|
begin
|
||||||
|
$LOAD_PATH.unshift dash_i_lib_arg
|
||||||
|
$LOAD_PATH.unshift dash_i_ext_arg
|
||||||
|
assert_require 'a'
|
||||||
|
assert_match(/a\.rb$/, $LOADED_FEATURES.last)
|
||||||
|
ensure
|
||||||
|
$LOAD_PATH.replace lp
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_concurrent_require
|
def test_concurrent_require
|
||||||
Object.const_set :FILE_ENTERED_LATCH, Latch.new(2)
|
Object.const_set :FILE_ENTERED_LATCH, Latch.new(2)
|
||||||
Object.const_set :FILE_EXIT_LATCH, Latch.new(1)
|
Object.const_set :FILE_EXIT_LATCH, Latch.new(1)
|
||||||
@ -541,4 +559,48 @@ class TestGemRequire < Gem::TestCase
|
|||||||
$VERBOSE = old_verbose
|
$VERBOSE = old_verbose
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def util_install_extension_file(name)
|
||||||
|
spec = quick_gem name
|
||||||
|
util_build_gem spec
|
||||||
|
|
||||||
|
spec.extensions << "extconf.rb"
|
||||||
|
write_file File.join(@tempdir, "extconf.rb") do |io|
|
||||||
|
io.write <<-RUBY
|
||||||
|
require "mkmf"
|
||||||
|
create_makefile("#{name}")
|
||||||
|
RUBY
|
||||||
|
end
|
||||||
|
|
||||||
|
write_file File.join(@tempdir, "#{name}.c") do |io|
|
||||||
|
io.write <<-C
|
||||||
|
#include <ruby.h>
|
||||||
|
void Init_#{name}() { }
|
||||||
|
C
|
||||||
|
end
|
||||||
|
|
||||||
|
spec.files += ["extconf.rb", "#{name}.c"]
|
||||||
|
|
||||||
|
so = File.join(spec.gem_dir, "#{name}.#{RbConfig::CONFIG["DLEXT"]}")
|
||||||
|
refute_path_exists so
|
||||||
|
|
||||||
|
path = Gem::Package.build spec
|
||||||
|
installer = Gem::Installer.at path
|
||||||
|
installer.install
|
||||||
|
assert_path_exists so
|
||||||
|
|
||||||
|
spec.gem_dir
|
||||||
|
end
|
||||||
|
|
||||||
|
def util_install_ruby_file(name)
|
||||||
|
dir_lib = Dir.mktmpdir("test_require_lib", @tempdir)
|
||||||
|
dash_i_lib_arg = File.join dir_lib
|
||||||
|
|
||||||
|
a_rb = File.join dash_i_lib_arg, "#{name}.rb"
|
||||||
|
|
||||||
|
FileUtils.mkdir_p File.dirname a_rb
|
||||||
|
File.open(a_rb, 'w') { |f| f.write "# #{name}.rb" }
|
||||||
|
|
||||||
|
dash_i_lib_arg
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user