为什么 RubyMine 2018.2 检测不到我的宝石?

Why doesn't RubyMine 2018.2 detect my gems?

我正在尝试在 RubyMine v2018.2(2018.2.1、2018.2.4)中调试一个 Rails 项目。当我启动编辑器时,我收到警告:

RubyMine Gem Manager: RubyMine has detected that some of the gems required for 'project' are not installed.

当我尝试 运行 Ruby我的 Bundler 安装命令时,有人告诉我

bash -c "RBENV_VERSION=2.5.0 /usr/bin/rbenv exec bundle install -V"
Activating bundler (< 2) failed:
Could not find 'bundler' (< 2) among 28 total gem(s)
Checked in 'GEM_PATH=/home/brad/.gem/ruby/2.5.0:/var/lib/gems/2.5.0:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.5.0:/usr/share/rubygems-integration/2.5.0:/usr/share/rubygems-integration/all', execute `gem env` for more information

To install the version of bundler this project requires, run `gem install bundler -v '< 2'`

...当我尝试 运行 或从 IDE 调试我的应用程序时,我被告知 "Could not find bundler (version) required by your project-path/Gemfile.lock."

为什么我的 Ruby 检测不到我安装的 Gems?

我的环境

我正在使用 rbenv 来管理我的 Ruby 版本。系统安装了Ruby 2.5.1,但我的项目依赖2.5.0。我已将 2.5.0 设置为 rbenv 全局版本。 how rbenv chooses a Ruby version 的配置似乎没问题;我从非交互式命令中得到以下结果:

> echo $RBENV_VERSION
(no response)

> cat ~/.rbenv/version
2.5.0

> rbenv global
2.5.0

注意到 rbenv 在某些 RubyMine 命令中无法正常工作后,我将 eval "$(rbenv init -)" 设置步骤从 ~/.zshrc 移动到 ~/.zshenv~/.profile 所以它也适用于 bash 和 zsh 登录 shells(根据指南 here)。我现在得到了交互式和登录 shells 的匹配结果,但仍然从 RubyMine.

中的 Bundle install 命令得到了意外行为

什么有效

我的应用程序在交互式 shell.

中安装、构建和 运行 都很好

Ruby我的 Ruby SDK 设置正确检测 rvm 及其安装版本,以及该版本的所有已安装 gem。

我试过的

bundle install 命令的输出声称它是 运行ning

bash -c "RBENV_VERSION=2.5.0 /usr/bin/rbenv exec bundle install"

当我 运行 从我的交互式 shell 或 Ruby 我的 "Run anything" 功能(登录 shell)这个命令工作正常 - 一样以下任何一项:

RBENV_VERSION=2.5.0 /usr/bin/rbenv exec bundle install
/usr/bin/rbenv exec bundle install
rbenv exec bundle install
bundle install

所以我不知道 Ruby我的 运行 这个命令使用的是什么上下文。从错误消息中可以清楚地看出 GEM_PATH 不正确,导致 RubyMine 在错误的位置查找捆绑程序,但我不知道它从哪里获得此配置。如果我在 Ruby 我的中调出 "RubyGems Environment" 视图,我得到:

RUBYGEMS VERSION: 2.7.3
RUBY VERSION: 2.5.0 (2017-12-25 patchlevel 0) [x86_64-linux]
INSTALLATION DIRECTORY: /home/brad/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0
USER INSTALLATION DIRECTORY: /home/brad/.gem/ruby/2.5.0
RUBY EXECUTABLE: /home/brad/.rbenv/versions/2.5.0/bin/ruby
EXECUTABLE DIRECTORY: /home/brad/.rbenv/versions/2.5.0/bin
SPEC CACHE DIRECTORY: /home/brad/.gem/specs
SYSTEM CONFIGURATION DIRECTORY: /home/brad/.rbenv/versions/2.5.0/etc
RUBYGEMS PLATFORMS:
  ruby
  x86_64-linux
