aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorEllen Marie Dash <me@duckie.co>2023-09-29 23:50:30 -0400
committergit <svn-admin@ruby-lang.org>2023-10-11 19:07:28 +0000
commitc83f8ad8671afbd8e9ae70c2c4e69a80c6a96d67 (patch)
tree003505d5ad2dd0ca5728b53654f90f7a8414a5d7 /lib
parente84b73398b96ea7abbeb3a17caae71b365f5016d (diff)
downloadruby-c83f8ad8671afbd8e9ae70c2c4e69a80c6a96d67.tar.gz
[rubygems/rubygems] Simplify logic for Gem::PathSupport#home, and make GEM_HOME always overide it.
https://github.com/rubygems/rubygems/commit/64273fd7e3
Diffstat (limited to 'lib')
-rw-r--r--lib/rubygems/path_support.rb35
1 files changed, 19 insertions, 16 deletions
diff --git a/lib/rubygems/path_support.rb b/lib/rubygems/path_support.rb
index b7900f67eb..6ce66494ca 100644
--- a/lib/rubygems/path_support.rb
+++ b/lib/rubygems/path_support.rb
@@ -24,19 +24,24 @@ class Gem::PathSupport
# hashtable, or defaults to ENV, the system environment.
#
def initialize(env)
- @home = default_home_dir(env)
-
- # If @home (aka Gem.paths.home) exists, but we can't write to it,
- # fall back to Gem.user_dir (the directory used for user installs).
- if File.exist?(@home) && !File.writable?(@home)
- warn "The default GEM_HOME (#{@home}) is not" \
- " writable, so rubygems is falling back to installing" \
- " under your home folder. To get rid of this warning" \
- " permanently either fix your GEM_HOME folder permissions" \
- " or add the following to your ~/.gemrc file:\n" \
- " gem: --install-dir #{Gem.user_dir}"
-
- @home = Gem.user_dir
+ # Current implementation of @home, which is exposed as `Gem.paths.home`:
+ # 1. If `env["GEM_HOME"]` is defined in the environment: `env["GEM_HOME"]`.
+ # 2. If `Gem.default_dir` is writable OR it does not exist and it's parent
+ # directory is writable: `Gem.default_dir`.
+ # 3. Otherwise: `Gem.user_dir`.
+
+ if env.key?("GEM_HOME")
+ @home = normalize_home_dir(env["GEM_HOME"])
+ elsif File.writable?(Gem.default_dir) || \
+ (!File.exist?(Gem.default_dir) && File.writable?(File.expand_path("..", Gem.default_dir)))
+
+ @home = normalize_home_dir(Gem.default_dir)
+ else
+ # If `GEM_HOME` is not set AND we can't use `Gem.default_dir`,
+ # default to a user installation and print a message about this.
+ puts "Defaulting to user installation because default GEM_HOME (#{Gem.default_dir}) is not writable."
+
+ @home = normalize_home_dir(Gem.user_dir)
end
@path = split_gem_path env["GEM_PATH"], @home
@@ -52,9 +57,7 @@ class Gem::PathSupport
# The default home directory.
# This function was broken out to accommodate tests in `bundler/spec/commands/doctor_spec.rb`.
- def default_home_dir(env)
- home = env["GEM_HOME"] || Gem.default_dir
-
+ def normalize_home_dir(home)
if File::ALT_SEPARATOR
home = home.gsub(File::ALT_SEPARATOR, File::SEPARATOR)
end