summarylogtreecommitdiffstats
path: root/Gem.default_install.patch
blob: 808e7828df6514105a0af4bfd2b380b3e2eb21be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
From 13fd35c291eee7dde9628b8923cc2a7d6853a1ef Mon Sep 17 00:00:00 2001
From: Felipe Contreras <felipe.contreras@gmail.com>
Date: Thu, 20 May 2021 16:26:25 -0500
Subject: [PATCH] Add Gem.default_install

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, but that doesn't work for bundler since
it doesn't read gem configurations.

The default installation directory should be Gem.user_dir, not
Gem.default_dir, but changing this invariably would break backwards
compatibility.

Instead we can add another default directory which is where gems will be
installed to by default.

Gem.default_dir is where the system gems are installed to,
Gem.default_install is where the gems installed by the user are
installed to.

The method Gem.default_install can be overridden by distributions to
Gem.user_dir, therefore making all installations user installations by
default (essentially the same as --user-install).

This way both bundler and gem will install gems in the same directory by
default 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 new behavior on.

If a user has sudo privileges, he/she can do --no-user-install to install
gems in the system directory (Gem.default_dir), which is the current
default behavior.

The current behavior remains unaffected: gems are still installed to
Gem.default_dir by default.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 lib/rubygems/defaults.rb       |  7 +++++++
 lib/rubygems/path_support.rb   |  2 +-
 test/rubygems/helper.rb        |  2 ++
 test/rubygems/test_rubygems.rb | 16 ++++++++++++++++
 4 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/lib/rubygems/defaults.rb b/lib/rubygems/defaults.rb
index 1bd208feb9..6b261a9412 100644
--- a/lib/rubygems/defaults.rb
+++ b/lib/rubygems/defaults.rb
@@ -9,6 +9,13 @@ module Gem
   @pre_uninstall_hooks  ||= []
   @pre_install_hooks    ||= []
 
+  ##
+  # Determines the default install directory
+
+  def self.default_install
+    @default_install ||= Gem.default_dir
+  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 13091e29ba..455f413f3d 100644
--- a/lib/rubygems/path_support.rb
+++ b/lib/rubygems/path_support.rb
@@ -24,7 +24,7 @@ class Gem::PathSupport
   # hashtable, or defaults to ENV, the system environment.
   #
   def initialize(env)
-    @home = normalize_home_dir(env["GEM_HOME"] || Gem.default_dir)
+    @home = normalize_home_dir(env["GEM_HOME"] || Gem.default_install)
     @path = split_gem_path env["GEM_PATH"], @home
 
     @spec_cache_dir = env["GEM_SPEC_CACHE"] || Gem.default_spec_cache_dir
diff --git a/test/rubygems/helper.rb b/test/rubygems/helper.rb
index ed9e34a906..2739847376 100644
--- a/test/rubygems/helper.rb
+++ b/test/rubygems/helper.rb
@@ -375,6 +375,7 @@ def setup
       @orig_default_gem_home = RbConfig::CONFIG["default_gem_home"]
       RbConfig::CONFIG["default_gem_home"] = @gemhome
     else
+      Gem.instance_variable_set(:@default_install, @gemhome)
       Gem.instance_variable_set(:@default_dir, @gemhome)
     end
 
@@ -466,6 +467,7 @@ def teardown
     if Gem.java_platform?
       RbConfig::CONFIG["default_gem_home"] = @orig_default_gem_home
     else
+      Gem.instance_variable_set :@default_install, nil
       Gem.instance_variable_set :@default_dir, nil
     end
 
diff --git a/test/rubygems/test_rubygems.rb b/test/rubygems/test_rubygems.rb
index ec195b65cd..385c204ce2 100644
--- a/test/rubygems/test_rubygems.rb
+++ b/test/rubygems/test_rubygems.rb
@@ -51,6 +51,22 @@ def self.default_dir
     end
   end
 
+  def test_operating_system_customizing_default_install
+    path = util_install_operating_system_rb <<-RUBY
+      module Gem
+        def self.default_install
+          Gem.user_dir
+        end
+      end
+    RUBY
+
+    assert system(
+      *ruby_with_rubygems_and_fake_operating_system_in_load_path(path),
+      "-e",
+      "raise unless Gem.dir == Gem.user_dir"
+    )
+  end
+
   private
 
   def util_install_operating_system_rb(content)
-- 
0.1