Skip to content

Commit c5bb138

Browse files
authored
Merge pull request rails#54129 from ermolaev/perf-file-cheker
perf FileUpdateChecker, ignore gems paths
2 parents c049370 + d5a3063 commit c5bb138

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

activesupport/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* FileUpdateChecker and EventedFileUpdateChecker ignore changes in Gem.path now.
2+
3+
*Ermolaev Andrey*, *zzak*
4+
15
* The new method `ActiveSupport::BacktraceCleaner#first_clean_frame` returns
26
the first clean frame of the caller's backtrace, or `nil`. Useful when you
37
want to report the application-level location where something happened.

activesupport/lib/active_support/evented_file_update_checker.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ class Core
7373
attr_reader :updated, :files
7474

7575
def initialize(files, dirs)
76-
@files = files.map { |file| Pathname(file).expand_path }.to_set
76+
gem_paths = Gem.path
77+
files = files.map { |f| Pathname(f).expand_path }
78+
files.reject! { |f| f.to_s.start_with?(*gem_paths) }
79+
@files = files.to_set
7780

7881
@dirs = dirs.each_with_object({}) do |(dir, exts), hash|
82+
next if dir.start_with?(*gem_paths)
7983
hash[Pathname(dir).expand_path] = Array(exts).map { |ext| ext.to_s.sub(/\A\.?/, ".") }.to_set
8084
end
8185

activesupport/lib/active_support/file_update_checker.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ def initialize(files, dirs = {}, &block)
4646
raise ArgumentError, "A block is required to initialize a FileUpdateChecker"
4747
end
4848

49-
@files = files.freeze
50-
@globs = compile_glob(dirs)
49+
gem_paths = Gem.path
50+
@files = files.reject { |file| File.expand_path(file).start_with?(*gem_paths) }.freeze
51+
52+
@globs = compile_glob(dirs)&.reject { |dir| dir.start_with?(*gem_paths) }
53+
5154
@block = block
5255

5356
@watched = nil

activesupport/lib/active_support/i18n_railtie.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ def self.initialize_i18n(app)
6666

6767
if app.config.reloading_enabled?
6868
directories = watched_dirs_with_extensions(reloadable_paths)
69-
root_load_paths = I18n.load_path.select { |path| path.to_s.start_with?(Rails.root.to_s) }
70-
reloader = app.config.file_watcher.new(root_load_paths, directories) do
69+
reloader = app.config.file_watcher.new(I18n.load_path, directories) do
7170
I18n.load_path.delete_if { |path| path.to_s.start_with?(Rails.root.to_s) && !File.exist?(path) }
7271
I18n.load_path |= reloadable_paths.flat_map(&:existent)
7372
end

activesupport/test/file_update_checker_shared_tests.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,51 @@ def run(*args)
3333
assert_equal 0, i
3434
end
3535

36+
test "should exclude files in gem path" do
37+
fake_gem_dir = File.join(tmpdir, "gemdir")
38+
FileUtils.mkdir_p(fake_gem_dir)
39+
gem_file = File.join(fake_gem_dir, "foo.rb")
40+
local_file = tmpfile("bar.rb")
41+
touched = []
42+
43+
Gem.stub(:path, [fake_gem_dir]) do
44+
checker = new_checker([gem_file, local_file]) { touched << :called }
45+
46+
touch(local_file)
47+
assert checker.execute_if_updated
48+
assert_equal [:called], touched
49+
50+
touched.clear
51+
touch(gem_file)
52+
assert_not checker.execute_if_updated
53+
assert_empty touched
54+
end
55+
end
56+
57+
test "should exclude directories in gem path" do
58+
local_dir = Dir.mktmpdir
59+
fake_gem_dir = Dir.mktmpdir
60+
local_file = File.join(local_dir, "foo.rb")
61+
gem_file = File.join(fake_gem_dir, "bar.rb")
62+
touched = []
63+
64+
Gem.stub(:path, [fake_gem_dir]) do
65+
checker = new_checker([], { fake_gem_dir => [], local_dir => [] }) { touched << :called }
66+
67+
touch(local_file)
68+
assert checker.execute_if_updated
69+
assert_equal [:called], touched
70+
71+
touched.clear
72+
touch(gem_file)
73+
assert_not checker.execute_if_updated
74+
assert_empty touched
75+
end
76+
ensure
77+
FileUtils.remove_entry(local_dir)
78+
FileUtils.remove_entry(fake_gem_dir)
79+
end
80+
3681
test "should not execute the block if no files change" do
3782
i = 0
3883

0 commit comments

Comments
 (0)