How do I change gem environment? - ruby-on-rails

I'm working on a distributed rails app and want to update a gem. The problem is on one server it won't update because the gem environment is different. Specifically, I get two different results when running gem env or bundle exec gem env in rails root. The latter determines the app's gem environment and it is wrong but I don't know how to change it. I don't see a .gemrc file anywhere on the server and the .bashrc files I checked, and they don't export any of the gem environment variables. It uses rvm for ruby. For example, the GEM PATH I would like to change so I grep'd through the app and rvm to see if that path was in a file somewhere and it isn't. I'm not sure where the environment is getting set, then. Any suggestions where to look or how to troubleshoot?

looking at the man page for bundle, I discovered 'bundle config'. It gave me the info I needed to find the problem. It told me it was getting some settings from /home/rails/.bundle/config . After examining that file, I found it was setting the BUNDLE_PATH to the incorrect setting. Moving that file aside allowed me to update the gem I wanted to. I guess the moral of the story is that 'bundle config' (more specifically, 'bundle exec bundle config') is your friend, and check your system for the settings in .bundle/config files if 'gem env' is giving unexpected results.

Related

Rails Bundle Options for Getting Gems back into your Local Application

I ran bundle --without development on my machine and now I want to include the dependencies in my development group in my local rails app.
I ran bundle thinking it would including everything again including development, but I receive this message Gems in the groups development were not installed still. Can you tell me the option I need to pass into the bundle command to get the development gems back into my application? I tried running bundle --include development with no luck.
Can you also tell me where you found the option (if thats the solution)? I can't seem to locate a list of bundle options.
If you run
bundle help install
then both the with and without options are documented. Note that without is listed as being a "remembered option" i.e. it is persisted for subsequent calls.
Remembered options are stored in .bundle/config at the root of the project. You can either edit this file directly or view them using
bundle config
Which will also take into account user level configuration or environment variables that affect bundler
I was surprised to confirm this behavior. It seems like the without --development flag is saved, so even if you rm Gemfile.lock, running bundle again will not install the development group. bundle update also doesn't work.
You were close, though, with bundle --include development.
What works is bundle --with development. You don't need to delete your Gemfile.lock before doing this.

Bundler is using a binstub that was created for a different gem.

