2

I am attempting to run bundle in a subfolder of my ruby project, but it appears to be running in the context of my initial directory even though I have changed the current working dir to the subfolder.

# change directories and run bundle in a sub directory:
# ruby script.rb
system('bundle')
system('cd sub_folder')
system('bundle')

The bundle command runs successfully but only for the parent folder. Changing directories via system commands does not properly switch the context for bundler, and runs for the parent folders gemfile twice. What am I missing?

random-forest-cat
  • 33,652
  • 11
  • 120
  • 99

2 Answers2

10

Just figured it out:

Dir.chdir('sub_folder') do
  Bundler.with_clean_env do 
    system('bundle')
  end
end

Shelling out - Any Ruby code that opens a subshell (like system, backticks, or %x{}) will automatically use the current Bundler environment. If you need to shell out to a Ruby command that is not part of your current bundle, use the with_clean_env method with a block. Any subshells created inside the block will be given the environment present before Bundler was activated. For example, Homebrew commands run Ruby, but don't work inside a bundle:

http://bundler.io/man/bundle-exec.1.html#ENVIRONMENT-MODIFICATIONS

random-forest-cat
  • 33,652
  • 11
  • 120
  • 99
1

You could try:

# ruby script.rb
Dir.chdir('sub_folder') do
  system('bundle')
end
TheMadHau5
  • 97
  • 1
  • 13