Skip to content
This repository
Browse code

Deprecate the `eager_load_paths` configuration

Since the default in Rails 4.0 is to run in 'threadsafe' mode we need
to eager load all of the paths in `autoload_paths` so we alias
`eager_load_paths` to it. This may have unintended consequences if
you have added 'lib' to `autoload_paths` such as loading unneeded
code or code intended only for development and/or test environments.
If this applies to your application you should thoroughly check what
is being eager loaded.
  • Loading branch information...
commit 0757b3388ffe4f44b60de950d40e18ef05055931 1 parent ccaeb6b
Andrew White authored
9  railties/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,14 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   Deprecate the `eager_load_paths` configuration and alias it to `autoload_paths`.
  4
+    Since the default in Rails 4.0 is to run in 'threadsafe' mode we need to eager
  5
+    load all of the paths in `autoload_paths`. This may have unintended consequences
  6
+    if you have added 'lib' to `autoload_paths` such as loading unneeded code or
  7
+    code intended only for development and/or test environments. If this applies to
  8
+    your application you should thoroughly check what is being eager loaded.
  9
+
  10
+    *Andrew White*
  11
+
3 12
 *   Restore Rails::Engine::Railties#engines with deprecation to ensure
4 13
     compatibility with gems such as Thinking Sphinx
5 14
     Fix #8551
12  railties/lib/rails/engine.rb
@@ -34,9 +34,8 @@ module Rails
34 34
   # == Configuration
35 35
   #
36 36
   # Besides the +Railtie+ configuration which is shared across the application, in a
37  
-  # <tt>Rails::Engine</tt> you can access <tt>autoload_paths</tt>, <tt>eager_load_paths</tt>
38  
-  # and <tt>autoload_once_paths</tt>, which, differently from a <tt>Railtie</tt>, are scoped to
39  
-  # the current engine.
  37
+  # <tt>Rails::Engine</tt> you can access <tt>autoload_paths</tt> and <tt>autoload_once_paths</tt>,
  38
+  # which, differently from a <tt>Railtie</tt>, are scoped to the current engine.
40 39
   #
41 40
   #   class MyEngine < Rails::Engine
42 41
   #     # Add a load path for this specific Engine
@@ -456,9 +455,9 @@ def load_generators(app=self)
456 455
     end
457 456
 
458 457
     # Eager load the application by loading all ruby
459  
-    # files inside eager_load paths.
  458
+    # files inside autoload_paths.
460 459
     def eager_load!
461  
-      config.eager_load_paths.each do |load_path|
  460
+      config.autoload_paths.each do |load_path|
462 461
         matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/
463 462
         Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
464 463
           require_dependency file.sub(matcher, '\1')
@@ -558,7 +557,6 @@ def load_seed
558 557
 
559 558
       # Freeze so future modifications will fail rather than do nothing mysteriously
560 559
       config.autoload_paths.freeze
561  
-      config.eager_load_paths.freeze
562 560
       config.autoload_once_paths.freeze
563 561
     end
564 562
 
@@ -671,7 +669,7 @@ def _all_autoload_once_paths #:nodoc:
671 669
     end
672 670
 
673 671
     def _all_autoload_paths #:nodoc:
674  
-      @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
  672
+      @_all_autoload_paths ||= (config.autoload_paths + config.autoload_once_paths).uniq
675 673
     end
676 674
 
677 675
     def _all_load_paths #:nodoc:
24  railties/lib/rails/engine/configuration.rb
@@ -4,7 +4,7 @@ module Rails
4 4
   class Engine
5 5
     class Configuration < ::Rails::Railtie::Configuration
6 6
       attr_reader :root
7  
-      attr_writer :middleware, :eager_load_paths, :autoload_once_paths, :autoload_paths
  7
+      attr_writer :middleware, :autoload_once_paths, :autoload_paths
8 8
 
9 9
       def initialize(root=nil)
10 10
         super()
@@ -39,16 +39,16 @@ def paths
39 39
         @paths ||= begin
40 40
           paths = Rails::Paths::Root.new(@root)
41 41
 
