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

Commit

Permalink
Fallback to a temp dir when the home directory is not usable
Browse files Browse the repository at this point in the history
  • Loading branch information
segiddins committed Sep 7, 2016
1 parent 002939b commit 02e7f67
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 5 deletions.
36 changes: 35 additions & 1 deletion lib/bundler.rb
Expand Up @@ -143,8 +143,41 @@ def ruby_scope
"#{Bundler.rubygems.ruby_engine}/#{Bundler.rubygems.config_map[:ruby_version]}"
end

def user_home
@user_home ||= begin
home = Bundler.rubygems.user_home
warning = "Your home directory is not set properly:"
if home.nil?
warning += "\n * It is not set at all"
elsif !File.directory?(home)
warning += "\n * `#{home}` is not a directory"
elsif !File.writable?(home)
warning += "\n * `#{home}` is not writable"
else
return Pathname.new(home)
end

login = Etc.getlogin || "unknown"

tmp_home = Pathname.new(Dir.tmpdir).join("bundler", "home", login)
begin
SharedHelpers.filesystem_access(tmp_home, :write) do |p|
FileUtils.mkdir_p(p)
end
rescue => e
warning += "\n\nBundler also failed to create a temporary home directory at `#{tmp_home}`:\n#{e}"
raise warning
end

warning += "\n\nBundler will use `#{tmp_home}` as your home directory temporarily"

Bundler.ui.warn(warning)
tmp_home
end
end

def user_bundle_path
Pathname.new(Bundler.rubygems.user_home).join(".bundle")
Pathname.new(user_home).join(".bundle")
end

def home
Expand Down Expand Up @@ -403,6 +436,7 @@ def reset!
@locked_gems = nil
@bundle_path = nil
@bin_path = nil
@user_home = nil

Plugin.reset!

Expand Down
2 changes: 2 additions & 0 deletions lib/bundler/cli.rb
Expand Up @@ -36,8 +36,10 @@ def initialize(*args)
ensure
self.options ||= {}
Bundler.settings.cli_flags_given = !options.empty?
unprinted_warnings = Bundler.ui.unprinted_warnings
Bundler.ui = UI::Shell.new(options)
Bundler.ui.level = "debug" if options["verbose"]
unprinted_warnings.each {|w| Bundler.ui.warn(w) }

if ENV["RUBYGEMS_GEMDEPS"] && !ENV["RUBYGEMS_GEMDEPS"].empty?
Bundler.ui.warn(
Expand Down
2 changes: 1 addition & 1 deletion lib/bundler/fetcher/compact_index.rb
Expand Up @@ -61,7 +61,7 @@ def fetch_spec(spec)
compact_index_request :fetch_spec

def available?
user_home = Pathname.new(Bundler.rubygems.user_home)
user_home = Bundler.user_home
return nil unless user_home.directory? && user_home.writable?
# Read info file checksums out of /versions, so we can know if gems are up to date
fetch_uri.scheme != "file" && compact_index_client.update_and_parse_checksums!
Expand Down
2 changes: 1 addition & 1 deletion lib/bundler/gem_helper.rb
Expand Up @@ -98,7 +98,7 @@ def rubygem_push(path)
allowed_push_host = @gemspec.metadata["allowed_push_host"]
gem_command += " --host #{allowed_push_host}" if allowed_push_host
end
unless allowed_push_host || Pathname.new("~/.gem/credentials").expand_path.file?
unless allowed_push_host || Bundler.user_home.join(".gem/credentials").file?
raise "Your rubygems.org credentials aren't set. Run `gem push` to set them."
end
sh(gem_command)
Expand Down
6 changes: 4 additions & 2 deletions lib/bundler/shared_helpers.rb
Expand Up @@ -39,10 +39,12 @@ def default_bundle_dir
bundle_dir = find_directory(".bundle")
return nil unless bundle_dir

global_bundle_dir = File.join(Bundler.rubygems.user_home, ".bundle")
bundle_dir = Pathname.new(bundle_dir)

global_bundle_dir = Bundler.user_home.join(".bundle")
return nil if bundle_dir == global_bundle_dir

Pathname.new(bundle_dir)
bundle_dir
end

def in_bundle?
Expand Down
4 changes: 4 additions & 0 deletions lib/bundler/ui/shell.rb
Expand Up @@ -83,6 +83,10 @@ def silence(&blk)
with_level("silent", &blk)
end

def unprinted_warnings
[]
end

private

# valimism
Expand Down
9 changes: 9 additions & 0 deletions lib/bundler/ui/silent.rb
Expand Up @@ -2,6 +2,10 @@
module Bundler
module UI
class Silent
def initialize
@warnings = []
end

def add_color(string, color)
string
end
Expand All @@ -13,6 +17,7 @@ def confirm(message, newline = nil)
end

def warn(message, newline = nil)
@warnings |= [message]
end

def error(message, newline = nil)
Expand Down Expand Up @@ -44,6 +49,10 @@ def trace(message, newline = nil)
def silence
yield
end

def unprinted_warnings
@warnings
end
end
end
end

0 comments on commit 02e7f67

Please sign in to comment.