Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Use realpath in clean_load_path #6502

Merged
merged 8 commits into from Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -26,7 +26,7 @@ addons:
secure: "TrzIv116JLGUxm6PAUskCYrv8KTDguncKROVwbnjVPKTGDAgoDderd8JUdDEXrKoZ9qGLD2TPYKExt9/QDl71E+qHdWnVqWv4HKCUk2P9z/VLKzHuggOUBkCXiJUhjywUieCJhI3N92bfq2EjSBbu2/OFHqWOjLQ+QCooTEBjv8="

rvm:
- 2.5.0
- 2.5.1
- 2.4.3
- 2.3.6

Expand Down
15 changes: 13 additions & 2 deletions lib/bundler/shared_helpers.rb
Expand Up @@ -324,7 +324,7 @@ def set_rubylib
end

def bundler_ruby_lib
File.expand_path("../..", __FILE__)
resolve_path File.expand_path("../..", __FILE__)
end

def clean_load_path
Expand All @@ -336,12 +336,23 @@ def clean_load_path
loaded_gem_paths = Bundler.rubygems.loaded_gem_paths

$LOAD_PATH.reject! do |p|
next if File.expand_path(p).start_with?(bundler_lib)
next if resolve_path(p).start_with?(bundler_lib)
loaded_gem_paths.delete(p)
end
$LOAD_PATH.uniq!
end

def resolve_path(path)
expanded = File.expand_path(path)
return expanded unless File.respond_to?(:realpath)

while File.exist?(expanded) && File.realpath(expanded) != expanded
expanded = File.realpath(expanded)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK File.realpath() resolves all symlinks it can, so a single call should be enough, unlike readlink(1).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ojab for making a PR to fix this :)

end

expanded
end

def prints_major_deprecations?
require "bundler"
deprecation_release = Bundler::VERSION.split(".").drop(1).include?("99")
Expand Down
44 changes: 44 additions & 0 deletions spec/runtime/setup_spec.rb
Expand Up @@ -857,6 +857,50 @@ def clean_load_path(lp)
expect(out).to eq("true\ntrue")
end

context "with bundler is located in symlinked GEM_HOME" do
let(:gem_home) { Dir.mktmpdir }
let(:symlinked_gem_home) { Tempfile.new("gem_home") }
let(:bundler_dir) { File.expand_path("../../..", __FILE__) }
let(:bundler_lib) { File.join(bundler_dir, "lib") }

before do
FileUtils.ln_sf(gem_home, symlinked_gem_home.path)
gems_dir = File.join(gem_home, "gems")
specifications_dir = File.join(gem_home, "specifications")
Dir.mkdir(gems_dir)
Dir.mkdir(specifications_dir)

FileUtils.ln_s(bundler_dir, File.join(gems_dir, "bundler-#{Bundler::VERSION}"))

gemspec = File.read("#{bundler_dir}/bundler.gemspec").
sub("Bundler::VERSION", %("#{Bundler::VERSION}"))
gemspec = gemspec.lines.reject {|line| line =~ %r{lib/bundler/version} }.join

File.open(File.join(specifications_dir, "bundler.gemspec"), "wb") do |f|
f.write(gemspec)
end
end

it "should succesfully require 'bundler/setup'" do
install_gemfile ""

ENV["GEM_PATH"] = symlinked_gem_home.path

ruby <<-R
if $LOAD_PATH.include?("#{bundler_lib}")
# We should use bundler from GEM_PATH for this test, so we should
# remove path to the bundler source tree
$LOAD_PATH.delete("#{bundler_lib}")
else
raise "We don't have #{bundler_lib} in $LOAD_PATH"
end
puts (require 'bundler/setup')
R

expect(out).to eql("true")
end
end

it "stubs out Gem.refresh so it does not reveal system gems" do
system_gems "rack-1.0.0"

Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -28,6 +28,14 @@ module Gem
require "uri"
require "digest"

# Delete any copies of Bundler that have been dumped into site_ruby without
# a gemspec. RubyGems cannot manage that Bundler, and so our tricks to make
# sure that the correct version of Bundler loads will stop working.
require "fileutils"
Dir.glob(File.join(RbConfig::CONFIG["sitelibdir"], "bundler*")).each do |file|
FileUtils.rm_rf(file)
end

if File.expand_path(__FILE__) =~ %r{([^\w/\.-])}
abort "The bundler specs cannot be run from a path that contains special characters (particularly #{$1.inspect})"
end
Expand Down