42  
-          paths.add "app",                 eager_load: true, glob: "*"
  42
+          paths.add "app",                 autoload: true, glob: "*"
43 43
           paths.add "app/assets",          glob: "*"
44  
-          paths.add "app/controllers",     eager_load: true
45  
-          paths.add "app/helpers",         eager_load: true
46  
-          paths.add "app/models",          eager_load: true
47  
-          paths.add "app/mailers",         eager_load: true
  44
+          paths.add "app/controllers",     autoload: true
  45
+          paths.add "app/helpers",         autoload: true
  46
+          paths.add "app/models",          autoload: true
  47
+          paths.add "app/mailers",         autoload: true
48 48
           paths.add "app/views"
49 49
 
50  
-          paths.add "app/controllers/concerns", eager_load: true
51  
-          paths.add "app/models/concerns",      eager_load: true
  50
+          paths.add "app/controllers/concerns", autoload: true
  51
+          paths.add "app/models/concerns",      autoload: true
52 52
 
53 53
           paths.add "lib",                 load_path: true
54 54
           paths.add "lib/assets",          glob: "*"
@@ -76,7 +76,13 @@ def root=(value)
76 76
       end
77 77
 
78 78
       def eager_load_paths
79  
-        @eager_load_paths ||= paths.eager_load
  79
+        ActiveSupport::Deprecation.warn "eager_load_paths is deprecated and all autoload_paths are now eagerly loaded."
  80
+        autoload_paths
  81
+      end
  82
+
  83
+      def eager_load_paths=(paths)
  84
+        ActiveSupport::Deprecation.warn "eager_load_paths is deprecated and all autoload_paths are now eagerly loaded."
  85
+        self.autoload_paths = paths
80 86
       end
81 87
 
82 88
       def autoload_once_paths
44  railties/lib/rails/paths.rb
@@ -5,13 +5,13 @@ module Paths
5 5
     # paths by a Hash like API. It requires you to give a physical path on initialization.
6 6
     #
7 7
     #   root = Root.new "/rails"
8  
-    #   root.add "app/controllers", eager_load: true
  8
+    #   root.add "app/controllers", autoload: true
9 9
     #
10 10
     # The command above creates a new root object and add "app/controllers" as a path.
11 11
     # This means we can get a <tt>Rails::Paths::Path</tt> object back like below:
12 12
     #
13 13
     #   path = root["app/controllers"]
14  
-    #   path.eager_load?               # => true
  14
+    #   path.autoload?                 # => true
15 15
     #   path.is_a?(Rails::Paths::Path) # => true
16 16
     #
17 17
     # The +Path+ object is simply an enumerable and allows you to easily add extra paths:
@@ -30,7 +30,7 @@ module Paths
30 30
     #   root["config/routes"].inspect # => ["config/routes.rb"]
31 31
     #
32 32
     # The +add+ method accepts the following options as arguments:
33  
-    # eager_load, autoload, autoload_once and glob.
  33
+    # autoload, autoload_once and glob.
34 34
     #
35 35
     # Finally, the +Path+ object also provides a few helpers:
36 36
     #
@@ -85,7 +85,8 @@ def autoload_once
85 85
       end
86 86
 
87 87
       def eager_load
88  
-        filter_by(:eager_load?)
  88
+        ActiveSupport::Deprecation.warn "eager_load is deprecated and all autoload_paths are now eagerly loaded."
  89
+        filter_by(:autoload?)
89 90
       end
90 91
 
91 92
       def autoload_paths
@@ -124,9 +125,13 @@ def initialize(root, current, paths, options = {})
124 125
         @glob     = options[:glob]
125 126
 
126 127
         options[:autoload_once] ? autoload_once! : skip_autoload_once!
127  
-        options[:eager_load]    ? eager_load!    : skip_eager_load!
128 128
         options[:autoload]      ? autoload!      : skip_autoload!
129 129
         options[:load_path]     ? load_path!     : skip_load_path!
  130
+
  131
+        if !options.key?(:autoload) && options.key?(:eager_load)
  132
+          ActiveSupport::Deprecation.warn "the :eager_load option is deprecated and all :autoload paths are now eagerly loaded."
  133
