Skip to content

Commit 23cc922

Browse files
committed
Ensure only directories exist in Rails default load paths
Previously, some files put under `app` directory would contaminate the load paths. This commit removes files from the default load paths set up by the Rails framework. Now, only directories are included as default in the following paths: * autoload_paths * autoload_once_paths * eager_load_paths * load_paths
1 parent 0ecfbe2 commit 23cc922

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

railties/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
* Ensure only directories exist in Rails default load paths
2+
3+
Previously, some files put under `app` directory would contaminate the load paths.
4+
This commit removes files from the default load paths set up by the Rails framework.
5+
6+
Now, only directories are included as default in the following paths:
7+
8+
* autoload_paths
9+
* autoload_once_paths
10+
* eager_load_paths
11+
* load_paths
12+
13+
*Takumasa Ochi*
14+
115
* Prevent unnecessary application reloads in development.
216

317
Previously, some files outside autoload paths triggered unnecessary reloads.

railties/lib/rails/paths.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def load_paths
105105
private
106106
def filter_by(&block)
107107
all_paths.find_all(&block).flat_map { |path|
108-
paths = path.existent
109-
paths - path.children.flat_map { |p| yield(p) ? [] : p.existent }
108+
paths = path.existent_directories
109+
paths - path.children.flat_map { |p| yield(p) ? [] : p.existent_directories }
110110
}.uniq
111111
end
112112
end

railties/test/paths_test.rb

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def setup
101101
end
102102

103103
test "it is possible to add a path that should be autoloaded only once" do
104-
File.stub(:exist?, true) do
104+
File.stub(:directory?, true) do
105105
@root.add "app", with: "/app"
106106
@root["app"].autoload_once!
107107
assert_predicate @root["app"], :autoload_once?
@@ -120,15 +120,15 @@ def setup
120120
end
121121

122122
test "it is possible to add a path without assignment and specify it should be loaded only once" do
123-
File.stub(:exist?, true) do
123+
File.stub(:directory?, true) do
124124
@root.add "app", with: "/app", autoload_once: true
125125
assert_predicate @root["app"], :autoload_once?
126126
assert_includes @root.autoload_once, "/app"
127127
end
128128
end
129129

130130
test "it is possible to add multiple paths without assignment and specify it should be loaded only once" do
131-
File.stub(:exist?, true) do
131+
File.stub(:directory?, true) do
132132
@root.add "app", with: ["/app", "/app2"], autoload_once: true
133133
assert_predicate @root["app"], :autoload_once?
134134
assert_includes @root.autoload_once, "/app"
@@ -137,7 +137,7 @@ def setup
137137
end
138138

139139
test "making a path autoload_once more than once only includes it once in @root.load_once" do
140-
File.stub(:exist?, true) do
140+
File.stub(:directory?, true) do
141141
@root["app"] = "/app"
142142
@root["app"].autoload_once!
143143
@root["app"].autoload_once!
@@ -146,7 +146,7 @@ def setup
146146
end
147147

148148
test "paths added to a load_once path should be added to the autoload_once collection" do
149-
File.stub(:exist?, true) do
149+
File.stub(:directory?, true) do
150150
@root["app"] = "/app"
151151
@root["app"].autoload_once!
152152
@root["app"] << "/app2"
@@ -155,7 +155,7 @@ def setup
155155
end
156156

157157
test "it is possible to mark a path as eager loaded" do
158-
File.stub(:exist?, true) do
158+
File.stub(:directory?, true) do
159159
@root["app"] = "/app"
160160
@root["app"].eager_load!
161161
assert_predicate @root["app"], :eager_load?
@@ -174,15 +174,15 @@ def setup
174174
end
175175

176176
test "it is possible to add a path without assignment and mark it as eager" do
177-
File.stub(:exist?, true) do
177+
File.stub(:directory?, true) do
178178
@root.add "app", with: "/app", eager_load: true
179179
assert_predicate @root["app"], :eager_load?
180180
assert_includes @root.eager_load, "/app"
181181
end
182182
end
183183

