Skip to content

Commit 085fa8b

Browse files
committed
Add RealpathCache and make require_relative to require via realpath
1 parent 3f7acf7 commit 085fa8b

3 files changed

Lines changed: 42 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
@@ -34,6 +34,14 @@ def require(path)
3434
require_with_bootsnap_lfi(path)
3535
end
3636

37+
alias_method :require_relative_without_bootsnap, :require_relative
38+
def require_relative(path)
39+
realpath = Bootsnap::LoadPathCache.realpath_cache.call(
40+
caller_locations.first.absolute_path, path
41+
)
42+
require(realpath)
43+
end
44+
3745
alias_method :load_without_bootsnap, :load
3846
def load(path, wrap = false)
3947
if resolved = Bootsnap::LoadPathCache.load_path_cache.find(path)
@@ -76,6 +84,14 @@ def require(path)
7684
require_with_bootsnap_lfi(path)
7785
end
7886

87+
alias_method :require_relative_without_bootsnap, :require_relative
88+
def require_relative(path)
89+
realpath = Bootsnap::LoadPathCache.realpath_cache.call(
90+
caller_locations.first.absolute_path, path
91+
)
92+
require(realpath)
93+
end
94+
7995
alias_method :load_without_bootsnap, :load
8096
def load(path, wrap = false)
8197
if resolved = Bootsnap::LoadPathCache.load_path_cache.find(path)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Bootsnap
2+
module LoadPathCache
3+
class RealpathCache
4+
def initialize
5+
@cache = Hash.new { |h, k| h[k] = realpath(*k) }
6+
end
7+
8+
def call(*key)
9+
@cache[key]
10+
end
11+
12+
private
13+
14+
def realpath(caller_location, path)
15+
base = File.dirname(caller_location)
16+
file = File.expand_path(path, base)
17+
dir = File.dirname(file)
18+
File.join(File.realpath(dir), File.basename(file))
19+
end
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)