+          options[:eager_load] ? autoload! : skip_autoload!
  134
+        end
130 135
       end
131 136
 
132 137
       def children
@@ -143,22 +148,37 @@ def last
143 148
         expanded.last
144 149
       end
145 150
 
146  
-      %w(autoload_once eager_load autoload load_path).each do |m|
  151
+      %w(autoload_once autoload load_path).each do |m|
147 152
         class_eval <<-RUBY, __FILE__, __LINE__ + 1
148  
-          def #{m}!        # def eager_load!
149  
-            @#{m} = true   #   @eager_load = true
  153
+          def #{m}!        # def autoload!
  154
+            @#{m} = true   #   @autoload = true
150 155
           end              # end
151 156
                            #
152  
-          def skip_#{m}!   # def skip_eager_load!
153  
-            @#{m} = false  #   @eager_load = false
  157
+          def skip_#{m}!   # def skip_autoload!
  158
+            @#{m} = false  #   @autoload = false
154 159
           end              # end
155 160
                            #
156  
-          def #{m}?        # def eager_load?
157  
-            @#{m}          #   @eager_load
  161
+          def #{m}?        # def autoload?
  162
+            @#{m}          #   @autoload
158 163
           end              # end
159 164
         RUBY
160 165
       end
161 166
 
  167
+      def eager_load!
  168
+        ActiveSupport::Deprecation.warn "eager_load paths are deprecated and all autoload paths are now eagerly loaded."
  169
+        autoload!
  170
+      end
  171
+
  172
+      def skip_eager_load!
  173
+        ActiveSupport::Deprecation.warn "eager_load paths are deprecated and all autoload paths are now eagerly loaded."
  174
+        skip_autoload!
  175
+      end
  176
+
  177
+      def eager_load?
  178
+        ActiveSupport::Deprecation.warn "eager_load paths are deprecated and all autoload paths are now eagerly loaded."
  179
+        autoload?
  180
+      end
  181
+
162 182
       def each(&block)
163 183
         @paths.each(&block)
164 184
       end
2  railties/test/application/initializers/load_path_test.rb
@@ -64,7 +64,7 @@ module Zoo::ReptileHouse ; end
64 64
 
65 65
       add_to_config <<-RUBY
66 66
         config.root = "#{app_path}"
67  
-        config.eager_load_paths << "#{app_path}/lib"
  67
+        config.autoload_paths << "#{app_path}/lib"
68 68
       RUBY
69 69
 
70 70
       require "#{app_path}/config/environment"
10  railties/test/application/paths_test.rb
@@ -54,11 +54,11 @@ def assert_not_in_load_path(*path)
54 54
       assert_equal root("app", "controllers"), @paths["app/controllers"].expanded.first
55 55
     end
56 56
 
57  
-    test "booting up Rails yields a list of paths that are eager" do
58  
-      eager_load = @paths.eager_load
59  
-      assert eager_load.include?(root("app/controllers"))
60  
-      assert eager_load.include?(root("app/helpers"))
61  
-      assert eager_load.include?(root("app/models"))
  57
+    test "booting up Rails yields a list of paths that will be eager loaded" do
  58
+      autoload_paths = @paths.autoload_paths
  59
+      assert autoload_paths.include?(root("app/controllers"))
  60
+      assert autoload_paths.include?(root("app/helpers"))
  61
+      assert autoload_paths.include?(root("app/models"))
62 62
     end
63 63
 
64 64
     test "environments has a glob equal to the current environment" do
56  railties/test/paths_test.rb
@@ -135,56 +135,48 @@ def setup
135 135
     assert_equal 2, @root.autoload_once.size
136 136
   end
137 137
 
138  
-  test "it is possible to mark a path as eager loaded" do
  138
+  test "marking a path as eager loaded is deprecated" do
139 139
     @root["app"] = "/app"
140  
-    @root["app"].eager_load!
141  
-    assert @root["app"].eager_load?
142  
-    assert @root.eager_load.include?(@root["app"].to_a.first)
  140
+    assert_deprecated{ @root["app"].eager_load! }
  141