184184
test "it is possible to add multiple paths without assignment and mark them as eager" do
185-
File.stub(:exist?, true) do
185+
File.stub(:directory?, true) do
186186
@root.add "app", with: ["/app", "/app2"], eager_load: true
187187
assert_predicate @root["app"], :eager_load?
188188
assert_includes @root.eager_load, "/app"
@@ -191,7 +191,7 @@ def setup
191191
end
192192

193193
test "it is possible to create a path without assignment and mark it both as eager and load once" do
194-
File.stub(:exist?, true) do
194+
File.stub(:directory?, true) do
195195
@root.add "app", with: "/app", eager_load: true, autoload_once: true
196196
assert_predicate @root["app"], :eager_load?
197197
assert_predicate @root["app"], :autoload_once?
@@ -201,7 +201,7 @@ def setup
201201
end
202202

203203
test "making a path eager more than once only includes it once in @root.eager_paths" do
204-
File.stub(:exist?, true) do
204+
File.stub(:directory?, true) do
205205
@root["app"] = "/app"
206206
@root["app"].eager_load!
207207
@root["app"].eager_load!
@@ -210,7 +210,7 @@ def setup
210210
end
211211

212212
test "paths added to an eager_load path should be added to the eager_load collection" do
213-
File.stub(:exist?, true) do
213+
File.stub(:directory?, true) do
214214
@root["app"] = "/app"
215215
@root["app"].eager_load!
216216
@root["app"] << "/app2"
@@ -243,7 +243,7 @@ def setup
243243
end
244244

245245
test "a path can be added to the load path" do
246-
File.stub(:exist?, true) do
246+
File.stub(:directory?, true) do
247247
@root["app"] = "app"
248248
@root["app"].load_path!
249249
@root["app/models"] = "app/models"
@@ -252,15 +252,15 @@ def setup
252252
end
253253

254254
test "a path can be added to the load path on creation" do
255-
File.stub(:exist?, true) do
255+
File.stub(:directory?, true) do
256256
@root.add "app", with: "/app", load_path: true
257257
assert_predicate @root["app"], :load_path?
258258
assert_equal ["/app"], @root.load_paths
259259
end
260260
end
261261

262262
test "a path can be marked as autoload path" do
263-
File.stub(:exist?, true) do
263+
File.stub(:directory?, true) do
264264
@root["app"] = "app"
265265
@root["app"].autoload!
266266
@root["app/models"] = "app/models"
@@ -269,12 +269,32 @@ def setup
269269
end
270270

271271
test "a path can be marked as autoload on creation" do
272-
File.stub(:exist?, true) do
272+
File.stub(:directory?, true) do
273273
@root.add "app", with: "/app", autoload: true
274274
assert_predicate @root["app"], :autoload?
275275
assert_equal ["/app"], @root.autoload_paths
276276
end
277277
end
278+
279+
test "load paths does NOT include files" do
280+
File.stub(:directory?, false) do
281+
@root.add "app/README.md", autoload_once: true, eager_load: true, autoload: true, load_path: true
282+
assert_equal [], @root.autoload_once
283+
assert_equal [], @root.eager_load
284+
assert_equal [], @root.autoload_paths
285+
assert_equal [], @root.load_paths
286+
end
287+
end
288+
289+
test "load paths does include directories" do
290+
File.stub(:directory?, true) do
291+
@root.add "app/special", autoload_once: true, eager_load: true, autoload: true, load_path: true
292+
assert_equal ["/foo/bar/app/special"], @root.autoload_once
293+
assert_equal ["/foo/bar/app/special"], @root.eager_load
294+
assert_equal ["/foo/bar/app/special"], @root.autoload_paths
295+
assert_equal ["/foo/bar/app/special"], @root.load_paths
296+
end
297+
end
278298
end
279299

280300
class PathsIntegrationTest < ActiveSupport::TestCase

0 commit comments

Comments
 (0)