Skip to content

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

aprescott opened this Issue Jan 29, 2014 · 5 comments

4 participants


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]

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.


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
$ (
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 aprescott added a commit to aprescott/chruby that referenced this issue Jan 29, 2014
@aprescott aprescott Use chpwd instead of preexec.
Allows 'cd foo && cmd' to see .ruby-version, as well as in subshells for

Fixes postmodern/chruby#245.
@havenwood havenwood added the zsh label Sep 1, 2014
wadkar commented Mar 4, 2016

Any updates on this issue? Any workaround?

@wadkar wadkar added a commit to wadkar/powerlevel9k that referenced this issue Mar 4, 2016
@wadkar wadkar [WIP] Add two versions of chruby prompt
See postmodern/chruby#245 for chruby issue
with ZSH for automagic switching of ruby versions.

The second prompt always shows the system ruby version. Its annoying. I
think I am going to remove it.

Are there any downsides to using both preexec_functions and chpwd_functions?


@postmodern I'm not sure. That same issue came up at #246 (comment) in my (now very-outdated) PR for this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.