1

In my mygem.gemspec I have the following gem that I only want for the purposes of debugging my ruby gem -

Gem::Specification.new do |s|
  ...
  s.add_development_dependency "pry"
  ...
end

Then in my Rails generator file I have require "pry", however I get the following error -

.rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require': cannot load such file -- pry (LoadError)

If I change add_development_dependency to add_dependency it works. Am I doing something wrong?

Edit:

lib/generators/mygem/install_generator.rb

require "pry"

module MyGem
  module Generators
    class InstallGenerator < Rails::Generators::Base

      namespace "mygem:install"

      def process
        binding.pry
      end

    end
  end
end

full stack trace

$ rails g mygem:install
Running via Spring preloader in process 99478
.rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require': cannot load such file -- pry (LoadError)
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
  from Projects/mygem/gem/mygem/lib/generators/cast/install_generator.rb:1:in `<top (required)>'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command/behavior.rb:82:in `block (2 levels) in lookup'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command/behavior.rb:78:in `each'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command/behavior.rb:78:in `block in lookup'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command/behavior.rb:77:in `each'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command/behavior.rb:77:in `lookup'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/generators.rb:250:in `find_by_namespace'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/generators.rb:267:in `invoke'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/commands/generate/generate_command.rb:24:in `perform'
  from .rvm/gems/ruby-2.3.3/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
  from .rvm/gems/ruby-2.3.3/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
  from .rvm/gems/ruby-2.3.3/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command/base.rb:63:in `perform'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/command.rb:44:in `invoke'
  from .rvm/gems/ruby-2.3.3/gems/railties-5.1.6/lib/rails/commands.rb:16:in `<top (required)>'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
  from Projects/mygem/themes/sample/bin/rails:9:in `<top (required)>'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `block in load'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
  from .rvm/gems/ruby-2.3.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'
  from .rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:59:in `require'
  from .rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:59:in `require'
  from -e:1:in `<main>'

Edit:

Here's a sample gem with the problem - https://github.com/ryangrush/stackoverflow_ruby_gem

Ryan Grush
  • 2,076
  • 3
  • 37
  • 64

2 Answers2

2

I think you misunderstand what "development_dependency" means: (This has nothing to do with your rails environment as most gems are not dependent on rails or environmental flags)

Taken From RubyGems Guide:

DECLARING DEPENDENCIES...

Runtime vs. development

RubyGems provides two main “types” of dependencies: runtime and development. Runtime dependencies are what your gem needs to work (such as rails needing activesupport).

Development dependencies are useful for when someone wants to make modifications to your gem. When you specify development dependencies, another developer can run gem install --dev your_gem and RubyGems will grab both sets of dependencies (runtime and development). Typical development dependencies include test frameworks and build systems.

Setting dependencies in your gemspec is easy. Just use add_runtime_dependency and add_development_dependency:

Gem::Specification.new do |s|
  s.name = "hola"
  s.version = "2.0.0"
  s.add_runtime_dependency "daemons", ["= 1.1.0"]
  s.add_development_dependency "bourne", [">= 0"]

So since your gem needs pry to work, by your design, then pry would be a runtime_dependency. Either that or you need to put some trapping around this code to determine

  • a) is Rails defined? and;
  • b) Are we in the development? environment? and;
  • c) Is Pry defined? or;
  • d) can we load pry (rescue from LoadError)
Community
  • 1
  • 1
engineersmnky
  • 25,495
  • 2
  • 36
  • 52
  • so even though I only need pry for debugging my gem and not the Rails app using it, I still have to load it as a runtime dependency? I found some SO answers that had it as an `add_development_dependency` (https://stackoverflow.com/a/14863281/772309) so I thought it was possible. – Ryan Grush Oct 17 '18 at 16:43
  • 1
    @RyanGrush you are requiring it in the generator which will run in the gem so yes. Your debugging should be done before placing the gem into a rails application. Usually this is done by writing tests to ensure the functionality. – engineersmnky Oct 17 '18 at 16:57
  • 2
    oh I see the difference now. I just tried loading pry with `add_development_dependency` and using it outside of a generator and it worked. Thanks for helping to clear that up. – Ryan Grush Oct 17 '18 at 17:01
1

Add this to your Gemfile in root of the project:

group :development, :test do
  gem 'pry'
end

And of course run

bundle install
lacostenycoder
  • 10,623
  • 4
  • 31
  • 48
  • I was hoping to do it from the gem side, instead of the project side. Again replacing replacing `add_development_dependency` with `add_dependency` in the .gemspec file works too. I don't know I'm out of ideas. I'll probably just use your solution or use add_dependency and move on even though its not ideal. Thanks for your help. – Ryan Grush Oct 16 '18 at 23:29
  • but your running a `rails generate` command and Rails needs to load gems from your project `Gemfile` so there shouldn't be a problem. And I doubt you want to require pry in a production environment. – lacostenycoder Oct 17 '18 at 05:12
  • 1
    you and @engineersmnky both helped clarify this, thanks. I think I use this solution when debugging my generators rather than loading it as a gem dependency. – Ryan Grush Oct 17 '18 at 17:05