Skip to content

auto.fish changes to system ruby when no .ruby-version is present #462

@todd-a-jacobs

Description

@todd-a-jacobs

Whether or not one has a ~/.ruby-version to defined with a default, when I have the following in my ~/.config/fish/config.fish:

source /usr/local/share/chruby/chruby.fish
source /usr/local/share/chruby/auto.fish
chruby 3.0.2

then I correctly have 3.0.2 in my home directory, and get whatever version is specified in a project directory with a .ruby-version. However, if I change to a directory without a .ruby-version chruby_auto just sets me back to the system ruby. For example:

# new shell; expected behavior
~> chruby
   jruby-9.2.14.0
   ruby-2.7.2
   ruby-3.0.0
   ruby-3.0.1
 * ruby-3.0.2

~> printenv | egrep '(CH)?RUBY_'
RUBY_CONFIGURE_OPTS=--with-openssl-dir="/usr/local/opt/openssl@1.1"
CHRUBY_VERSION=0.3.9
CHRUBY_FISH_VERSION=0.8.2
RUBY_VERSION=3.0.2
RUBY_ROOT=/Users/tjacobs/.rubies/ruby-3.0.2
RUBY_AUTO_VERSION=3.0.2
RUBY_ENGINE=ruby
# directory without .ruby-version; unexpected behavior
~> cd /tmp

~> printenv | egrep '(CH)?RUBY_'
RUBY_CONFIGURE_OPTS=--with-openssl-dir="/usr/local/opt/openssl@1.1"
CHRUBY_FISH_VERSION=0.8.2
CHRUBY_VERSION=0.3.9
RUBY_AUTO_VERSION=

~> chruby
   jruby-9.2.14.0
   ruby-2.7.2
   ruby-3.0.0
   ruby-3.0.1
   ruby-3.0.2

If you try cd /tmp; and printenv | egrep '(CH)?RUBY_' it looks like it works, but it's transient success because fish appears to be reporting the environment variables before the Ruby change is completed. If you cd and then check, the Ruby has definitely been reset to the system Ruby.

From a user point of view, it looks like when auto.fish unsets RUBY_AUTO_VERSION, it loses track of whatever the default was supposed to be. When I set a default using chruby or a .ruby-version in my home directory, my expectation is that Ruby will be my default unless overridden by a project .ruby-version.

Looking at the source, auto.fish and chruby.fish use set -gx rather than set -lx for a lot of things, which may be part of the problem since -g affects all shells rather than just the current one. That aside, though, it seems that there's nothing that resets chruby to the initial value of chruby use or checks the ~/.ruby-version when leaving a project directory. This may be by design, but it feels like a bug.

A potential solution might be to have a set -Ux RUBY_AUTO_VERSION_DEFAULT (or maybe just a CHRUBY_DEFAULT_RUBY) set by the first use of chruby use or an encounter with ~/.ruby-version, and then fall back to that when needed. Alternatively, if the user has a .ruby-version in the home directory, then the following makes more sense to me (and is probably less complicated). For example:

set -q RUBY_AUTO_VERSION; or set -lx RUBY_AUTO_VERSION=(cat ~/.ruby-version)

There may be problems with these particular approaches that I haven't thought of; I'm simply offering them as possible solutions if there isn't a better one. The issue may not even be fish-specific (it seems to exhibit the same behavior with Bash on my system, too), but I still find the behavior surprising.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions