Skip to content

Commit

Permalink
Use Gem.use_gemdeps only from binstubs
Browse files Browse the repository at this point in the history
The previous behavior was to automatically require `bundler/setup`
everytime `rubygems` was required, which I think was too much.
  • Loading branch information
deivid-rodriguez committed Aug 4, 2021
1 parent e72c273 commit b25379a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 17 deletions.
2 changes: 0 additions & 2 deletions lib/rubygems.rb
Expand Up @@ -1350,5 +1350,3 @@ def default_gem_load_paths
require 'rubygems/core_ext/kernel_gem'
require 'rubygems/core_ext/kernel_require'
require 'rubygems/core_ext/kernel_warn'

Gem.use_gemdeps
2 changes: 2 additions & 0 deletions lib/rubygems/installer.rb
Expand Up @@ -762,6 +762,8 @@ def app_script_text(bin_file_name)
require 'rubygems'
Gem.use_gemdeps
version = "#{Gem::Requirement.default_prerelease}"
str = ARGV.first
Expand Down
6 changes: 5 additions & 1 deletion test/rubygems/helper.rb
Expand Up @@ -1295,7 +1295,11 @@ def self.rubybin
end

def ruby_with_rubygems_in_load_path
[Gem.ruby, "-I", $LOAD_PATH.find{|p| p == File.dirname($LOADED_FEATURES.find{|f| f.end_with?("/rubygems.rb") }) }]
[Gem.ruby, "-I", rubygems_path]
end

def rubygems_path
$LOAD_PATH.find{|p| p == File.dirname($LOADED_FEATURES.find{|f| f.end_with?("/rubygems.rb") }) }
end

def with_clean_path_to_ruby
Expand Down
62 changes: 48 additions & 14 deletions test/rubygems/test_gem.rb
Expand Up @@ -1730,10 +1730,18 @@ def add_bundler_full_name(names)
names
end

def test_looks_for_gemdeps_files_automatically_on_start
def test_looks_for_gemdeps_files_automatically_from_binstubs
pend "Requiring bundler messes things up" if Gem.java_platform?

a = util_spec "a", "1", nil, "lib/a.rb"
a = util_spec "a", "1" do |s|
s.executables = %w[foo]
s.bindir = "exe"
end

write_file File.join(@tempdir, 'exe', 'foo') do |fp|
fp.puts "puts Gem.loaded_specs.values.map(&:full_name).sort"
end

b = util_spec "b", "1", nil, "lib/b.rb"
c = util_spec "c", "1", nil, "lib/c.rb"

Expand All @@ -1747,29 +1755,41 @@ def test_looks_for_gemdeps_files_automatically_on_start
ENV['GEM_PATH'] = path
ENV['RUBYGEMS_GEMDEPS'] = "-"

new_PATH = [File.join(path, "bin"), ENV["PATH"]].join(File::PATH_SEPARATOR)
new_RUBYOPT = "-I#{rubygems_path} -I#{BUNDLER_LIB_PATH}"

path = File.join @tempdir, "gem.deps.rb"
cmd = [*ruby_with_rubygems_in_load_path,
"-I#{BUNDLER_LIB_PATH}"]
cmd << "-eputs Gem.loaded_specs.values.map(&:full_name).sort"

File.open path, "w" do |f|
f.puts "gem 'a'"
end
out0 = IO.popen(cmd, &:read).split(/\n/)
out0 = with_path_and_rubyopt(new_PATH, new_RUBYOPT) do
IO.popen("foo", &:read).split(/\n/)
end

File.open path, "a" do |f|
f.puts "gem 'b'"
f.puts "gem 'c'"
end
out = IO.popen(cmd, &:read).split(/\n/)
out = with_path_and_rubyopt(new_PATH, new_RUBYOPT) do
IO.popen("foo", &:read).split(/\n/)
end

assert_equal ["b-1", "c-1"], out - out0
end

def test_looks_for_gemdeps_files_automatically_on_start_in_parent_dir
def test_looks_for_gemdeps_files_automatically_from_binstubs_in_parent_dir
pend "Requiring bundler messes things up" if Gem.java_platform?

a = util_spec "a", "1", nil, "lib/a.rb"
a = util_spec "a", "1" do |s|
s.executables = %w[foo]
s.bindir = "exe"
end

write_file File.join(@tempdir, 'exe', 'foo') do |fp|
fp.puts "puts Gem.loaded_specs.values.map(&:full_name).sort"
end

b = util_spec "b", "1", nil, "lib/b.rb"
c = util_spec "c", "1", nil, "lib/c.rb"

Expand All @@ -1785,21 +1805,25 @@ def test_looks_for_gemdeps_files_automatically_on_start_in_parent_dir

Dir.mkdir "sub1"

new_PATH = [File.join(path, "bin"), ENV["PATH"]].join(File::PATH_SEPARATOR)
new_RUBYOPT = "-I#{rubygems_path} -I#{BUNDLER_LIB_PATH}"

path = File.join @tempdir, "gem.deps.rb"
cmd = [*ruby_with_rubygems_in_load_path, "-Csub1",
"-I#{BUNDLER_LIB_PATH}"]
cmd << "-eputs Gem.loaded_specs.values.map(&:full_name).sort"

File.open path, "w" do |f|
f.puts "gem 'a'"
end
out0 = IO.popen(cmd, &:read).split(/\n/)
out0 = with_path_and_rubyopt(new_PATH, new_RUBYOPT) do
IO.popen("foo", :chdir => "sub1", &:read).split(/\n/)
end

File.open path, "a" do |f|
f.puts "gem 'b'"
f.puts "gem 'c'"
end
out = IO.popen(cmd, &:read).split(/\n/)
out = with_path_and_rubyopt(new_PATH, new_RUBYOPT) do
IO.popen("foo", :chdir => "sub1", &:read).split(/\n/)
end

Dir.rmdir "sub1"

Expand Down Expand Up @@ -2114,4 +2138,14 @@ def util_remove_interrupt_command
def util_cache_dir
File.join Gem.dir, "cache"
end

def with_path_and_rubyopt(path_value, rubyopt_value)
path, ENV['PATH'] = ENV['PATH'], path_value
rubyopt, ENV['RUBYOPT'] = ENV['RUBYOPT'], rubyopt_value

yield
ensure
ENV['PATH'] = path
ENV['RUBYOPT'] = rubyopt
end
end
2 changes: 2 additions & 0 deletions test/rubygems/test_gem_installer.rb
Expand Up @@ -33,6 +33,8 @@ def test_app_script_text
require 'rubygems'
Gem.use_gemdeps
version = \">= 0.a\"
str = ARGV.first
Expand Down

0 comments on commit b25379a

Please sign in to comment.