postmodern / chruby

Changes the current Ruby
MIT License
2.85k stars 190 forks source link

chruby picking the wrong ruby on substring match #500

Closed Fryguy closed 5 months ago

Fryguy commented 5 months ago

Description

When 2 different rubies are installed where a shared substring is in both, chruby can pick the wrong one. For example, if I have ruby 3.0.6 and ruby 3.3.0 installed, and I say chruby 3.0, it will choose ruby 3.3.0, which is unexpected as I asked for the "3.0" series. I believe this is happening because the string "3.0" is actually present in both "3.0.6" and "3.3.0", and so the substring matches on both.

Steps To Reproduce

Steps to reproduce the bug:

  1. ruby-install ruby 3.0.6 and ruby-install ruby 3.3.0
  2. chruby 3.0
  3. $ echo $RUBY_VERSION
    3.3.0

I can similarly demonstrate this with an example that makes it more evident that the behavior is unexpected

  1. ruby-install ruby 3.0.6
  2. chruby 0.6
  3. $ echo $RUBY_VERSION
    3.0.6

My expectation there would be that chruby should fail to find anything because 0.6 is not a real version. You can also use similarly strange things like chruby .6 or even chruby 6 and both of those resolve to 3.0.6, which is unexpected

Expected Behavior

$ echo $RUBY_VERSION
3.0.6

Actual Behavior

$ echo $RUBY_VERSION
3.3.0

Environment

$ zsh --version
zsh 5.9 (x86_64-apple-darwin22.0)
$ chruby --version
chruby: 0.3.9
Fryguy commented 5 months ago

I do realize there is fuzzy matching involved here, but I feel like the algorithm is unexpectedly picking things. So, the algorithm picks the "last" one, but I wonder instead if it should pick the one where it matched most to the left of the string or something to that effect.

Fryguy commented 5 months ago

Story time - I think it's useful to know how this occurred for me.

Our application uses Ruby 3.0 at the moment, so in my .profile I do chruby 3.0, since that's the version I typically want in a new Terminal tab. I don't want to hardcode the full 3 digit version, because I generally update to the latest, and 3.0 "Just Works" as I install the latest patch versions.

Recently, I installed Ruby 3.3.0 to play with the new features. Existing Terminal tabs were fine, and I guess I didn't notice new tabs, however when my Terminal restarted all of the tabs were reopened but with Ruby 3.3.0. On top of that trying to switch back to 3.0 with chruby 3.0 didn't work! I thought I was stuck, but I was able to workaround it by doing chruby 3.0.6.

Fryguy commented 5 months ago

Gah - just noticed this is a duplicate of https://github.com/postmodern/chruby/issues/291 . Didn't see that when i was searching.