rails / spring

Rails application preloader
MIT License
2.8k stars 339 forks source link

spring rspec "uninitialized constant StatsD::Instrument::Matchers" when using statsd-instrument gem #440

Open russellsilva opened 8 years ago

russellsilva commented 8 years ago

I'm using the statsd-instrument gem rspec matchers.

$ bundle exec rspec # works fine
$ spring rspec
/Users/rsilva/work/project/spec/spec_helper.rb:149:in `block in <top (required)>': uninitialized constant StatsD::Instrument::Matchers (NameError)
        from /Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core.rb:97:in `configure'
        from /Users/rsilva/work/project/spec/spec_helper.rb:98:in `<top (required)>'
        from /Users/rsilva/work/project/spec/controllers/example_spec.rb:1:in `<top (required)>'
        from /Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
        from /Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
        from /Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
        from /Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
        from /Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
        from /Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
        from /Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
        from /Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
        from /Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
        from /Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from -e:1:in `<main>'

In spec_helper.rb:

RSpec.configure do |config|
  ...
  config.include StatsD::Instrument::Matchers
end
$ ruby -v
ruby 2.1.6p336 (2015-04-13 revision 50298) [x86_64-darwin14.0]
$ bundle show statsd-instrument
/Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/statsd-instrument-2.0.10
$ bundle show rails
/Users/rsilva/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rails-4.2.4

Incidentally, the Matchers module can't be found from rails console, even without using spring:

$ spring rails console
[1] pry(main)> require 'statsd-instrument'
=> false
[2] pry(main)> StatsD::Instrument::Matchers
NameError: uninitialized constant StatsD::Instrument::Matchers
from (pry):2:in `<main>'

$ bundle exec rails console
[1] pry(main)> require 'statsd-instrument'
=> false
[2] pry(main)> StatsD::Instrument::Matchers
NameError: uninitialized constant StatsD::Instrument::Matchers
from (pry):2:in `<main>'
[3] pry(main)> StatsD::Instrument
=> StatsD::Instrument

I'm really confused as to why it can't see StatsD::Instrument::Matchers. As noted, bundle exec rspec works fine. Any ideas?

kaspth commented 8 years ago

We reserve the issue tracker for bugs only, please the Rails talk mailing list or Stack Overflow. Thanks! 😁

reprah commented 8 years ago

@kaspth I understand, but why is this not considered a bug? There was a similar issue reported earlier that dealt with libraries in the test environment: https://github.com/rails/spring/issues/83

The general problem is the same, there are some constants that seem to stay unloaded when using Spring but everything works fine without Spring.

Any thoughts?

kaspth commented 8 years ago

Ah, I see. I just saw the external gem and thought that can't be right. Sorry about that :smile:

Do we have a more general issue reported we can pool resources around? It seems like the issues reported focus on the external gems, maybe there's more to gain by focusing on the general issue?

Thanks 😁

russellsilva commented 8 years ago

@ryansouza pointed out that the statsd/instrument/matchers file is conditionally required only if RSpec is already defined by statsd-instrument. This explains the rails console behavior in my original report.

Some sort of load order difference between spring and bundle exec?

I found a workaround: require 'statsd/instrument/matchers' in my spec_helper.rb.

kaspth commented 8 years ago

Interesting! I'll reopen but pass the torch, spring isn't really my domain.