【问题标题】:bundle exec - You have already activated json X, but your Gemfile requires json Ybundle exec - 你已经激活了 json X,但是你的 Gemfile 需要 json Y
【发布时间】:2017-08-22 21:25:32
【问题描述】:

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

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

对这里出了什么问题有什么想法吗?我对 Ruby 宝石的世界完全陌生。我查看了其他“你已经激活..”帖子显示了类似的消息,但那里的答案并没有解决我的问题。

$ 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)

环境

  • Cygwin x86_64 安装在 Windows 7 上
  • RVM 安装在 Cygwin 上
$ 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'

json 宝石

$ 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 的沙箱依赖于它在 Ruby 进程的一开始就存在的能力,因此能够确保所有加载的库的版本都反映在 Gemfile.lock 中列出的版本。通过运行系统可执行文件,您可以在 Bundler 修改加载路径并替换正常的 Rubygems 加载机制之前执行 Ruby 代码,从而允许将任意非托管 gem 加载到内存中。 一旦发生这种情况,所有赌注都将取消。

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

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

【问题讨论】:

  • 你找到解决办法了吗?
  • 对不起,伙计。我从 4 月 12 日开始的更新是我得到的最好的。我没有进一步看,因为我只需要它来运行 jekyll 命令。我相信您也可以定义宝石集。在当前情况下,这可能是下一个最佳选择。 HTH。

标签: json rubygems bundler


【解决方案1】:

游戏有点晚了,但是,嘿,我有一个解决方案!我就是这样做的:所以除了查看安装 gem 的标准位置之外,还有一个地方可以明显地“定义”gem 并将其带入系统。我使用 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

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

【讨论】:

  • 哇,多么疯狂的修复……谢谢!我使用ruby-installchruby,对我来说,要移动/删除的文件位于~/.rubies/ruby-2.5.0/lib/ruby/gems/2.5.0/specifications/default/json-2.1-0.gemspec。这让我的包再次使用 json-1.8.6
  • 对迟到的回复表示歉意。我无法确认此建议是否适合我。我目前没有使用这项技术,也没有旧的环境设置来验证。但是,基于社区的另一位成员已确认此工作并且此响应已被投票的事实,我将接受此作为答案 - 除非稍后出现新的事实。感谢您的贡献!! @jeffrey-jf-lim
  • 您也可以尝试gem list -d json 找到每个路径
  • 对于 rvm,.rvm/rubies/ruby-2.4.5/lib/ruby/gems/2.4.0/specifications/default
【解决方案2】:

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

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

$ jekyll new "name_site" 

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

$ jekyll serve 

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

【讨论】:

  • 我相信 gems 需要使用 bundle exec 执行才能启动 bundler gem。它就像正在执行的主要 gem 的包装器(即在这种情况下为 jekyll)并应用定义的依赖项在 Gemfile 中。
猜你喜欢
  • 2011-09-13
  • 2015-09-19
  • 2015-10-24
  • 2011-11-11
  • 2011-11-06
  • 2015-11-21
  • 2011-11-26
  • 2017-08-29
  • 2011-09-07
相关资源
最近更新 更多