-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve handling of chaining #require_relative patch #401
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
47 changes: 47 additions & 0 deletions
47
test/load_path_cache/core_ext/kernel_require_relative_test.rb
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,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require("test_helper") | ||
require("bootsnap/load_path_cache") | ||
|
||
module Bootsnap | ||
module KernelRequireRelativeTest | ||
class KernelRequireRelativeTest < MiniTest::Test | ||
def setup | ||
@initial_dir = Dir.pwd | ||
@dir1 = File.realpath(Dir.mktmpdir) | ||
|
||
Bootsnap::LoadPathCache.setup( | ||
cache_path: "#{@dir1}/cache", | ||
development_mode: true, | ||
) | ||
FileUtils.touch("#{@dir1}/a.rb") | ||
File.open("#{@dir1}/a.rb", "wb") { |f| f.write("require_relative 'b.rb'\n") } | ||
FileUtils.touch("#{@dir1}/b.rb") | ||
|
||
# Chaining a relative_require patch after bootsnap's | ||
Kernel.module_eval do | ||
alias_method :pre_patch_require_relative, :require_relative | ||
undef :require_relative | ||
def require_relative(path) | ||
pre_patch_require_relative(path) | ||
end | ||
end | ||
end | ||
|
||
def teardown | ||
FileUtils.rm_rf(@dir1) | ||
Kernel.module_eval do | ||
module_function | ||
|
||
undef :require_relative | ||
alias_method :require_relative, :pre_patch_require_relative | ||
undef :pre_patch_require_relative | ||
end | ||
end | ||
|
||
def test_chaining_require_relative | ||
assert(require("#{@dir1}/a.rb")) | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum this increase the cost quite significantly.
Out of curiosity what else decorates
require_relative
? Maybe we could do something likecaller_locations(1..3)
to support up to two extra decorators?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, not aware of any public Gem decorate it, but our projects internally is about to decorate it to track some state change pre/post file load, and it's blocked by the decorator in bootnap.
Only traverse the first few caller seems fine to me. The chaining of require_relative may not happen that often probably.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have to look on Monday if we even still need to decorate
require_relative
. I think it we might be able to remove this decorator entirely.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so this is the change I remembered that may allow us to remove the
require_relative
decorator: ruby/ruby@79a4484. It shipped with Ruby 3.1, so that wouldn't remove it for everyone, but I'm much less concerned about a small perf impact if it doesn't apply to the newest rubies.The decorator was added in #136, I'll have to dig more to see what it would take to remove it on 3.1+.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum, the more I dig into #136, the more it seems like we tried to limit a Ruby bug more than a Bootsnap bug.
Assuming
a/test.rb
andb/test.rb
symlink each others, the following load the file twice on all rubies but 3.1:So I don't think we should have done this at all.