How dry-rails handles code reloading in Rails
mainIn development mode, dry-rails integrates with the Rails reloading mechanism via config.to_prepare. When Rails reloads code, Dry::Rails::Railtie#finalize! is called to ensure the dependency injection container and auto-injection constants are refreshed.
To prevent stale constants and memory leaks during reloading, the process follows these steps:
- Stop Features: It calls
stop_features, which triggers thestoplifecycle step for all currently started bootable components (features). This allows components to clear runtime constants they created. - Re-initialize Container: A new container is created using the Rails root path and default provider directories (e.g.,
config/system/boot). - Register Defaults: The
:envplugin is enabled by default (inferring fromRails.env), and therailtieandinflectorare registered. - Constant Management: Existing
ContainerandDeps(auto-inject) constants are removed from the application namespace to avoid name collisions or unused constants. - Reload: The new container and injector are set into the application namespace.