Skip to content

Commit 6c7cb2a

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

4 files changed

Lines changed: 91 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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 = File.realpath(File.expand_path(path, base))
19+
dir = File.dirname(file)
20+
File.join(dir, File.basename(file))
21+
end
22+
end
23+
end
24+
end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
module Bootsnap
6+
module LoadPathCache
7+
class RealpathCacheTest < MiniTest::Test
8+
def setup
9+
@cache = RealpathCache.new
10+
@base_dir = Dir.mktmpdir
11+
@absolute_dir = "#{@base_dir}/absolute"
12+
Dir.mkdir(@absolute_dir)
13+
14+
@real_caller = File.new("#{@absolute_dir}/real_caller.rb", 'w').path
15+
@real_required = File.new("#{@absolute_dir}/real_required.rb", 'w').path
16+
17+
@symlinked_dir = "#{@base_dir}/symlink"
18+
FileUtils.ln_s(@absolute_dir, @symlinked_dir)
19+
20+
@symlinked_caller = "#{@absolute_dir}/symlinked_caller.rb"
21+
FileUtils.ln_s(@real_caller, @symlinked_caller)
22+
23+
@symlinked_required = "#{@absolute_dir}/symlinked_required.rb"
24+
FileUtils.ln_s(@real_required, @symlinked_required)
25+
end
26+
27+
def teardown
28+
FileUtils.remove_entry(@base_dir)
29+
end
30+
31+
variants = %w(absolute symlink).product(%w(absolute symlink),
32+
%w(real_caller symlinked_caller),
33+
%w(real_required symlinked_required))
34+
35+
variants.each do |caller_dir, required_dir, caller_file, required_file|
36+
method_name = "test_with_#{caller_dir}_caller_dir_" \
37+
"#{required_dir}_require_dir_" \
38+
"#{caller_file}_#{required_file}"
39+
define_method(method_name) do
40+
caller_path = "#{@base_dir}/#{caller_dir}/#{caller_file}"
41+
require_path = "../#{required_dir}/#{required_file}.rb"
42+
assert @cache.call(caller_path, require_path).eql?(@real_required)
43+
end
44+
end
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)