-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This fixes an issue where we were defeating ruby's ability to detect that a feature's name-as-required has already been loaded in situations where the $LOAD_PATH changed in such a way as to make the feature expand to a new absolute path.
- Loading branch information
Showing
5 changed files
with
221 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
module Bootsnap | ||
module LoadPathCache | ||
# LoadedFeaturesIndex partially mirrors an internal structure in ruby that | ||
# we can't easily obtain an interface to. | ||
# | ||
# This works around an issue where, without bootsnap, *ruby* knows that it | ||
# has already required a file by its short name (e.g. require 'bundler') if | ||
# a new instance of bundler is added to the $LOAD_PATH which resolves to a | ||
# different absolute path. This class makes bootsnap smart enough to | ||
# realize that it has already loaded 'bundler', and not just | ||
# '/path/to/bundler'. | ||
# | ||
# If you disable LoadedFeaturesIndex, you can see the problem this solves by: | ||
# | ||
# 1. `require 'a'` | ||
# 2. Prepend a new $LOAD_PATH element containing an `a.rb` | ||
# 3. `require 'a'` | ||
# | ||
# Ruby returns false from step 3. | ||
# With bootsnap but with no LoadedFeaturesIndex, this loads two different | ||
# `a.rb`s. | ||
# With bootsnap and with LoadedFeaturesIndex, this skips the second load, | ||
# returning false like ruby. | ||
class LoadedFeaturesIndex | ||
def initialize | ||
@lfi = {} | ||
@mutex = defined?(::Mutex) ? ::Mutex.new : ::Thread::Mutex.new # TODO: Remove once Ruby 2.2 support is dropped. | ||
|
||
# In theory the user could mutate $LOADED_FEATURES and invalidate our | ||
# cache. If this ever comes up in practice — or if you, the | ||
# enterprising reader, feels inclined to solve this problem — we could | ||
# parallel the work done with ChangeObserver on $LOAD_PATH to mirror | ||
# updates to our @lfi. | ||
$LOADED_FEATURES.each do |feat| | ||
$LOAD_PATH.each do |lpe| | ||
next unless feat.start_with?(lpe) | ||
# /a/b/lib/my/foo.rb | ||
# ^^^^^^^^^ | ||
short = feat[(lpe.length + 1)..-1] | ||
@lfi[short] = true | ||
@lfi[strip_extension(short)] = true | ||
end | ||
end | ||
end | ||
|
||
def key?(feature) | ||
@mutex.synchronize { @lfi.key?(feature) } | ||
end | ||
|
||
# There is a relatively uncommon case where we could miss adding an | ||
# entry: | ||
# | ||
# If the user asked for e.g. `require 'bundler'`, and we went through the | ||
# `FallbackScan` pathway in `kernel_require.rb` and therefore did not | ||
# pass `long` (the full expanded absolute path), then we did are not able | ||
# to confidently add the `bundler.rb` form to @lfi. | ||
# | ||
# We could either: | ||
# | ||
# 1. Just add `bundler.rb`, `bundler.so`, and so on, which is close but | ||
# not quite right; or | ||
# 2. Inspect $LOADED_FEATURES upon return from yield to find the matching | ||
# entry. | ||
def register(short, long = nil) | ||
ret = yield | ||
|
||
# do we have 'bundler' or 'bundler.rb'? | ||
altname = if File.extname(short) != '' | ||
# strip the path from 'bundler.rb' -> 'bundler' | ||
strip_extension(short) | ||
elsif long && ext = File.extname(long) | ||
# get the extension from the expanded path if given | ||
# 'bundler' + '.rb' | ||
short + ext | ||
end | ||
|
||
@mutex.synchronize do | ||
@lfi[short] = true | ||
(@lfi[altname] = true) if altname | ||
end | ||
|
||
ret | ||
end | ||
|
||
private | ||
|
||
STRIP_EXTENSION = /\..*?$/ | ||
private_constant :STRIP_EXTENSION | ||
|
||
def strip_extension(f) | ||
f.sub(STRIP_EXTENSION, '') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Bootsnap | ||
VERSION = "1.1.8" | ||
VERSION = "1.2.0.pre" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'test_helper' | ||
|
||
module Bootsnap | ||
module LoadPathCache | ||
class LoadedFeaturesIndexTest < MiniTest::Test | ||
def setup | ||
@index = LoadedFeaturesIndex.new | ||
# not really necessary but let's just make it a clean slate | ||
@index.instance_variable_set(:@lfi, {}) | ||
end | ||
|
||
def test_successful_addition | ||
refute @index.key?('bundler') | ||
refute @index.key?('bundler.rb') | ||
refute @index.key?('foo') | ||
@index.register('bundler', '/a/b/bundler.rb') {} | ||
assert @index.key?('bundler') | ||
assert @index.key?('bundler.rb') | ||
refute @index.key?('foo') | ||
end | ||
|
||
def test_no_add_on_raise | ||
refute @index.key?('bundler') | ||
refute @index.key?('bundler.rb') | ||
refute @index.key?('foo') | ||
assert_raises(RuntimeError) do | ||
@index.register('bundler', '/a/b/bundler.rb') { raise } | ||
end | ||
refute @index.key?('bundler') | ||
refute @index.key?('bundler.rb') | ||
refute @index.key?('foo') | ||
end | ||
|
||
def test_infer_base_from_ext | ||
refute @index.key?('bundler') | ||
refute @index.key?('bundler.rb') | ||
refute @index.key?('foo') | ||
@index.register('bundler.rb') {} | ||
assert @index.key?('bundler') | ||
assert @index.key?('bundler.rb') | ||
refute @index.key?('foo') | ||
end | ||
|
||
def test_cannot_infer_ext_from_base # Current limitation | ||
refute @index.key?('bundler') | ||
refute @index.key?('bundler.rb') | ||
refute @index.key?('foo') | ||
@index.register('bundler') {} | ||
assert @index.key?('bundler') | ||
refute @index.key?('bundler.rb') | ||
refute @index.key?('foo') | ||
end | ||
|
||
def test_derives_initial_state_from_loaded_features | ||
index = LoadedFeaturesIndex.new | ||
assert index.key?('minitest/autorun') | ||
assert index.key?('minitest/autorun.rb') | ||
refute index.key?('minitest/autorun.so') | ||
end | ||
end | ||
end | ||
end |