Why are third party gems not loaded by default?

I added awesome_print to the Gemfile, because I want to use it while developing and testing.

group :development, :test do
  gem "dotenv"
  gem "awesome_print"

I would have thought that would be enough, but it is not. It seems that just Bundler.setup is called. Where should I put…

require "awesome_print"

Should I create a provider?
I’m using

* hanami (2.0.0.beta4)

Thanks and best regards!

I don’t think a provider is necessary, here. Try something like this:

In config/app.rb:

require "hanami"

Bundler.require(:default, Hanami.env)

module MyApp
  class App < Hanami::App
    # etc
1 Like

@wuarmin Gems aren’t required for fast boot time.

My suggestion is to require gems in the Ruby file where they are needed.
If you need a global gem, I would use config/initializers/awesome_print.rb, in which you can add a require "awesome_print", so it would be loaded at the boot time.

Going with Bundler.require(:default, Hanami.env) is :point_down: for me, as it will slow down your app boot, including each time you start a CLI command or when you reload your code in dev mode.

1 Like

Thanks @alassek and @jodosha for the answers. I’ve been following Hanami for years and I’ve also tried to keep up with the developments in Hanami 2. But now it’s the 1st time I can really try Hanami 2. Thank you for all the hard work. Hanami 2 is a massive milestone.

1 Like

I tried the solution using the initializers, but it seems, that the initializers are not loaded.

@wuarmin Thanks for reporting this. How are you loading the app? Via hanami server or hanami console?

@jodosha hanami server, but the initializers are also not loaded when I call hanami console.
Should I open an issue?

@wuarmin I suggested something that doesn’t work anymore in Hanami 2: initializers.
My apologize.

If you need to require a “global” gem, for now you should do it manually in config/app.rb

  require "awesome_print"
rescue LoadError
1 Like

All right! That’s why I couldn’t find anything about it in the code.
Thanks for the clarification.

@wuarmin For the record, this thread opened an internal discussion.
You may also want to check this PR.

Notice the PR description :wink: