bundle exec - 你已经激活了 json X,但是你的 Gemfile 需要 json Y

bundle exec - You have already activated json X, but your Gemfile requires json Y

尝试在 Windows 上的 cygwin (x64) 上执行 jekyll 时,尽管使用了 bundle exec 前缀,但出现此错误。

Gemfile中定义的依赖(即1.8.6)执行时不是应该激活吗?

想知道这里出了什么问题吗?我对 Ruby gem 的世界完全陌生。我看过其他 "You have already activated .. " 显示类似消息的帖子,但那里的答案并没有解决我的问题。

$ bundle exec jekyll serve

$ bundle exec jekyll serve
/cygdrive/c/Users/mochapenguin/.rvm/gems/ruby-2.4.0/gems/bundler-1.14.6/lib/bundler/runtime.rb:40:in
 `block in setup': You have already activated json 2.0.2, but your Gemfile requires json 1.8.6. 
 Prepending `bundle exec` to your command may solve this. (Gem::LoadError)

环境

$ rvm -v
rvm 1.29.0 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io/]

$ ruby -v
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-cygwin]

Gemfile - 用于 jekyll 项目

source 'https://rubygems.org'

require 'json'
require 'open-uri'
versions = JSON.parse(open('https://pages.github.com/versions.json').read)

gem 'github-pages', group: :jekyll_plugins
gem 'json', '~> 1.8.0'

$gem环境

$ gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 2.6.10
  - RUBY VERSION: 2.4.0 (2016-12-24 patchlevel 0) [x86_64-cygwin]
  - INSTALLATION DIRECTORY: /cygdrive/c/Users/mochapenguin/.rvm/gems/ruby-2.4.0
  - USER INSTALLATION DIRECTORY: /cygdrive/c/Users/mochapenguin/.gem/ruby/2.4.0
  - RUBY EXECUTABLE: /cygdrive/c/Users/mochapenguin/.rvm/rubies/ruby-2.4.0/bin/ruby.exe
  - EXECUTABLE DIRECTORY: /cygdrive/c/Users/mochapenguin/.rvm/gems/ruby-2.4.0/bin
  - SPEC CACHE DIRECTORY: /cygdrive/c/Users/mochapenguin/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /cygdrive/c/Users/mochapenguin/.rvm/rubies/ruby-2.4.0/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-cygwin
  - GEM PATHS:
     - /cygdrive/c/Users/mochapenguin/.rvm/gems/ruby-2.4.0
     - /cygdrive/c/Users/mochapenguin/.rvm/gems/ruby-2.4.0@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /cygdrive/c/Users/mochapenguin/.rvm/gems/ruby-2.4.0/bin
     - /cygdrive/c/Users/mochapenguin/.rvm/gems/ruby-2.4.0@global/bin
     - /cygdrive/c/Users/mochapenguin/.rvm/rubies/ruby-2.4.0/bin
     - /usr/local/bin
     - /usr/bin
     - /cygdrive/c/Windows/system32
     - /cygdrive/c/Windows
     - /cygdrive/c/Windows/System32/Wbem
     - /cygdrive/c/Windows/System32/WindowsPowerShell/v1.0
     - /cygdrive/c/Program Files/Java/jdk1.8.0_121/bin
     ..... snip .....
     - /cygdrive/c/Users/mochapenguin/AppData/Local/atom/bin
     - /usr/lib/lapack
     - /cygdrive/c/Users/mochapenguin/.rvm/bin

捆绑更新

$ bundle update
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/..
Fetching dependency metadata from https://rubygems.org/.
Resolving dependencies...
Using i18n 0.8.1
Using json 1.8.6
Using minitest 5.10.1
Using thread_safe 0.3.6
Using public_suffix 2.0.5
Using coffee-script-source 1.12.2
Using execjs 2.7.0
Using colorator 1.1.0
Using ffi 1.9.18
Using multipart-post 2.0.0
Using forwardable-extended 2.6.0
Using gemoji 3.0.0
Using net-dns 0.8.0
Using sass 3.4.23
Using rb-fsevent 0.9.8
Using kramdown 1.13.2
Using liquid 3.0.6
Using mercenary 0.3.6
Using rouge 1.11.1
Using safe_yaml 1.0.4
Using mini_portile2 2.1.0
Using jekyll-paginate 1.1.0
Using jekyll-swiss 0.4.0
Using minima 2.0.0
Using unicode-display_width 1.1.3
Using bundler 1.14.6
Using tzinfo 1.2.3
Using addressable 2.5.0
Using coffee-script 2.4.1
Using ethon 0.10.1
Using rb-inotify 0.9.8
Using faraday 0.11.0
Using pathutil 0.14.0
Using jekyll-sass-converter 1.5.0
Using nokogiri 1.7.1
Using terminal-table 1.7.3
Using activesupport 4.2.7
Using jekyll-coffeescript 1.0.1
Using typhoeus 0.8.0
Using listen 3.0.6
Using sawyer 0.8.1
Using html-pipeline 2.5.0
Using jekyll-watch 1.5.0
Using octokit 4.6.2
Using jekyll 3.4.3
Using github-pages-health-check 1.3.3
Using jekyll-gist 1.4.0
Using jekyll-avatar 0.4.2
Using jekyll-default-layout 0.1.4
Using jekyll-feed 0.9.2
Using jekyll-github-metadata 2.3.1
Using jekyll-mentions 1.2.0
Using jekyll-optional-front-matter 0.1.2
Using jekyll-readme-index 0.1.0
Using jekyll-redirect-from 0.12.1
Using jekyll-relative-links 0.4.0
Using jekyll-seo-tag 2.1.0
Using jekyll-sitemap 1.0.0
Using jekyll-theme-architect 0.0.3
Using jekyll-theme-cayman 0.0.3
Using jekyll-theme-dinky 0.0.3
Using jekyll-theme-hacker 0.0.3
Using jekyll-theme-leap-day 0.0.3
Using jekyll-theme-merlot 0.0.3
Using jekyll-theme-midnight 0.0.3
Using jekyll-theme-minimal 0.0.3
Using jekyll-theme-modernist 0.0.3
Using jekyll-theme-primer 0.1.7
Using jekyll-theme-slate 0.0.3
Using jekyll-theme-tactile 0.0.3
Using jekyll-theme-time-machine 0.0.3
Using jekyll-titles-from-headings 0.1.5
Using jemoji 0.8.0
Using github-pages 132
Bundle updated!

bundle exec - 完整错误消息

$ bundle exec jekyll serve
/cygdrive/c/Users/mochapenguin/.rvm/gems/ruby-2.4.0/gems/bundler-1.14.6/lib/bundler/runtime.rb:40:in `block in setup': You have already activated json 2.0.2, but your Gemfile requires json 1.8.6. Prepending `bundle exec` to your command may solve this. (Gem::LoadError)
        from /cygdrive/c/Users/mochapenguin/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/forwardable.rb:228:in `each'
        from /cygdrive/c/Users/mochapenguin/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/forwardable.rb:228:in `each'
        from /cygdrive/c/Users/mochapenguin/.rvm/gems/ruby-2.4.0/gems/bundler-1.14.6/lib/bundler/runtime.rb:25:in `map'
        from /cygdrive/c/Users/mochapenguin/.rvm/gems/ruby-2.4.0/gems/bundler-1.14.6/lib/bundler/runtime.rb:25:in `setup'
        from /cygdrive/c/Users/mochapenguin/.rvm/gems/ruby-2.4.0/gems/bundler-1.14.6/lib/bundler.rb:100:in `setup'
        from /cygdrive/c/Users/mochapenguin/.rvm/gems/ruby-2.4.0/gems/bundler-1.14.6/lib/bundler/setup.rb:10:in `<top (required)>'
        from /cygdrive/c/Users/mochapenguin/.rvm/rubies/ruby-2.4.0/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /cygdrive/c/Users/mochapenguin/.rvm/rubies/ruby-2.4.0/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'

