Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run `bundle` system command in subfolder

Tags:

ruby

bundler

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?

like image 405
lfender6445 Avatar asked Sep 22 '26 12:09

lfender6445


2 Answers

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

like image 159
lfender6445 Avatar answered Sep 24 '26 07:09

lfender6445


You could try:

# ruby script.rb
Dir.chdir('sub_folder') do
  system('bundle')
end
like image 27
TheMadHau5 Avatar answered Sep 24 '26 05:09

TheMadHau5