# Replace auto\_register! by component\_dirs.add

**URL:** <https://discourse.dry-rb.org/t/replace-auto-register-by-component-dirs-add/1432>\
**Category:** dry-system\
**Created:** [February 18, 2022, 3:37pm UTC](https://discourse.dry-rb.org/t/replace-auto-register-by-component-dirs-add/1432 "2022-02-18T15:37:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tyflomate](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/tyflomate/32/746_2.png) [@Tyflomate](https://discourse.dry-rb.org/u/Tyflomate)\
**Post date:** [February 18, 2022, 3:37pm UTC](https://discourse.dry-rb.org/t/replace-auto-register-by-component-dirs-add/1432/1 "2022-02-18T15:37:26Z")

</div>

Hello ! I’m trying to upgrade to the latest version of dry\_rails (which comes with latest version of dry\_system) and I’m having troubles with the update of `auto_register!` into `config.component_dirs.add 'smth'` . I was in 0.3.0 for dry\_rails and i’m trying to upgrade to 0.5.0. The register was like this:

```auto
Dry::Rails.container do
  ...
  auto_register!('app/event_sourcing')
  ...
end

```

and now:

```auto
Dry::Rails.container do
  ...
  config.component_dirs.add 'app/event_sourcing'
  ...

```

but it still breaks all my tests. Am i missing something ? Is there something else to change ?  
By the way, the add in component\_dirs return this:

```auto
config.component_dirs.add 'app/event_sourcing'
=> #<Dry::System::Config::ComponentDir:0x000055e15988ef88
 @config=
  #<Dry::Configurable::Config values={:auto_register=>true, :instance=>nil, :loader=>Dry::System::Loader, :memoize=>false, :namespaces=>#<Dry::System::Config::Namespaces:0x000055e15988e970 @namespaces={}>, :add_to_load_path=>true}>,
 @path="app/event_sourcing">

```

So it looks like it works but I don’t know what else to do.  
Thank you for your help!

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [February 18, 2022, 5:34pm UTC](https://discourse.dry-rb.org/t/replace-auto-register-by-component-dirs-add/1432/2 "2022-02-18T17:34:46Z")

</div>

Can you share a little more detail about what, specifically, is going wrong?

---

<div class="post-metadata">

**Author:** ![Tyflomate](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/tyflomate/32/746_2.png) [@Tyflomate](https://discourse.dry-rb.org/u/Tyflomate)\
**Post date:** [February 21, 2022, 8:34am UTC](https://discourse.dry-rb.org/t/replace-auto-register-by-component-dirs-add/1432/3 "2022-02-21T08:34:25Z")

</div>

Well, i’m using rails event store in my app, and every ReadEvents isn’t launching anymore. But I just saw that we use `register()` method to add them(?) into dry config. Maybe I also need to replace these methods by something else ?  
Example:  
`register(:event_store, RailsEventStore::Client.new)`

---

<div class="post-metadata">

**Author:** ![Tyflomate](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/tyflomate/32/746_2.png) [@Tyflomate](https://discourse.dry-rb.org/u/Tyflomate)\
**Post date:** [February 21, 2022, 10:50am UTC](https://discourse.dry-rb.org/t/replace-auto-register-by-component-dirs-add/1432/4 "2022-02-21T10:50:41Z")

</div>

I just investigated a bit more and the problem is that in this code in the start step of my provider:

```auto
start do
  Read::ApplicationProjection.descendants.each do |projection|
    event_store.subscribe(
      projection, to: projection.subscribed_events
    )
  end
end

```

The line `Read::ApplicationProjection.descendants` returns an empty array. I have the following tree:

```auto
> app
  > event_sourcing
    > read
      application_projection.rb
      > titles
        ...all descendants are here...

```

So maybe the `config.component_dirs.add 'app/event_sourcing'` doesn’t add the descendants properly? I am a litte lost here

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [February 23, 2022, 2:29am UTC](https://discourse.dry-rb.org/t/replace-auto-register-by-component-dirs-add/1432/5 "2022-02-23T02:29:11Z")

</div>

Is it possible that you switched from standard `require` loading to autoloading?

In my configuration, my container entries are lazy-instantiated using the [Zeitwerk](https://github.com/fxn/zeitwerk) plugin. This speeds up boot time in development and testing, but a side-effect of autoloading is that the constants for your code don’t exist until you reference them (or resolve the corresponding container key).

Take a look at `component_dirs["app/event_sourcing"].loader.ancestors`. In my case I can see that `Dry::System::Loader::Autoloading` is an ancestor of the component loader.

Another potential way to tell is that the zeitwerk plugin adds `config.autoloader`

`descendants` is problematic when used in combination with autoloading. You have to reference the constants first before they will appear in the list.

The Zeitwerk changelog suggests [iterating constants in the namespace](https://github.com/fxn/zeitwerk/blob/main/CHANGELOG.md#breaking-changes) to preload them in situations like this.

---

<div class="post-metadata">

**Author:** ![Tyflomate](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/tyflomate/32/746_2.png) [@Tyflomate](https://discourse.dry-rb.org/u/Tyflomate)\
**Post date:** [February 23, 2022, 9:09am UTC](https://discourse.dry-rb.org/t/replace-auto-register-by-component-dirs-add/1432/6 "2022-02-23T09:09:29Z")

</div>

Thanks for your answer but unfortunately no, I do not have autoloading 😕 this is the output of ancestors:

```auto
> config.component_dirs["app/event_sourcing"].loader.ancestors
=> [Dry::System::Loader,
 ActiveSupport::Dependencies::ZeitwerkIntegration::RequireDependency,
 ActiveSupport::ForkTracker::CoreExtPrivate,
 ActiveSupport::ForkTracker::CoreExt,
 ActiveSupport::ToJsonWithActiveSupportEncoder,
 Object,
 Minitest::Expectations,
 PP::ObjectMixin,
 ActiveSupport::Dependencies::Loadable,
 ActiveSupport::Tryable,
 JSON::Ext::Generator::GeneratorMethods::Object,
 Kernel,
 BasicObject]

```

and `config.autoloader` returns undefined method error.  
So the fix would be tu use Zeitwerk and configure the loader to preload every constants i need ?  
Thank you!

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [February 23, 2022, 6:14pm UTC](https://discourse.dry-rb.org/t/replace-auto-register-by-component-dirs-add/1432/7 "2022-02-23T18:14:46Z")

</div>

This may look a little different in dry-rails compared to me, since I am using dry-system without Rails.

Rails uses Zeitwerk autoloading for everything in `app/` so I believe that your missing descendants is happening because your provider’s start step is running before Zeitwerk is loading the classes you need.

You might consider moving those out of `app/` and into `lib/`, and requiring them in the `prepare` step to ensure that they exist in the object system. Downside to that approach is that you don’t get hot reloading with them anymore, but I’m uncertain if that works with your event subscriptions anyway.

---

<div class="post-metadata">

**Author:** ![Tyflomate](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/tyflomate/32/746_2.png) [@Tyflomate](https://discourse.dry-rb.org/u/Tyflomate)\
**Post date:** [February 24, 2022, 9:08am UTC](https://discourse.dry-rb.org/t/replace-auto-register-by-component-dirs-add/1432/8 "2022-02-24T09:08:07Z")

</div>

I should’ve added the dry-rails tag indeed, but this was working before the upgrade and I didn’t changed a thing expect from upgrading to new version so I can’t see another reason that the upgrade to cause empty descendants. Was there a loading of some sort in previous versions removed in the newest version ? I’ll try using zeitwerk in the container like you suggested maybe that’ll help
