Skip to content

Commit cf244b2

Browse files
committed
Add RealpathCache and make require_relative to require via realpath
1 parent 1cd87f3 commit cf244b2

4 files changed

Lines changed: 139 additions & 1 deletion

File tree

lib/bootsnap/load_path_cache.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ module LoadPathCache
2121
CACHED_EXTENSIONS = DLEXT2 ? [DOT_RB, DLEXT, DLEXT2] : [DOT_RB, DLEXT]
2222

2323
class << self
24-
attr_reader :load_path_cache, :autoload_paths_cache, :loaded_features_index
24+
attr_reader :load_path_cache, :autoload_paths_cache,
25+
:loaded_features_index, :realpath_cache
2526

2627
def setup(cache_path:, development_mode:, active_support: true)
2728
store = Store.new(cache_path)
2829

2930
@loaded_features_index = LoadedFeaturesIndex.new
31+
@realpath_cache = RealpathCache.new
3032

3133
@load_path_cache = Cache.new(store, $LOAD_PATH, development_mode: development_mode)
3234
require_relative 'load_path_cache/core_ext/kernel_require'
@@ -53,3 +55,4 @@ def setup(cache_path:, development_mode:, active_support: true)
5355
require_relative 'load_path_cache/store'
5456
require_relative 'load_path_cache/change_observer'
5557
require_relative 'load_path_cache/loaded_features_index'
58+
require_relative 'load_path_cache/realpath_cache'

lib/bootsnap/load_path_cache/core_ext/kernel_require.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ def require(path)
3636
require_with_bootsnap_lfi(path)
3737
end
3838

39+
alias_method :require_relative_without_bootsnap, :require_relative
40+
def require_relative(path)
41+
realpath = Bootsnap::LoadPathCache.realpath_cache.call(
42+
caller_locations(1..1).first.absolute_path, path
43+
)
44+
require(realpath)
45+
end
46+
3947
alias_method :load_without_bootsnap, :load
4048
def load(path, wrap = false)
4149
if resolved = Bootsnap::LoadPathCache.load_path_cache.find(path)
@@ -78,6 +86,14 @@ def require(path)
7886
require_with_bootsnap_lfi(path)
7987
end
8088

89+
alias_method :require_relative_without_bootsnap, :require_relative
90+
def require_relative(path)
91+
realpath = Bootsnap::LoadPathCache.realpath_cache.call(
92+
caller_locations(1..1).first.absolute_path, path
93+
)
94+
require(realpath)
95+
end
96+
8197
alias_method :load_without_bootsnap, :load
8298
def load(path, wrap = false)
8399
if resolved = Bootsnap::LoadPathCache.load_path_cache.find(path)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
module Bootsnap
4+
module LoadPathCache
5+
class RealpathCache
6+
def initialize
7+
@cache = Hash.new { |h, k| h[k] = realpath(*k) }
8+
end
9+
10+
def call(*key)
11+
@cache[key]
12+
end
13+
14+
private
15+
16+
def realpath(caller_location, path)
17+
base = File.dirname(caller_location)
18+
file = find_file(File.expand_path(path, base))
19+
dir = File.dirname(file)
20+
File.join(dir, File.basename(file))
21+
end
22+
23+
def find_file(name)
24+
['', *CACHED_EXTENSIONS].each do |ext|
25+
filename = "#{name}#{ext}"
26+
return File.realpath(filename) if File.exist?(filename)
27+
end
28+
name
29+
end
30+
end
31+
end
32+
end
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
module Bootsnap
6+
module LoadPathCache
7+
class RealpathCacheTest < MiniTest::Test
8+
EXTENSIONS = ['', *CACHED_EXTENSIONS]
9+
10+
def setup
11+
@cache = RealpathCache.new
12+
@base_dir = Dir.mktmpdir
13+
@absolute_dir = "#{@base_dir}/absolute"
14+
Dir.mkdir(@absolute_dir)
15+
16+
@symlinked_dir = "#{@base_dir}/symlink"
17+
FileUtils.ln_s(@absolute_dir, @symlinked_dir)
18+
19+
real_caller = File.new("#{@absolute_dir}/real_caller.rb", 'w').path
20+
symlinked_caller = "#{@absolute_dir}/symlinked_caller.rb"
21+
22+
FileUtils.ln_s(real_caller, symlinked_caller)
23+
24+
EXTENSIONS.each do |ext|
25+
real_required = File.new("#{@absolute_dir}/real_required#{ext}", 'w').path
26+
27+
symlinked_required = "#{@absolute_dir}/symlinked_required#{ext}"
28+
FileUtils.ln_s(real_required, symlinked_required)
29+
end
30+
end
31+
32+
def teardown
33+
FileUtils.remove_entry(@base_dir)
34+
end
35+
36+
def remove_required(extensions)
37+
extensions.each do |ext|
38+
FileUtils.rm("#{@absolute_dir}/real_required#{ext}")
39+
FileUtils.rm("#{@absolute_dir}/symlinked_required#{ext}")
40+
end
41+
end
42+
43+
variants = %w(absolute symlink).product(%w(absolute symlink),
44+
%w(real_caller symlinked_caller),
45+
%w(real_required symlinked_required))
46+
47+
variants.each do |caller_dir, required_dir, caller_file, required_file|
48+
method_name = "test_with_#{caller_dir}_caller_dir_" \
49+
"#{required_dir}_require_dir_" \
50+
"#{caller_file}_#{required_file}"
51+
define_method(method_name) do
52+
caller_path = "#{@base_dir}/#{caller_dir}/#{caller_file}"
53+
require_path = "../#{required_dir}/#{required_file}.rb"
54+
55+
expected = "#{@absolute_dir}/real_required.rb"
56+
57+
assert @cache.call(caller_path, require_path).eql?(expected)
58+
end
59+
60+
(EXTENSIONS.size - 1).times do |n|
61+
removing = EXTENSIONS[0..n]
62+
63+
define_method("#{method_name}_no#{removing.join('_')}_extensions") do
64+
caller_path = "#{@base_dir}/#{caller_dir}/#{caller_file}"
65+
require_path = "../#{required_dir}/#{required_file}"
66+
67+
remove_required(removing)
68+
69+
expected = "#{@absolute_dir}/real_required#{EXTENSIONS[n + 1]}"
70+
71+
assert @cache.call(caller_path, require_path).eql?(expected)
72+
end
73+
end
74+
75+
define_method("#{method_name}_no_files") do
76+
caller_path = "#{@base_dir}/#{caller_dir}/#{caller_file}"
77+
require_path = "../#{required_dir}/#{required_file}"
78+
79+
remove_required(EXTENSIONS)
80+
81+
expected = "#{@base_dir}/#{required_dir}/#{required_file}"
82+
assert @cache.call(caller_path, require_path).eql?(expected)
83+
end
84+
end
85+
end
86+
end
87+
end

0 commit comments

Comments
 (0)