Skip to content

Commit

Permalink
Deletes the classic implementation of require_dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Aug 9, 2021
1 parent 5422751 commit 6983a89
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
33 changes: 2 additions & 31 deletions activesupport/lib/active_support/dependencies.rb
Expand Up @@ -17,6 +17,8 @@

module ActiveSupport # :nodoc:
module Dependencies # :nodoc:
require_relative "dependencies/require_dependency"

extend self

UNBOUND_METHOD_MODULE_NAME = Module.instance_method(:name)
Expand Down Expand Up @@ -250,37 +252,6 @@ def require_or_load(file_name)
Dependencies.require_or_load(file_name)
end

# :doc:

# <b>Warning:</b> This method is obsolete in +:zeitwerk+ mode. In
# +:zeitwerk+ mode semantics match Ruby's and you do not need to be
# defensive with load order. Just refer to classes and modules normally.
# If the constant name is dynamic, camelize if needed, and constantize.
#
# In +:classic+ mode, interprets a file using +mechanism+ and marks its
# defined constants as autoloaded. +file_name+ can be either a string or
# respond to <tt>to_path</tt>.
#
# In +:classic+ mode, use this method in code that absolutely needs a
# certain constant to be defined at that point. A typical use case is to
# make constant name resolution deterministic for constants with the same
# relative name in different namespaces whose evaluation would depend on
# load order otherwise.
#
# Engines that do not control the mode in which their parent application
# runs should call +require_dependency+ where needed in case the runtime
# mode is +:classic+.
def require_dependency(file_name, message = "No such file to load -- %s.rb")
file_name = file_name.to_path if file_name.respond_to?(:to_path)
unless file_name.is_a?(String)
raise ArgumentError, "the file name must either be a String or implement #to_path -- you passed #{file_name.inspect}"
end

Dependencies.depend_on(file_name, message)
end

# :nodoc:

def load_dependency(file)
if Dependencies.load? && Dependencies.constant_watch_stack.watching?
descs = Dependencies.constant_watch_stack.watching.flatten.uniq
Expand Down
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module ActiveSupport::Dependencies::RequireDependency
# <b>Warning:</b> This method is obsolete. The semantics of the autoloader
# match Ruby's and you do not need to be defensive with load order anymore.
# Just refer to classes and modules normally.
#
# Engines that do not control the mode in which their parent application runs
# should call +require_dependency+ where needed in case the runtime mode is
# +:classic+.
def require_dependency(filename)
filename = filename.to_path if filename.respond_to?(:to_path)

unless filename.is_a?(String)
raise ArgumentError, "the file name must be either a String or implement #to_path -- you passed #{filename.inspect}"
end

if abspath = ActiveSupport::Dependencies.search_for_file(filename)
require abspath
else
require filename
end
end

# We could define require_dependency in Object directly, but a module makes
# the extension apparent if you list ancestors.
Object.prepend(self)
end
6 changes: 2 additions & 4 deletions activesupport/test/dependencies_test.rb
Expand Up @@ -46,10 +46,6 @@ def test_depend_on_message
assert_equal "No such file to load -- omgwtfbbq.rb", e.message
end

def test_missing_dependency_raises_missing_source_file
assert_raise(LoadError) { require_dependency("missing_service") }
end

def test_smart_name_error_strings
e = assert_raise NameError do
Object.module_eval "ImaginaryObject"
Expand Down Expand Up @@ -118,4 +114,6 @@ def test_load_and_require_stay_private
ensure
ActiveSupport::Dependencies.hook!
end

# Coverage for require_dependency can be found in the railties test suite.
end
8 changes: 8 additions & 0 deletions railties/test/application/zeitwerk_integration_test.rb
Expand Up @@ -168,6 +168,14 @@ def user.to_path; "user"; end
end
end

test "require_dependency raises ArgumentError if the argument is not a String and does not respond to #to_path" do
assert_raises(ArgumentError) { require_dependency(Object.new) }
end

test "require_dependency raises LoadError if the given argument is not found" do
assert_raise(LoadError) { require_dependency("nonexistent_filename") }
end

test "eager loading loads the application code" do
$zeitwerk_integration_test_user = false
$zeitwerk_integration_test_post = false
Expand Down

0 comments on commit 6983a89

Please sign in to comment.