Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to spawn jruby-1.7.3 subprocess within ruby-1.9.3 #358

Closed
jkndrkn opened this issue Mar 8, 2013 · 3 comments
Closed

Unable to spawn jruby-1.7.3 subprocess within ruby-1.9.3 #358

jkndrkn opened this issue Mar 8, 2013 · 3 comments

Comments

@jkndrkn
Copy link

jkndrkn commented Mar 8, 2013

Has anyone been able to spawn ruby subprocesses running different versions of ruby than the parent process?

I'm running rbenv 0.4.0-20-ga7da069 on Mac OS X 10.6.8.

I'm trying to see if the below scenario is feasible. I took over a project where the following was working reliably for some time but after upgrading rbenv, ruby-build, and updating bundler and gem, it broke.

# foo.rb
Dir.chdir("./jruby_project") do
  cmd = "bundle exec bin/bar.rb"
  IO.popen(cmd, "r").tap {|io| p io.readline}
end

If I call the above foo.rb in a project running ruby-1.9.3 using bundle exec foo.rb, then the above script fails to run ./jruby_project/bin/bar.rb using the version of jruby specified in ./jruby_project/.rbenv-version. Instead, it runs it using ruby-1.9.3.

Inspecting the ENV vars inside of bar.rb shows that RBENV_VERSION, PATH, and a number of other vars show that bar.rb is running using ruby-1.9.3 instead of jruby.

I have tried other ways of spawning a subprocess such as the backtick operator and these result in the same issue.

@mislav
Copy link
Member

mislav commented Mar 8, 2013

Without any editing of the PATH environment variable, you can't spawn different Ruby version subprocesses from a ruby process initiated with rbenv. See #121

@mislav mislav closed this as completed Mar 8, 2013
@jkndrkn
Copy link
Author

jkndrkn commented Mar 8, 2013

Thanks for the quick replies on this issue!

I found a fix that doesn't require editing of the PATH environment variable.

Dir.chdir("./jruby_project") do
  cmd = "rbenv exec bundle exec bin/bar.rb".split(" ")
  IO.popen([{"RBENV_VERSION" => "jruby-1.7.3"}, *cmd], "r").tap {|io| p io.readline}
end

Process.spawn, IO.popen, Open3.popen3, etc. all allow setting env vars. Using rbenv exec allows the subprocess to honor RBENV_VERSION and spawn with a different version than the parent ruby process initiated with rbenv.

@mislav
Copy link
Member

mislav commented Mar 8, 2013

Oh, nice workaround for your problem. But I don't want to advise this to everyone, because people shouldn't be using rbenv exec that often (it's a semi-internal command). Also, this makes your script unusable by a teammate who's using a different Ruby version manager than rbenv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants