错误:无法加载文件--rspec/core/rake_task

31
当我使用 rake 命令时,出现了以下错误:
AllenLins-MacBook-Pro:geoblacklight allenlin$ rake -t
Resolving dependencies...
You must `gem install bundler` and `bundle install` to run rake tasks
rake aborted!
LoadError: cannot load such file -- rspec/core/rake_task
/Users/allenlin/Documents/USpatial/geoblacklight/Rakefile:12:in `require'
/Users/allenlin/Documents/USpatial/geoblacklight/Rakefile:12:in `<top (required)>'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load_rakefile'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:689:in `raw_load_rakefile'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:94:in `block in load_rakefile'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:93:in `load_rakefile'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:77:in `block in run'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/Users/allenlin/.rbenv/versions/2.2.2/bin/rake:23:in `load'
/Users/allenlin/.rbenv/versions/2.2.2/bin/rake:23:in `<main>'

然而,我已经运行了'gem install bundler'和'bundle install'命令,实际上rspec/core/rake_task已经存在,如下所示:
AllenLins-MacBook-Pro:geoblacklight allenlin$ gem which rspec/core/rake_task
/Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rspec-core-3.0.4/lib/rspec/core/rake_task.rb

我在这里迷失了方向。非常感谢您对此的任何帮助!我附上了Gemfile:

source 'https://rubygems.org'

# Specify your gem's dependencies in geoblacklight.gemspec
gemspec

# If we don't specify 2.11.0 we'll end up with sprockets 2.12.0 in the main
# Gemfile.lock but since sass-rails gets generated (rails new) into the test app
# it'll want sprockets 2.11.0 and we'll have a conflict
gem 'sprockets', '2.11.0'

# If we don't specify 3.2.15 we'll end up with sass 3.3.2 in the main
# Gemfile.lock but since sass-rails gets generated (rails new) into the test app
# it'll want sass 3.2.0 and we'll have a conflict
gem 'sass', '~> 3.2.0'
gem 'bootstrap-sass', ">= 3.2"

group :test do
  # Peg simplecov to < 0.8 until this is resolved:
  # https://github.com/colszowka/simplecov/issues/281
  gem 'simplecov', '~> 0.7.1', require: false
  gem 'coveralls', require: false
end

更新

如果有帮助,请添加我的 gem 环境。

RubyGems Environment:
  - RUBYGEMS VERSION: 2.4.5
  - RUBY VERSION: 2.2.2 (2015-04-13 patchlevel 95) [x86_64-darwin14]
  - INSTALLATION DIRECTORY: /Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0
  - RUBY EXECUTABLE: /Users/allenlin/.rbenv/versions/2.2.2/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/allenlin/.rbenv/versions/2.2.2/bin
  - SPEC CACHE DIRECTORY: /Users/allenlin/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/allenlin/.rbenv/versions/2.2.2/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-14
  - GEM PATHS:
     - /Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0
     - /Users/allenlin/.gem/ruby/2.2.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /Users/allenlin/.rbenv/versions/2.2.2/bin
     - /usr/local/Cellar/rbenv/0.4.0/libexec
     - /Users/allenlin/.rbenv/shims
     - /Applications/Postgres.app/Contents/Versions/9.3/bin
     - /Library/Frameworks/Python.framework/Versions/2.7/bin
     - /Users/allenlin/Library/Enthought/Canopy_64bit/User/bin
     - /Users/allenlin/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/bundler-1.9.6/bin
     - /Users/allenlin/gradle-1.12/bin
     - /usr/local/bin
     - /Users/allenlin/apache-maven/apache-maven-3.1.1/bin
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /opt/X11/bin
     - /usr/local/git/bin
     - /usr/texbin

当你按照这个指令执行时,会发生什么情况呢:“(通过使用--trace选项运行任务以查看完整的跟踪信息)”? :) - Taryn East
另外,底部的整个file-expand-path部分。那不就是第二次加载相同的Gemfile吗?这可能就是为什么你会得到“两个来源”和其他两个东西的原因。 - Taryn East
@TarynEast 刚刚尝试删除了最后一段代码 - 你是对的,最后一段加载了Gemfile第二次,从而创建了警告消息。但是当我运行没有最后一段代码时,其他错误消息完全相同。 - Allen Lin
@pangpang 是的,这是Bundler 1.9.6版本。 - Allen Lin
让我们在聊天中继续这个讨论。点击此处进入聊天室 - pangpang
显示剩余7条评论
5个回答

20

3
-t参数只是增加了一个跟踪信息。 - nikkypx

15

之前已经运行过:sudo gem install bundler

我创建了一个包含以下内容的 Gemfile:

gem 'rake'
gem 'rspec'
gem 'kramdown'

然后我运行了bundle install。现在我不再遇到错误了。


2

在生产环境中

发生的情况是,capistrano 配置为仅在 production 组中安装 gem 包(如 Gemfile 中所示)。因此,它运行的 bundle 命令类似于 bundle install --without development test

想要验证吗?

在生产环境中项目目录下运行 bundle install 命令,您将看到以下内容:

Using devise 4.2.0
Using activeadmin 1.0.0.pre4
Using responsive_active_admin 0.0.5
Bundle complete! 60 Gemfile dependencies, 133 gems now installed.
Gems in the groups development and test were not installed.
Bundled gems are installed into /path/to/your/app/production/shared/bundle.

请注意 (在开发和测试环境中未安装组中的宝石(Gems)。)。

您可能记得已经指定了rspec-rails宝石只在development模式下安装。


解决方案

尝试像这样指定RAILS_ENV

# For Rails 5.0 +
RAILS_ENV=production bundle exec rails -T

# or 

RAILS_ENV=production bundle exec rake -T

# but this may not work
bundle exec rake -T RAILS_ENV=production

1
假设您的Gemfile中实际上有rspec,如果出现此错误,则意味着您没有使用应用程序所需的正确gems运行rake(请在应用程序的根目录中使用bundle exec rake解决),或者您的rspec核心gem未安装。
如果在运行bundle install后,在运行bundle exec rake时仍然出现相同的错误,请检查是否已安装了您的“测试模式”gems。 这种情况发生在我身上。
在我的情况下,我没有安装我的测试gems,因为Bundler通过记住传递给--without选项如此指出的先前参数而表现出非常规行为,因此您可能认为在运行bundle install时安装了所有gems,但实际上可能会运行(例如...)--without development test,并且不知道它。
在您的bundle install输出末尾检查类似于“未安装开发和测试组中的Gems。”的内容。
要重置您的包选项,只需删除./.bundle目录(即rm -r ./.bundle)。现在bundle install将恢复正常,rake也应该可以工作。

1
因为在我的 gem 文件中没有指定 rspec 的版本,所以出现了这个问题。它使用的是旧版本。 我将 gem 文件更新为以下内容: gem 'rspec', '3.8.0' 然后问题解决了!

1
果然gem install rspec解决了问题。 - rogerdpack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接