Using RVM Gemsets & Bundler & RubyMine

2020-06-18 09:12发布

问题:

I use RVM to manage Ruby versions.
In my project I use Bundler to manage gems for the project.

RVM also have gemsets.
Gem in gemset don't have a connection with Bundler's gem. ← Is this correct?
I came to this conclusion because gem files stored in different locations:
RVM gemset: ~/.rvm/gems/ruby-2.0.0-p247@myApp
Bundler: [my_app_dir]/vendor/bundle/gems
So app uses Bundler gems, not RVM gemset gems.

But when I add gem to my Gemfile, RubyMine IDE shows me warning, that this gem is not in RVM gemset. So I add this gem to RVM gemset also (just to get rid of this warning).

So the questions are:

  1. Is there any good reason to add gems in both places (RVM Gemset and Gemfile)?
  2. If no, then why RubyMine warning me about this?

回答1:

  1. Is there any good reason to add gems in both places (RVM Gemset and Gemfile)?

The gemset is incidental, the Gemfile is absolutely the place to declare your dependencies. Where you store those gems is up to you.

It sounds like Bundler is configured to store them in a project-local path, but you're expecting them to be in a gemset. Bundler got that configuration by running bundle install --path vendor/bundle/gems at some point. It stores that configuration in its project configuration file at project_dir/.bundle/config:

BUNDLE_PATH: vendor/bundle/gems

I'm unfamiliar with Rubymine, but if you run the Rails server using Bundler (i.e. bundle exec rails server) you can ignore that warning. Bundler will correctly load the gems listed in the Gemfile.

If you want to use a gemset instead of the Bundler cache, you can just remove that line from the Bundler configuration file and reinstall your gems with bundle install.

  1. If no, then why RubyMine warning me about this?

My guess is that Rubymine is not reading the Bundler project configuration (in project_path/.bundle/config) and does not understand where the gems are installed.



回答2:

You (or if you are working in a team, somebody of your team) has once done a bundle install and specified a installation-folder. In your case vendor/bundle/gems. Bundle remembers this setting and all next invocations of the bundle command will use the same path.

There is a good reason to do it that way: your application-folder will contain all requirements and will be easier to redistribute (for instance).

Now if you want that bundle installs your gems in the normal locations, you can do the following:

  • run bundle install --system which will use the default location
  • alternatively: bundle stores it settings in a config file, I think .bundle/config and you can check that one as well. Normally it is not needed, since bundle install --system will set that correctly again.
  • then you can safely remove the vendor/bundle/gems folder


回答3:

No, something's wrong, you shouldn't have anything under vendor/bundle, it should all be under ~/.rvm/gems/ruby-2.0.0-p247@myApp and perhaps ~/.rvm/gems/ruby-2.0.0-p247@global assuming your .rvmrc (or.ruby-version) is setup correctly.

What does "gem env" look like? Also "bundle env"?



回答4:

So this just took me 3 days, since nothing else I was finding here was helping. I also run multiple projects through RubyMine at the same time (and different versions) so setting my GEM_PATH and launching from command-line doesn't work for me. I use IntelliJ with RM plugin, this should work on RM standalone.

Bundler seems to install custom gems, or gems from custom repos, in a different directory than gems from rubygems, or github.

/Users/YOURUSER/.rvm/environments/ruby-{version}\@yourgemset/bundler/gems

One thing I wasn't able to fix is in the GEMFILE, I have some custom git_sources, and rubymine highlights those and gives me the warning that it cannot find the gem in my bundle (you can ignore this warning; unless the gem doesn't install at all):

gem 'somegem', custom_git:'gituser/repo'

is highlighted and warning is "Gem x cannot be found... in SDK'

However Bundler installed it, and ruby is able to load it.

# TLDR: Steps to have RUBYMine find extra gems, and show up in external libs

  1. vim ~/.rvm/environments/ruby-{your-verion-here}\@{your-gemset}
  2. add the bundler gems path to GEM_PATH entry

    export GEM_PATH='/Users/YOURUSER/.rvm/gems/ruby-{version}@yourgemset/bundler/gems:{the rest}'

  3. save the file
  4. Restart RubyMine/IntelliJ, reopen your project (if not open automatically)
  5. Open the Project Structure dialog > Platform SDKs > Choose the GEMSET you're working with
  6. add /Users/YOURUSER/.rvm/environments/ruby-{version}\@yourgemset/bundler/gems to your classpath
  7. Hit OK, then REOPEN the Project Structure Dialog > Project Settings > Project
  8. Your project will likely have no SDK So select the one you're using again and hit OKAY
  9. RM/IJ will now reindex files
  10. You're done, any broken/missing inspection links should now be fixed. And you should be able to introspect into your gems.

In the above instructions that when you run bundle install (from terminal or RM) it works successfully, and that you have RVM correctly setup, and gemset already created


I hope this helps! Let me know if I should clarify anything (happy NYE)