7

I got a new machine and installed rbenv fresh (I always used rvm before). This gem now won't load in my app. I made no code changes. The only difference between now and then is rbenv.

Loading development environment (Rails 5.2.1)
irb(main):001:0> SportsApi::Fetcher::Score::NBA
Traceback (most recent call last):
        1: from (irb):1
NameError (uninitialized constant SportsApi::Fetcher)

Interestingly, if I clone the gem to my local machine, then change the path in the Gemfile, it DOES load/work.

# gem 'sports_api', git: 'git@github.com:mikesilvis/sports_api.git'
gem 'sports_api', path: '~/dev/sports_api'


irb(main):001:0> SportsApi::Fetcher::Score::NBA
=> SportsApi::Fetcher::Score::NBA

Why does it work on local path but not on the git path? Any ideas on what I can do to fix this (to use the git path)? Thank you in advance.

Edit: Here is the full backtrace.

uninitialized constant SportsApi::Fetcher

/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:74:in `block in load_missing_constant'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in `without_bootsnap_cache'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:74:in `rescue in load_missing_constant'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:56:in `load_missing_constant'
(irb):2:in `irb_binding'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/workspace.rb:85:in `eval'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/workspace.rb:85:in `evaluate'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/context.rb:380:in `evaluate'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:491:in `block (2 levels) in eval_input'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:623:in `signal_status'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:488:in `block in eval_input'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/ruby-lex.rb:246:in `block (2 levels) in each_top_level_statement'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/ruby-lex.rb:232:in `loop'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/ruby-lex.rb:232:in `block in each_top_level_statement'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/ruby-lex.rb:231:in `catch'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb/ruby-lex.rb:231:in `each_top_level_statement'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:487:in `eval_input'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:428:in `block in run'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:427:in `catch'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:427:in `run'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/2.5.0/irb.rb:383:in `start'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/commands/console/console_command.rb:64:in `start'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/commands/console/console_command.rb:19:in `start'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/commands/console/console_command.rb:96:in `perform'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/command/base.rb:65:in `perform'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/command.rb:46:in `invoke'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/commands.rb:18:in `<main>'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
bin/rails:4:in `<main>'
iamse7en
  • 609
  • 8
  • 21
  • Did you run `bundle` beforehand to install that gem? And then, did you run `bundle exec rails c` (note - `bundle exec` part in the beginning)? – nattfodd Nov 12 '18 at 09:37
  • 1
    @nattfodd Yes, bundle and I tried both with and without bundle exec – iamse7en Nov 12 '18 at 16:29
  • Bundling `gem 'sports_api', git: 'https://github.com/mikesilvis/sports_api.git'` worked for me. However, the SportsAPI gem does not seem to return data for me. – whodini9 Nov 12 '18 at 20:44
  • @whodini9 yes, bundling always worked for me as well, but are you using rbenv or rvm? it no longer works under rbenv for some strange reason. – iamse7en Nov 13 '18 at 05:04
  • what do you get when you run `bundle show sports_api`? – trueinViso Nov 14 '18 at 18:39
  • I'm using rvm(rails 5.2.1), it works for me with `gem 'sports_api', git: 'git@github.com:mikesilvis/sports_api.git'` weird, have you tried with `rvm`? – unlimitedfocus Nov 15 '18 at 03:40
  • @trueinViso `/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/sports_api-b98e25a1acfc` – iamse7en Nov 15 '18 at 19:03
  • @unlimitedfocus Yes, I used rvm before and it worked. I wanted to switch to rbenv because it seemed better/more simplistic. The only thing that doesn't work is this gem. I do not get why this doesn't work in rbenv. – iamse7en Nov 15 '18 at 19:04
  • could it be something with this line in the sports_api gem? `/lib/sports_api.rb` I tried changing `Dir["#{File.dirname(__FILE__)}/sports_api/**/**/*.rb"].sort_by(&:length).reject { |file| file.match(/version/) }.each { |f| load(f) }` to `Dir["#{File.dirname(__FILE__)}/sports_api/**/**/*.rb"].sort_by(&:length).reject { |file| file.match(/version/) }.each { |f| require f }` but didn't make a difference. – iamse7en Nov 15 '18 at 22:23

4 Answers4

2

Updated:

There's a trip made by gem sports_api.

At the first line of lib/sports_api.rb: reject { |file| file.match(/version/) } the block would filter all the ruby files. Because of rbenv install ruby into ~/.rbenv/versions directory, with that the path of every gem files would come along with version string:

Each Ruby version is installed into its own directory under ~/.rbenv/versions.


Old answer:

Remove reject method at the first line of sports_api.rb:

Dir["#{File.dirname(__FILE__)}/sports_api/**/**/*.rb"].sort_by(&:length).reject { |file| file.match(/version/) }.each { |f| load(f) }

to:

Dir["#{File.dirname(__FILE__)}/sports_api/**/**/*.rb"].sort_by(&:length).each { |f| load(f) }

then you should delete cached gem files installed by bundler and run bundle install

Cong Chen
  • 2,436
  • 12
  • 21
  • This did it! I did not write this gem, and I'm not sure I quite understand why it's there. Are there any unintended consequences of removing this I should be aware of? – iamse7en Nov 16 '18 at 10:03
  • 1
    The `reject` method just filtered `sports_api/version.rb` – Cong Chen Nov 16 '18 at 10:10
  • @GentryChen that's a good catch. Do you know why this worked? The `version.rb` file was required explicitly in the gemspec so it looked to me like that line was fine. Maybe it has something to do with the way bundler loads gems and resolves versions? – Jay Dorsey Nov 16 '18 at 13:45
  • 2
    @JayDorsey I'll try my best to find out the root cause, then i will update my answer. – Cong Chen Nov 16 '18 at 14:22
1

Run rbenv init and follow instructions. Note, restarting terminal is require after installing rbenv (see https://github.com/rbenv/rbenv#installation for details).

Ensure gem installed in rbenv path by executing bundle show sports_api.

Tema Bolshakov
  • 1,128
  • 8
  • 14
  • Thank you, I already did `echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile` and `source ~/.bash_profile` when I installed rbenv. And Here is the `bundle show sports_api` - `/Users/myusername/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/sports_api-b98e25a1acfc`. Head scratching. It will only work when I change the source of the git to my local path. It works fine in rvm though. – iamse7en Nov 15 '18 at 19:19
  • could it be something with this line in the sports_api gem? `/lib/sports_api.rb` I tried changing `Dir["#{File.dirname(__FILE__)}/sports_api/**/**/*.rb"].sort_by(&:length).reject { |file| file.match(/version/) }.each { |f| load(f) }` to `Dir["#{File.dirname(__FILE__)}/sports_api/**/**/*.rb"].sort_by(&:length).reject { |file| file.match(/version/) }.each { |f| require f }` but didn't make a difference. – iamse7en Nov 15 '18 at 22:23
  • @iamse7en that line just automatically requires everything in all of the subfolders, except the version file (which was explicitly included in the gemspec or one of the other files). I don't actually see any difference between behavior in this gem vs others on my system (using `path:` it is automatically required, using `git:` I have to manually `require`) – Jay Dorsey Nov 16 '18 at 13:41
0

I see this same behavior with asdf (vs rbenv) so it's not an rbenv issue.

Adding a require 'sports_api' somewhere in your codebase before calling the code, while using the syntax you wish to use, will resolve the load issue.

I'm not 100% sure of the root cause, but I suspect it's may be a a combo of how path/git syntax is handled. Using the path syntax appears to automatically require the code for you (I wouldn't expect it to)

Jay Dorsey
  • 3,563
  • 2
  • 18
  • 24
  • 1
    I tried this. I've tried `require 'sports_api'` in rails console, as well as in one of the models (`game.rb`) I use this for api for. Still unable to load it. Always get `NameError (uninitialized constant SportsApi::Fetcher)`. Never had this problem with rvm. Really confusing. – iamse7en Nov 16 '18 at 02:03
  • @iamse7en Are you running `bundle exec irb` (or have a shell alias to run it) and `bundle exec rails console` for your commands? Also have you tried to run `rbenv rehash` and/or start a new terminal window? And disabling bootsnap gem temporarily to rule it out might help (it's in the stack trace). If your code is on github I'd be happy to take a look – Jay Dorsey Nov 16 '18 at 02:50
  • A couple other things you can do to troubleshoot: make sure that your bundler, rake, irb, etc. are the correct ones (e.g. your $PATH is setup to support rbenv per install instructions). Also, I'd try disabling spring with `spring stop` if it's running just to narrow down the problem. – Jay Dorsey Nov 16 '18 at 03:31
  • 1
    I'm running `bundle exec rails console`. I tried `rebenv rehash` and new terminal windows, restarting computer, etc. I tried disabling bootsnap as well. I've also tried this in rails 4.2 with no boostnap. Same exact thing. The code for `sports_api` is on github. Not the code for my app. But don't think the app is my problem. – iamse7en Nov 16 '18 at 07:24
0

I was getting the same issue with other gems and solved it by restarting spring:

bin/spring stop

spring helps to load environment faster but sometimes get issues with caching.

Stéphane Busso
  • 135
  • 2
  • 12