Gem 未加载:NameError(未初始化常量)|但是 gem 在本地路径中工作

Gem not loading: NameError (uninitialized constant) | But gem works in local path

我买了一台新机器并全新安装了 rbenv(我之前一直使用 rvm)。 This gem 现在不会在我的应用程序中加载。我没有做任何代码更改。现在和那时的唯一区别是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)

有趣的是,如果我将 gem 克隆到我的本地计算机,然后更改 Gemfile 中的路径,它会 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

为什么它在本地路径上有效,但在 git 路径上无效?关于我可以做些什么来解决这个问题(使用 git 路径)有什么想法吗?提前谢谢你。

编辑:这是完整的回溯。

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>'

运行 rbenv init 并按照说明进行操作。注意,安装 rbenv 后需要重新启动终端(详见 https://github.com/rbenv/rbenv#installation)。

通过执行 bundle show sports_api 确保 gem 安装在 rbenv 路径中。

我在 asdf(与 rbenv)中看到了同样的行为,所以这不是 rbenv 问题。

在调用代码之前在代码库中的某处添加 require 'sports_api',同时使用您希望使用的语法,将解决加载问题。

我不是 100% 确定根本原因,但我怀疑这可能是 path/git 语法处理方式的组合。使用路径语法似乎自动需要你的代码(我不希望它)

更新:

gemsports_api进行了一次旅行。

lib/sports_api.rb的第一行: reject { |file| file.match(/version/) } 该块将过滤所有 ruby 文件。因为 rbenv 将 ruby 安装到 ~/.rbenv/versions 目录中,每个 gem 文件的路径将与 version 字符串一起出现:

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


旧答案:

删除sports_api.rb第一行的reject方法:

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

至:

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

那么你应该删除缓存的 gem 由 bundler 和 运行 bundle install

安装的文件

我在使用其他 gem 时遇到了同样的问题,并通过重新启动 spring 解决了它:

bin/spring stop

spring 有助于更快地加载环境,但有时会遇到缓存问题。