jsongems

$ gem list json

*** LOCAL GEMS ***
json (default: 2.0.2, 1.8.6)

捆绑展示json

$ bundle show json
/cygdrive/c/Users/mochapenguin/.rvm/gems/ruby-2.4.0/gems/json-1.8.6

无法卸载默认 gem

$ gem uninstall json -v2.0.2
ERROR:  While executing gem ... (Gem::InstallError)
    gem "json" cannot be uninstalled because it is a default gem

更新 - 2017 年 4 月 12 日

我不知道问题出在哪里,但我将 Gemfile 缩减到这个程度,它不会陷入困境。当不强制执行那些特定版本依赖性时,它可能会以某种方式起作用。

source 'https://rubygems.org'

# sample suggested [here](https://jekyllrb.com/docs/github-pages/) and in github help
gem 'github-pages', group: :jekyll_plugins

更新 - 2017 年 4 月 21 日

捆绑程序和 系统 gems 的这个问题已突出显示 at Gem Versioning and Bundler - Doing it right

Bundler's sandbox relies on its ability to be present at the very beginning of the Ruby process, and to therefore have the ability to ensure that the versions of all loaded libraries will reflect the ones listed in the Gemfile.lock. By running a system executable, you are executing Ruby code before Bundler can modify the load path and replace the normal Rubygems loading mechanism, allowing arbitrary unmanaged gems to get loaded into memory. Once that happens, all bets are off.

这就是在这种情况下似乎正在发生的事情。

但是,如上所述-无法卸载系统json 2.0.2 gem.

我昨天遇到了同样的问题。我不是专家,但以下内容对我有用:

我使用 Jekyll 创建了一个新网站:

$ jekyll new "name_site" 

然后我将新的 GemFile 和 GemFile.lock 从新文件夹复制到旧文件夹(您遇到问题的地方)。那么,你运行:

$ jekyll serve 

在您当前网站的文件夹中,它应该可以工作。让我知道它是否适合您!

比赛有点晚了,但是嘿,我有办法!我就是这样做的:所以除了查看安装 gems 的标准位置外,还有一个地方 gems 显然可以是 "defined",并带入系统.我使用 rbenv 安装东西(我推荐这个而不是 RVM),这些是我的目录:

/home/jf/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems
/home/jf/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/specifications/default

在您的情况下,您的确切 location/s 可能有所不同,但您应该能够弄清楚。如果 gem 位于标准位置 (/home/jf/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems),您将能够毫无问题地 'gem uninstall'。不过,对于另一个位置……很难。为了简单地防止系统版本被识别以便您可以使用 Gemfile 中的版本,只需将 json-X.gemspec 文件从第二个目录(上面的 /home/jf/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/specifications/default)移到其他地方。就是这样!