Skip to content

Commit

Permalink
Fix crash when installing gems with symlinks
Browse files Browse the repository at this point in the history
If BUNDLE_PATH is configured to a symlinked path, installing gems with
symlinks would crash with an error like this:

```
Gem::Package::SymlinkError: installing symlink 'man/man0/README.markdown' pointing to parent path /usr/home/stevewi/srv/mail/lib/tools/.vendor/ruby/3.1.0/gems/binman-5.1.0/README.markdown of /srv/mail/lib/tools/.vendor/ruby/3.1.0/gems/binman-5.1.0 is not allowed
```

This commit fixes the problem by changing the bundle path to be the
realpath of the configured value, right after we're sure the path has
been created.
  • Loading branch information
deivid-rodriguez committed May 27, 2022
1 parent 70ff7ce commit 3cd3dd1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
11 changes: 11 additions & 0 deletions bundler/lib/bundler.rb
Expand Up @@ -97,6 +97,17 @@ def bundle_path
@bundle_path ||= Pathname.new(configured_bundle_path.path).expand_path(root)
end

def create_bundle_path
SharedHelpers.filesystem_access(bundle_path.to_s) do |p|
mkdir_p(p)
end unless bundle_path.exist?

@bundle_path = bundle_path.realpath
rescue Errno::EEXIST
raise PathError, "Could not install to path `#{bundle_path}` " \
"because a file already exists at that path. Either remove or rename the file so the directory can be created."
end

def configured_bundle_path
@configured_bundle_path ||= settings.path.tap(&:validate!)
end
Expand Down
1 change: 1 addition & 0 deletions bundler/lib/bundler/inline.rb
Expand Up @@ -38,6 +38,7 @@ def gemfile(install = false, options = {}, &gemfile)
raise ArgumentError, "Unknown options: #{opts.keys.join(", ")}" unless opts.empty?

begin
Bundler.instance_variable_set(:@bundle_path, Pathname.new(Gem.dir))
old_gemfile = ENV["BUNDLE_GEMFILE"]
Bundler::SharedHelpers.set_env "BUNDLE_GEMFILE", "Gemfile"

Expand Down
11 changes: 1 addition & 10 deletions bundler/lib/bundler/installer.rb
Expand Up @@ -66,7 +66,7 @@ def initialize(root, definition)
# require paths and save them in a `setup.rb` file. See `bundle standalone --help` for more
# information.
def run(options)
create_bundle_path
Bundler.create_bundle_path

ProcessLock.lock do
if Bundler.frozen_bundle?
Expand Down Expand Up @@ -262,15 +262,6 @@ def install_in_parallel(size, standalone, force = false)
end
end

def create_bundle_path
SharedHelpers.filesystem_access(Bundler.bundle_path.to_s) do |p|
Bundler.mkdir_p(p)
end unless Bundler.bundle_path.exist?
rescue Errno::EEXIST
raise PathError, "Could not install to path `#{Bundler.bundle_path}` " \
"because a file already exists at that path. Either remove or rename the file so the directory can be created."
end

# returns whether or not a re-resolve was needed
def resolve_if_needed(options)
if !@definition.unlocking? && !options["force"] && !Bundler.settings[:inline] && Bundler.default_lockfile.file?
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/source/rubygems.rb
Expand Up @@ -499,7 +499,7 @@ def requires_sudo?
end

def rubygems_dir
Bundler.rubygems.gem_dir
Bundler.bundle_path
end

def default_cache_path_for(dir)
Expand Down
33 changes: 33 additions & 0 deletions bundler/spec/commands/install_spec.rb
Expand Up @@ -969,4 +969,37 @@ def run
expect(last_command).to be_success
end
end

context "with a symlinked configured as bundle path and a gem with symlinks" do
before do
symlinked_bundled_app = tmp("bundled_app-symlink")
File.symlink(bundled_app, symlinked_bundled_app)
bundle "config path #{File.join(symlinked_bundled_app, ".vendor")}"

binman_path = tmp("binman")
FileUtils.mkdir_p binman_path

readme_path = File.join(binman_path, "README.markdown")
FileUtils.touch(readme_path)

man_path = File.join(binman_path, "man", "man0")
FileUtils.mkdir_p man_path

File.symlink("../../README.markdown", File.join(man_path, "README.markdown"))

build_repo4 do
build_gem "binman", :path => gem_repo4("gems"), :lib_path => binman_path, :no_default => true do |s|
s.files = ["README.markdown", "man/man0/README.markdown"]
end
end
end

it "installs fine" do
install_gemfile <<~G
source "#{file_uri_for(gem_repo4)}"
gem "binman"
G
end
end
end
5 changes: 3 additions & 2 deletions bundler/spec/support/builders.rb
Expand Up @@ -484,7 +484,7 @@ def @spec.validate(*); end
end

@spec.authors = ["no one"]
@spec.files = @files.keys
@spec.files += @files.keys

case options[:gemspec]
when false
Expand Down Expand Up @@ -589,7 +589,8 @@ def ref_for(ref, len = nil)

class GemBuilder < LibBuilder
def _build(opts)
lib_path = super(opts.merge(:path => @context.tmp(".tmp/#{@spec.full_name}"), :no_default => opts[:no_default]))
lib_path = opts[:lib_path] || @context.tmp(".tmp/#{@spec.full_name}")
lib_path = super(opts.merge(:path => lib_path, :no_default => opts[:no_default]))
destination = opts[:path] || _default_path
FileUtils.mkdir_p(lib_path.join(destination))

Expand Down

0 comments on commit 3cd3dd1

Please sign in to comment.