chruby icon indicating copy to clipboard operation
chruby copied to clipboard

.ruby-version not read with `cd foo && cmd` in zsh

Open aprescott opened this issue 11 years ago • 5 comments

While investigating #241, and possibly related to #160, I discovered this issue with zsh:

# zsh

cd /tmp
mkdir foo
echo '1.9.3' > foo/.ruby-version

ruby -v # ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]

# incorrect:
cd foo && ruby -v # ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]
ruby -v           # ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]

cd ..

# correct:
cd foo
ruby -v # ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]

Also happens with cd foo; cmd.

Contrast this to bash, where cd foo && ruby -v prints the same version as if they'd been entered completely separately:

# bash

cd foo && ruby -v && cd .. # ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]

cd foo
ruby -v # ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]

aprescott avatar Jan 29 '14 03:01 aprescott

It looks like using chpwd instead of preexec would fix this problem, but I don't know zsh enough to recognise any pitfalls with that approach.

aprescott avatar Jan 29 '14 03:01 aprescott

Actually that looks like it also fixes #241 and auto-switching would work for zsh subshells:

$ ruby -v
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]
$ chruby
   ruby-1.9.3-p448
   ruby-2.0.0-p247
   ruby-2.0.0-p353
$ (
subsh> cd foo
subsh> ruby -v
subsh> )
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]
$ ruby -v
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]

I can fix both issues in one PR by moving to chpwd if that's acceptable.

aprescott avatar Jan 29 '14 03:01 aprescott

Any updates on this issue? Any workaround?

wadkar avatar Mar 04 '16 18:03 wadkar

Are there any downsides to using both preexec_functions and chpwd_functions?

postmodern avatar Mar 08 '16 21:03 postmodern

@postmodern I'm not sure. That same issue came up at https://github.com/postmodern/chruby/pull/246#commitcomment-5218445 in my (now very-outdated) PR for this fix.

aprescott avatar Mar 08 '16 22:03 aprescott