Getting ProviderNotFoundError when provider isn't directly in container.rb

Hi,

I’m trying to configure providers for some api clients that I have. I’ve been following the example provided in the documentation for dry-system, but I keep running into issues trying to get them to be in their own files.

I have a Container (app/system/container.rb) that looks like the following

require 'dry/system/container'

class Container < Dry::System::Container
  configure do |config|
    config.root = Pathname('.')
    config.component_dirs.add('app/services')
    config.component_dirs.add('app/repositories')
  end
end

If I try to put a provider in app/system/providers like in the example, when I’m in a ruby console and try to run Container.start(:provider) I get the following error:

Provider :provider not found (Dry::System::ProviderNotFoundError)

raise ProviderNotFoundError, provider_name unless provider

Stepping through this, it seems like the register_provider method is never called. Even if I place the provider file in the same directory as container.rb (app.system) it doesn’t work.

But if I put the provider directly in the container file like this:

require 'dry/system/container'

class Container < Dry::System::Container
  configure do |config|
    config.root = Pathname('.')
    config.component_dirs.add('app/services')
    config.component_dirs.add('app/repositories')
  end
end

Container.register_provider(:api_client) do
  start do
    client = API::Client.new
    register(:api_client, client)
  end
end

Then register_provider gets called and I’m able to inject the provider and access it using Container.start(:api_client)

Does anyone have an idea of what I’m doing wrong here?

This is solved, my Container needed to have it’s root directory set to ./app so that it would be able to find the providers directory.

1 Like