ConradIrwin / pry-rescue

Start a pry session whenever something goes wrong.
MIT License
852 stars 49 forks source link

doesn't work with Rails 4.0 + RSpec #49

Closed odigity closed 10 years ago

odigity commented 11 years ago

Just discovered this gem. Followed the instructions:

1) added to Gemfile 2) ran bundle install 3) ran my specs with the "rescue" runner: $ rescue rake spec

But it never dropped me into Pry when one of my tests failed.

Robsteranium commented 10 years ago

I'm also experiencing this with:

Running a spec individually without the runner - e.g. with rescue spec/models/user_spec.rb - does drop me into a Pry session, indeed for the error LoadError: cannot load such file -- spec_helper! Running rescue spec/spec_helper.rb spec/models/user_spec.rb runs (my failing) rspec normally but doesn't drop me into a Pry session.

felixbuenemann commented 10 years ago

Same problem as described by @Robsteranium using following gems:

felixbuenemann commented 10 years ago

It seems to me that the @exception variable that pry-rescue requires is nil, so probably something in newer rspec versions broke this hack?

felixbuenemann commented 10 years ago

I looked at the rspec-core 2.14 code and it seems it wraps the Example in a proc using Procsy, so to get at the example's exception we have to access @exception in the binding of the proc: example.binding.eval('@example.exception'). This retuns the exception, but I'm now getting: WARNING: Tried to inspect exception outside of Pry::rescue{ } although I'm inside the Pry::rescue block.

NaN1488 commented 10 years ago

@felixbuenemann I've replicated the same change that you did in this line https://github.com/ConradIrwin/pry-rescue/blob/master/lib/pry-rescue/rspec.rb#L15 but no warning at all when running rescue rspec

could you provide an example of your test?

I have tested it with an easy example:

describe "Float" do
  it "should be able to add" do
    (0.1 + 0.2).should == 0.3
  end
end

btw, the line just before example.run https://github.com/ConradIrwin/pry-rescue/blob/master/lib/pry-rescue/rspec.rb#L13 example.binding.eval('@exception = nil') should be example.binding.eval("@example.set_exception(nil)") otherwise it would be setting an instance var in the Example::Procsy instead of the Example https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/example.rb

felixbuenemann commented 10 years ago

@NaN1488: I get the warning with your changes, even for a very barebones example Gemfile:

source 'https://rubygems.org'

gem 'rspec', '~> 2.14'
gem 'pry'
gem 'pry-rescue'

And using the same spec you wrote above:

rescue rspec
WARNING: Tried to inspect exception outside of Pry::rescue{ }

However, after switching from ruby-head to ruby-2.0.0-p247 in rvm the warning went away and pry-rescue started to work. So it seems pry-rescue doesn't work properly with the current ruby development version.

jkogara commented 10 years ago

Experiencing the same issue with ruby-2.0.0-p353 and the following gem versions

pry (0.9.12.4)
pry-debugger (0.2.2)
pry-plus (1.0.0)
pry-rescue (1.2.0)
pry-stack_explorer (0.4.9.1)
rspec (2.14.1)
rspec-core (2.14.7)

pry-rescue works for neither individual spec or blanket rescue rspec

@NaN1488 I tried your changes but it doesn't work

NaN1488 commented 10 years ago

My ruby version 1.9.3p484 @jkogara Did you test it with ruby-2.0.0-p247? just to be sure that the "problem" is the version ruby-2.0.0-p353

@felixbuenemann So changing the ruby version it worked... I'm smelling a hard bug to solve. lol What do you think @ConradIrwin ?

jkogara commented 10 years ago

@NaN1488 Just tested with p247 and it didn't work with that either, I think the "problem" is more likely with rspec version rather than ruby version

felixbuenemann commented 10 years ago

Strange, because I used the same Gemfile.lock when switching ruby, so I used the same rspec version with ruby 2.0. I'll need to investigate this further.

felixbuenemann commented 10 years ago

It's working fine for me without modifications on ruby 2.0.0-p352, but it fails on ruby 2.1.0 trunk 44253 because of this check:

https://github.com/ConradIrwin/pry-rescue/blob/master/lib/pry-rescue/core_ext.rb#L36

So it seems some of the changes to ruby 2.1.0 break pry-rescue's way of injecting the binding. Of the top of my head notable changes in 2.1 are nested exceptions and major changes to the garbage collector.