When I run a rails console using
rails console
everything is fine.
When I run a rails console using
bundle exec rails console
I get the following warning
Bundler is using a binstub that was created for a different gem.
This is deprecated, in future versions you may need to `bundle binstub my_gem` to work around a system/bundle conflict.
my_gem happens to be a gem that I've created that is completely unrelated and not used in the current project directory.
I've tried every solution in this question with no luck:
Bundler is using a binstub that was created for a different gem
I would appreciate any guidance on removing this warning or help understanding how binstubs work so that I can figure out what's going on.
Nowadays it's common for projects to have "specialized" versions of tools. E.g. in some projects the "rails" command may be expected to be run using "spring" (to start up faster).
So it's not uncommon to generate files in your project's 'bin' directory, and then use those versions when running commands, so e.g. instead of
bundle exec rails console
or
bundle exec spring rails console
you could simply expect the following to work correctly
bin/rails console
and not care whether the project needs spring or bundler or zeus or whatever.
So if you don't have 'bin/rails' in your project, you should generate one that suits the project, e.g. using
bin/rake rails:update:bin
If you don't already have bin/rake, you might have to use
bundle exec rake rails:update:bin
(so your bin/rake commands will also get a speedup from using spring)
Some people even put ./bin in their paths, so whenever they run rake (or whatever) they are actually running ./bin/rake if it exists.
Troubleshooting
for project specific tasks, use bin/* files, creating them if needed (e.g. using special rake tasks like in Rails or using bundle binstub <gemname>) - usually those have Bundler specific lines that will make Bundler happy.
for non-project gems (like your gem), find out where it is (e.g. which mygem) and check out it's contents - it's probably using e.g. "bundler/setup" which is confusing Bundler (because bundler expects a local Gemfile file). Maybe your gem is using bundler (it shouldn't if it's a "global" kind of tool and not a "project" tool).
Also, if you're using them, check if tools like RVM and .rbenv are correctly adding their stuff to your bin files (they usually need to setup specific paths)
If you still have questions, it's best to post the contents of the bin file causing problems - it's meant to be a plain Ruby file, so if there's something wrong, it's usually because of the file contents (and not anything else).
More info: https://github.com/sstephenson/rbenv/wiki/Understanding-binstubs
It happened in a project of mine. Because I ran bundle install with another ruby version.
Make sure your rvm is the correctly ruby version.

rbenv and gemsets mismatch (?) causing Cucumber failures

Last week, my Cucumber tests started failing. All of them. Here is the error:
undefined method `split_stylesheet_link_tag' for #<#<Class:0x007ff9a433c390>:0x007ff9a6a848f8> (ActionView::Template::Error)
split_stylesheet_link_tag is a method I have from the css_splitter gem. It's found in my application.html.haml file. (Hence why all the tests are failing; they all use that main template.) Loading the view that the Cucumber test is trying to load works in development. In other words, this is only an issue in my test environment.
Here's where it gets weird. I was using RVM, and I had concluded it was an issue with my gemsets, because it's the gem that exposes that method. So I switched to rbenv, and ran the tests. They worked.
I'm not sure what I've changed in the meantime (I know, I know), but I'm getting that failure again. If you're interested in the backtrace, you can find it here.
What I've Tried
Googling around caused me to check the contents of .bundle/config. It contains
BUNDLE_DISABLE_SHARED_GEMS: '1'
That sounded like something that could be a culprit, so I changed that to 0 and ran bundle install. It changed it back to 1.
One interesting thing is that when I run bundle install in the root of this Rails app, I get this message:
Your bundle is complete!
It was installed into ./vendor/bundle
This didn't seem right.
I'm using rbenv-gemset. When I ran cat .rbenv-gemsets, I got global. So I followed the docs for rbenv-gemsets and changed global to .gems. The docs suggested that this would install the gems into the .gems directory in my project root. Running bundle install again gave me the same message about installing the gems into ./vendor/bundle, and there isn't a .gems directory in the root of my project. (I had assumed that adding .gems would create one.)
I'm using tmux, and wondering if that had something to do with it because of this issue, I tried running the tests outside of tmux and got the same error.
I've also made sure that my .bash_profile file contains eval "$(rbenv init -)" and that I've gone back through the rbenv setup. I've confirmed that both rbenv and its shims directory are on my path.
I made sure that css_splitter is in my Gemfile and is not nested under a group.
I'm sort of grasping at straws here. Just throw out any ideas.
This was apparently an issue with my bundler config. Gems were apparently being stored in the wrong directory. Based on this answer, I ran rm -rf ~/.bundle/ ~/.gem/ .bundle/ Gemfile.lock. Then I ran bundle install.
It's worth noting here that this also fixed the issue I was having with rbenv-gemset. That is, deleting those directories caused bundle install to install the gems into the .gems directory in this project as expected. That is, I'm guessing, why my test environment had access to the gems as expected.

RhoStudio build for iOS7 Simulator: "Gem rake not installed" error

I am trying to build a RhoStudio application for the iPhone-simulator on Mac OS X 10.9.1.
The build with XCode fails at
/bin/sh -c ~/.rvm/gems/ruby-1.9.3-p545/gems/rhodes-4.0.1/platform/iphone/build/rhorunner.build/Release-iphonesimulator/rhorunner.build/Script-5C0442920EFBE79D0014E5C6.sh
[31mERROR: Gem rake is not installed, run `gem install rake` first. (B [m
Command /bin/sh failed with exit code 127
Within the script (Script-5C0442920EFBE79D0014E5C6.sh), there is a "source ~/.profile" call which seems to mess up the environment for rake. The error can be reproduced by opening a shell and executing the source command. If i don't source, there is no error for rake.
Also, if i try to gem install rake after the source command, there is a confirmation that it has been installed but the error still occurs.
Anybody help?
This is one of those issues where there are way too many specific factors involved to easily come up with a solution; I'm going through the ropes myself with Rhodes in 10.9. The problem is because Rhodes constantly is changing environments (it's a real mess) so your environment, which tracks the paths to your Ruby installations and Gems, will be very volatile. Here are some tips that might help you diagnose the issue:
Try running gem which rake, which shows you the absolute path of your rake gem as visible to the calling script. You can place this in one of your config files like ~/.profile or ~/.bashrc to test different contexts.
You can also use gem env to see a full printout of the gem's configuration for your environment, and just plain env shows you the system (shell) environment. Look very closely for the variables GEM_PATH AND GEM_ROOT which show you where Rubygems and your gem libs are searched for, respectively.
Keep in mind using sudo before install will affect where your gems are installed; this depends on where you installed Ruby, which ruby manager (for RVM look in ~/.rvm) and whether or not you are using Bundler for instance.
Hope that helps. Also, if you are into Bash scripting I recently posted a tip for managing the environment paths that might help: https://coderwall.com/p/f_dlyg

How to use multiple bundle paths in Ruby on Rails (bundler)

I'm using a few gems for interactivity with my database and to provide easy pagination. After deploying my application on my server, it'll automatically execute bundle install to install those gems if they aren't installed yet. This is helpful when I deploy a codebase update including a new gem.
However, there's one gem (will_paginate) I've made a little change to (so that it always shows on what page you are, no matter the total number of pages).
Therefore, I used bundle install --local --path vendor at my local machine (OS X Lion) so that I can easily edit them (at per-app base, instead of system-wide).
Because of dependency problems, I cannot 'just' copy the vendor-folder to my web server. Therefore, I decided to add another rule to my .gitignore-file:
vendor
Because this caused my customized will_paginate-gem not to get uploaded, I executed another command:
git add -f vendor/ruby/1.8/gems/will_paginate
Now, when I push, the only gem in the vendor folder at my web server is will_paginate, which is great.
However, it doesn't get loaded, since it isn't in the bundle path.
I already tried to create a file called .bundle/config and add the following in it:
---
BUNDLE_PATH: vendor
I also tried adding BUNDLE_DISABLE_SHARED_GEMS: "0", but using this, bundle check says it's missing the shared gems.
My question is, how can I add a bundle path vendor, and fallback on the system-wide gem-path.

Resources