提交 c701e0b8 编写于 作者: X Xavier Noria

Improves compatibility of require_dependency in zeitwerk mode [Closes #36774]

Applications are not supposed to use require_dependency in their own
code if running in zeitwerk mode, and require_dependency was initially
aliased to require with that use case in mind.

However, there are situations in which you cannot control the mode and
need to be compatible with both. There, you might need require_dependency
in case you are being executed in classic mode. Think about engines that
want to support both modes in their parent applications, for example.

Furthermore, Rails itself loads helpers using require_dependency.

Therefore, we need better compatibility.
上级 f5067e4d
* Let `require_dependency` in `zeitwerk` mode look the autoload paths up for
better backwards compatibility.
*Xavier Noria*
* Let `require_dependency` in `zeitwerk` mode support arguments that respond
to `to_path` for better backwards compatibility.
*Xavier Noria*
* Make ActiveSupport::Logger Fiber-safe. Fixes #36752. * Make ActiveSupport::Logger Fiber-safe. Fixes #36752.
Use `Fiber.current.__id__` in `ActiveSupport::Logger#local_level=` in order Use `Fiber.current.__id__` in `ActiveSupport::Logger#local_level=` in order
......
...@@ -42,6 +42,17 @@ def unhook! ...@@ -42,6 +42,17 @@ def unhook!
end end
end end
module RequireDependency
def require_dependency(filename)
filename = filename.to_path if filename.respond_to?(:to_path)
if abspath = ActiveSupport::Dependencies.search_for_file(filename)
require abspath
else
require filename
end
end
end
module Inflector module Inflector
def self.camelize(basename, _abspath) def self.camelize(basename, _abspath)
basename.camelize basename.camelize
...@@ -91,7 +102,7 @@ def freeze_paths ...@@ -91,7 +102,7 @@ def freeze_paths
def decorate_dependencies def decorate_dependencies
Dependencies.unhook! Dependencies.unhook!
Dependencies.singleton_class.prepend(Decorations) Dependencies.singleton_class.prepend(Decorations)
Object.class_eval { alias_method :require_dependency, :require } Object.prepend(RequireDependency)
end end
end end
end end
......
...@@ -150,6 +150,35 @@ def self.name ...@@ -150,6 +150,35 @@ def self.name
assert_empty deps.autoloaded_constants assert_empty deps.autoloaded_constants
end end
[true, false].each do |add_aps_to_lp|
test "require_dependency looks autoload paths up (#{add_aps_to_lp})" do
add_to_config "config.add_autoload_paths_to_load_path = #{add_aps_to_lp}"
app_file "app/models/user.rb", "class User; end"
boot
assert require_dependency("user")
end
test "require_dependency handles absolute paths correctly (#{add_aps_to_lp})" do
add_to_config "config.add_autoload_paths_to_load_path = #{add_aps_to_lp}"
app_file "app/models/user.rb", "class User; end"
boot
assert require_dependency("#{app_path}/app/models/user.rb")
end
test "require_dependency supports arguments that repond to to_path (#{add_aps_to_lp})" do
add_to_config "config.add_autoload_paths_to_load_path = #{add_aps_to_lp}"
app_file "app/models/user.rb", "class User; end"
boot
user = Object.new
def user.to_path; "user"; end
assert require_dependency(user)
end
end
test "eager loading loads the application code" do test "eager loading loads the application code" do
$zeitwerk_integration_test_user = false $zeitwerk_integration_test_user = false
$zeitwerk_integration_test_post = false $zeitwerk_integration_test_post = false
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册