lvela commented 10 years ago

Having the same issue. Using: ruby 2.0.0-p247 rspec 2.14.7

Changed rspec.rb as @NaN1488 suggested like this and it worked:

 13           example.binding.eval('@example.set_exception(nil)')
 14           example.run
 15           if e = example.binding.eval('@example.exception')
 16             Pry::rescued(e)
 17           end

Not sure what the solution is since it seems to be working in other versions of rspec. This change would probably break the other versions where it's working.

lvela commented 10 years ago

Not really sure this worked like I thought. Had this issue when running "try-again".

From: /Users/lvela/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/example.rb @ line 215 RSpec::Core::Example#set_exception:

    208:       def set_exception(exception, context=nil)
    209:         if @exception && context != :dont_print
    210:           # An error has already been set; we don't want to override it,
    211:           # but we also don't want silence the error, so let's print it.
    212:           msg = <<-EOS
    213: 
    214: An error occurred #{context}
 => 215:   #{exception.class}: #{exception.message}
    216:   occurred at #{exception.backtrace.first}
    217: 
    218:           EOS
    219:           RSpec.configuration.reporter.message(msg)
    220:         end
    221: 
    222:         @exception ||= exception
    223:       end

NoMethodError: undefined method `message' for nil:NilClass
anlek commented 10 years ago

I'm having the same issue (pry not dropping me in on an exception). Using an earlier version of Ruby 2.0.0-p247 didn't work.

I've also tried rspec 2.13 and ruby 1.9.3 and neither of those fixed the issue.

miketheman commented 10 years ago

Failing to drop into rescue as well, Ruby 2.1.0, Rails 4.0.2, rspec 2.14.7

felixbuenemann commented 10 years ago

It's also failing on Travis CI, but only on ruby 2.1.0: https://travis-ci.org/ConradIrwin/pry-rescue/jobs/16421818

YayC commented 10 years ago

Same issue with:

ruby-2.0.0-p353
rails (~> 4.0.2)
rspec-rails (2.14.0)
pry (0.9.12.4)
pry-stack_explorer (0.4.9.1)
pry-rescue (1.2.0)
pry-remote (0.1.7)
pry-rails (0.3.2)
kyrylo commented 10 years ago

Hi, everyone! If anyone could contribute a project (it can be a dummy project) and provide an information on how to trigger the bug, we would be very thankful.

YayC commented 10 years ago

New rails project with a failing test example: https://github.com/YayConnolly/failing_pry_rescue_example

(to get it working just clone, create psql user with createuser -s -e failing_pry_rescue_example, rails db:create db:migrate db:test:prepare, rescue rspec)

phoet commented 10 years ago

another example for when it's not working can be found here: https://github.com/phoet/on_ruby/tree/try_pry_rescue

felixbuenemann commented 10 years ago

It's working for me, including try-again using the following code in self.run of rspec.rb:

13 example.binding.eval('@example.instance_variable_set(:@exception, nil)')
14 example.run
15 if e = example.binding.eval('@example.instance_variable_get(:@exception)')
16   Pry::rescued(e)
17 end

This is pretty similar to what @NaN1488 did, but I'm bypassing the setter and getter to to fix try-again.

@kyrylo Just use rspec 2.14 and you should be able to reproduce it easily. It's caused by internal changes in rspec.

phoet commented 10 years ago

@ConradIrwin i will try to use master and report back

ConradIrwin commented 10 years ago

@phoet you can try using release 1.4.1. Thanks to @felixbuenemann for the fix.

phoet commented 10 years ago

:+1:

bundle exec rescue rspec ./spec/models/user_spec.rb

  gem "pry-rails"
  gem 'pry-rescue', '1.4.1'
  gem 'pry-stack_explorer'

see those changes: https://github.com/phoet/on_ruby/commit/a9619d091d02da623fe6ad395f756329e38fffaf

also works with master.

felixbuenemann commented 10 years ago

@ConradIrwin Thanks for the new release, it's working fine!

neumachen commented 9 years ago

Anyone still looking into this? It seems with rspec 3.2.0 it does not work again.

felixbuenemann commented 9 years ago

@magicalbanana Please open a new issue with steps to reproduce the issue.

neumachen commented 9 years ago

@felixbuenemann I have. And I will look into that.

neumachen commented 9 years ago

@felixbuenemann #81 just added it. Strange really.