+    assert_deprecated{ assert @root["app"].eager_load? }
  142
+    assert_deprecated{ assert @root.eager_load.include?(@root["app"].to_a.first) }
143 143
   end
144 144
 
145  
-  test "it is possible to skip a path from eager loading" do
  145
+  test "skipping a path from eager loading is deprecated" do
146 146
     @root["app"] = "/app"
147  
-    @root["app"].eager_load!
148  
-    assert @root["app"].eager_load?
  147
+    assert_deprecated{ @root["app"].eager_load! }
  148
+    assert_deprecated{ assert @root["app"].eager_load? }
149 149
 
150  
-    @root["app"].skip_eager_load!
151  
-    assert !@root["app"].eager_load?
152  
-    assert !@root.eager_load.include?(@root["app"].to_a.first)
  150
+    assert_deprecated{ @root["app"].skip_eager_load! }
  151
+    assert_deprecated{ assert !@root["app"].eager_load? }
  152
+    assert_deprecated{ assert !@root.eager_load.include?(@root["app"].to_a.first) }
153 153
   end
154 154
 
155  
-  test "it is possible to add a path without assignment and mark it as eager" do
156  
-    @root.add "app", with: "/app", eager_load: true
157  
-    assert @root["app"].eager_load?
158  
-    assert @root.eager_load.include?("/app")
  155
+  test "adding a path with eager_load option is deprecated" do
  156
+    assert_deprecated{ @root.add "app", with: "/app", eager_load: true }
  157
+    assert_deprecated{ assert @root["app"].eager_load? }
  158
+    assert_deprecated{ assert @root.eager_load.include?("/app") }
159 159
   end
160 160
 
161  
-  test "it is possible to add multiple paths without assignment and mark them as eager" do
162  
-    @root.add "app", with: ["/app", "/app2"], eager_load: true
163  
-    assert @root["app"].eager_load?
164  
-    assert @root.eager_load.include?("/app")
165  
-    assert @root.eager_load.include?("/app2")
166  
-  end
167  
-
168  
-  test "it is possible to create a path without assignment and mark it both as eager and load once" do
169  
-    @root.add "app", with: "/app", eager_load: true, autoload_once: true
170  
-    assert @root["app"].eager_load?
171  
-    assert @root["app"].autoload_once?
172  
-    assert @root.eager_load.include?("/app")
173  
-    assert @root.autoload_once.include?("/app")
  161
+  test "adding multiple paths with eager_load option is deprecated" do
  162
+    assert_deprecated{ @root.add "app", with: ["/app", "/app2"], eager_load: true }
  163
+    assert_deprecated{ assert @root["app"].eager_load? }
  164
+    assert_deprecated{ assert @root.eager_load.include?("/app") }
  165
+    assert_deprecated{ assert @root.eager_load.include?("/app2") }
174 166
   end
175 167
 
176 168
   test "making a path eager more than once only includes it once in @root.eager_paths" do
177 169
     @root["app"] = "/app"
178  
-    @root["app"].eager_load!
179  
-    @root["app"].eager_load!
180  
-    assert_equal 1, @root.eager_load.select {|p| p == @root["app"].expanded.first }.size
  170
+    assert_deprecated{ @root["app"].eager_load! }
  171
+    assert_deprecated{ @root["app"].eager_load! }
  172
+    assert_deprecated{ assert_equal 1, @root.eager_load.select {|p| p == @root["app"].expanded.first }.size }
181 173
   end
182 174
 
183 175
   test "paths added to a eager_load path should be added to the eager_load collection" do
184 176
     @root["app"] = "/app"
185  
-    @root["app"].eager_load!
  177
+    assert_deprecated{ @root["app"].eager_load! }
186 178
     @root["app"] << "/app2"
187  
-    assert_equal 2, @root.eager_load.size
  179
+    assert_deprecated{ assert_equal 2, @root.eager_load.size }
188 180
   end
189 181
 
190 182
   test "it should be possible to add a path's default glob" do

0 notes on commit 0757b33

Please sign in to comment.
Something went wrong with that request. Please try again.