如果不先安装 gem 本身,就无法在 Gem/Engine 项目中 运行 Rails

Can't run Rails in Gem/Engine project without first installing the gem itself

我刚刚检查了我的 Rails 引擎项目,当(在 bundle install 之后)我在其根目录中调用 rails -v 时,我得到了一个 LoadError 其中 Rails 似乎正在寻找我正在尝试构建的 gem/engine:

my-engine dmoles$ rails -v
/Users/dmoles/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/dependency.rb:315:in `to_specs': Could not find 'my-engine' (>= 0) among 117 total gem(s) (Gem::LoadError)
Checked in 'GEM_PATH=/Users/dmoles/.rvm/gems/ruby-2.2.1:/Users/dmoles/.rvm/gems/ruby-2.2.1@global', execute `gem env` for more information
    from /Users/dmoles/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/dependency.rb:324:in `to_spec'
    from /Users/dmoles/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_gem.rb:64:in `gem'
    from /Users/dmoles/.rvm/gems/ruby-2.2.1/bin/rails:22:in `<main>'

运行 gem env 如建议的那样没有提供更多启示:

my-engine dmoles$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 2.4.6
  - RUBY VERSION: 2.2.1 (2015-02-26 patchlevel 85) [x86_64-darwin14]
  - INSTALLATION DIRECTORY: /Users/dmoles/.rvm/gems/ruby-2.2.1
  - RUBY EXECUTABLE: /Users/dmoles/.rvm/rubies/ruby-2.2.1/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/dmoles/.rvm/gems/ruby-2.2.1/bin
  - SPEC CACHE DIRECTORY: /Users/dmoles/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/dmoles/.rvm/rubies/ruby-2.2.1/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-14
  - GEM PATHS:
     - /Users/dmoles/.rvm/gems/ruby-2.2.1
     - /Users/dmoles/.rvm/gems/ruby-2.2.1@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /Users/dmoles/.rvm/gems/ruby-2.2.1/bin
     - /Users/dmoles/.rvm/gems/ruby-2.2.1@global/bin
     - /Users/dmoles/.rvm/rubies/ruby-2.2.1/bin
     - /Users/dmoles/.rvm/bin
     - /Users/dmoles/bin
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /usr/local/git/bin

我可以 build/install gem

gem build my-engine.gemspec
gem install my-engine-0.0.1.gem

之后 rails -v 开始工作。* 不过,这似乎不是必需的,这让我担心 Rails 可能正在使用 built/installed 版本代码而不是实时源。我做错了什么?


* 也就是说,它运行,即使它抱怨 "Bundler is using a binstub that was created for a different gem"。可能是因为它不喜欢 gem 被命名为 my-engineENGINE_PATHmy/engine?

在引擎根目录中调用 rails -v 没有意义,因为引擎是 gem 毕竟需要在 rails 应用程序中加载,除非你使其结构像普通 rails 应用程序而不是引擎 gem.

所以基本上,当您 运行 rails -v 时,它会尝试将您的引擎作为 gem 而不是正常的 rails 应用程序加载,因此如果它未安装在您的环境中。

看起来这台机器上的本地 gem 设置不知何故被破坏了——即使我创建了一个全新的不相关的引擎,它仍然抱怨找不到 my-enginerm -r ~/.rvm/gems/ruby-2.2.1* / gem install rails 解决了这个问题(尽管代价是必须重新安装一堆 gem)。