From 467d90a6dc14c802c37081643c70cc161b05e81f Mon Sep 17 00:00:00 2001 From: Gabriel Mazetto Date: Wed, 23 May 2018 05:51:53 +0200 Subject: [PATCH] Don't fallback to tempdir when required directories exist. When home directory is not writable, but the required .bundle is, we should use it instead of falling back to use tempdirs. This creates a workaround for more restrictive setups using Omnibus Docker or any hardened setup, to overcome the annoyances introduced by #4951. --- lib/bundler.rb | 3 ++- spec/bundler/bundler_spec.rb | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/bundler.rb b/lib/bundler.rb index 9944ebd0513..e4f5bce75d0 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -159,12 +159,13 @@ def ruby_scope def user_home @user_home ||= begin home = Bundler.rubygems.user_home + dotbundle = home ? File.join(home, '.bundle') : nil warning = if home.nil? "Your home directory is not set." elsif !File.directory?(home) "`#{home}` is not a directory." - elsif !File.writable?(home) + elsif !File.writable?(home) && (!File.directory?(dotbundle) || !File.writable?(dotbundle)) "`#{home}` is not writable." end diff --git a/spec/bundler/bundler_spec.rb b/spec/bundler/bundler_spec.rb index 131146119e1..c483701d6e8 100644 --- a/spec/bundler/bundler_spec.rb +++ b/spec/bundler/bundler_spec.rb @@ -199,6 +199,57 @@ allow(File).to receive(:writable?).with(path).and_return true expect(Bundler.user_home).to eq(Pathname(path)) end + + context "is not a directory" do + it "should issue a warning and return a temporary user home" do + path = "/home/oggy" + allow(Bundler.rubygems).to receive(:user_home).and_return(path) + allow(File).to receive(:directory?).with(path).and_return false + allow(Etc).to receive(:getlogin).and_return("USER") + allow(Dir).to receive(:tmpdir).and_return("/TMP") + allow(FileTest).to receive(:exist?).with("/TMP/bundler/home").and_return(true) + expect(FileUtils).to receive(:mkpath).with("/TMP/bundler/home/USER") + message = <