GEM PATHS:
   /home/brad/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0
   /home/brad/.gem/ruby/2.5.0
(...)
SHELL PATH:
   /home/brad/.rbenv/versions/2.5.0/bin
   /usr/lib/rbenv/libexec
   /home/brad/.rbenv/plugins/ruby-build/bin
   /home/brad/.rbenv/plugins/rbenv-gemset/bin
   /home/brad/.rbenv/plugins/rbenv-binstubs/bin
   (...rest of PATH...)
----------------------
IDE: RubyMine 2018.2.4, build #RM-182.4505.55
OS: Linux 4.15.0-38-generic[amd64]
Java: 1.8.0_152-release-1248-b8
RubyMine SDK Environment:
Sdk: rbenv: 2.5.0
Sdk Version: ver.2.5.0p0 ( revision 61468) p0
Ruby Interpreter: /home/brad/.rbenv/versions/2.5.0/bin/ruby
RVM Sdk: Rbenv sdk, gemset nullSdk Language Level: 2.5
Sdk Load Path:
     ~/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/did_you_mean-1.2.0/lib
     ~/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0
     ~/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/x86_64-linux
     ~/.rbenv/versions/2.5.0/lib/ruby/site_ruby
     ~/.rbenv/versions/2.5.0/lib/ruby/vendor_ruby/2.5.0
     ~/.rbenv/versions/2.5.0/lib/ruby/vendor_ruby/2.5.0/x86_64-linux
     ~/.rbenv/versions/2.5.0/lib/ruby/vendor_ruby
     ~/.rbenv/versions/2.5.0/lib/ruby/2.5.0
     ~/.rbenv/versions/2.5.0/lib/ruby/2.5.0/x86_64-linux
     ~/.local/share/JetBrains/Toolbox/apps/RubyMine/ch-0/182.4505.55/rubystubs25
Sdk Gem paths: 
     ~/.gem/ruby/2.5.0/gems
     ~/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/bundler/gems
     ~/.bundle/ruby/2.5.0/gems
     ~/.bundle/ruby/2.5.0/bundler/gems
     ~/.gem/ruby/2.5.0/bundler/gems
     ~/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems
(full list of gems used for project)

所以我认为最后一点 - "Sdk Gem paths" - 可能是问题点,但我没有看到任何配置它的方法。

我也已经知道 YouTrack 问题 "Rubymine 2018.2 release does not take $GEM_HOME environment variable" 并且不认为这里发生了什么。它还声称已在 2018.3 EAP1 中修复,所以我安装了 2018.3 EAP7 但我仍然看到这个问题。

你去过很多次了。

在您可以 运行 的脚本列表下,单击 "Edit Configurations"。应该在主工具栏中。

仔细检查您是否已 "Run the script in context of bundle (bundle exec)" 在捆绑器选项卡上选中。这是非常重要的,默认情况下不检查。确保检查任何耙子、rails 或 irb 配置。

请记住,运行 配置是您可以覆盖项目设置的地方。设置不当会出现各种问题

希望对您有所帮助!咬了我好几口

我开始工作了!通过退出并重新登录。:facepalm:

具体来说,我认为关键步骤是

  1. $(rbenv init -) 步骤从我的 .zshrc 移到我的 .zshenv and/or 到 .profile
  2. 注销并重新登录。

关键的见解是注意到 ~/.rbenv/shims 在 RubyMine Gem 环境中缺失,尽管它出现在交互式和登录 shell 中(在 RubyMine 内外),并且意识到虽然 new shell 获取了我的 .profile 更改,RubyMine 本身可能从我的会话中继承了它的环境变量,我需要登录 out/in 来获取更改.

我现在在我的 RubyMine Gem 环境中看到 ~/.rbenv/shims,Bundler Install 命令和我的 运行 配置再次工作。

谢谢 Casper 为我指明了正确的方向!