Most people don't install gems in the default directory, but in
the user's directory (--user-install), this can be configured by
distributions using /etc/gemrc. However, that doesn't work for bundler,
since it doesn't read gem configurations.

Many bugs have been opened about this mismatch, but there's no easy
clean solution, except this.

The method Gem.user_install can be overridden by distributions to
enforce that all installs are user installs.

This way both bundler and gem will install gems in the same directory
without users having to manually set GEM_HOME to volatile locations such
as $HOME/.local/share/gem/ruby/3.0.0.

All distributions need to do is turn this on.

Signed-off-by: Felipe Contreras <felipe.contreras / gmail.com>
---
 lib/rubygems/defaults.rb     | 8 ++++++++
 lib/rubygems/path_support.rb | 3 ++-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/lib/rubygems/defaults.rb b/lib/rubygems/defaults.rb
index e95bc06792..5961882d40 100644
--- a/lib/rubygems/defaults.rb
+++ b/lib/rubygems/defaults.rb
@@ -8,6 +8,14 @@ module Gem
   @pre_uninstall_hooks  ||= []
   @pre_install_hooks    ||= []
 
+  ##
+  # Determines if gems should be installed in user_dir instead of default_dir by
+  # default
+
+  def self.user_install
+    false
+  end
+
   ##
   # An Array of the default sources that come with RubyGems
 
diff --git a/lib/rubygems/path_support.rb b/lib/rubygems/path_support.rb
index 8103caf324..a71265ce12 100644
--- a/lib/rubygems/path_support.rb
+++ b/lib/rubygems/path_support.rb
@@ -23,7 +23,8 @@ class Gem::PathSupport
   # hashtable, or defaults to ENV, the system environment.
   #
   def initialize(env)
-    @home = env["GEM_HOME"] || Gem.default_dir
+    gem_dir = Gem.user_install ? Gem.user_dir : Gem.default_dir
+    @home = env["GEM_HOME"] || gem_dir
 
     if File::ALT_SEPARATOR
       @home = @home.gsub(File::ALT_SEPARATOR, File::SEPARATOR)
-- 
2.32.0.rc0


Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>