ruby / ruby/rubygems

`gem uninstall somegem` when multiple gem are installed: exit codes & ctrl+d handling when no selection given

Open
#6,028 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

RubyGems
Dominant language
Ruby
Stars
4k
Forks
1.9k
Avg merge
1d 2h
Merged PRs (30d)
81

Description

Following with the issues posted on the 2 issues I reported on ruby-lang Rubymine,
here and here, one related to handling ctrl+d on gem uninstall prompt, the other related to the exit return code on gem uninstall error:

Descriptions:

Scenario:
Calling gem uninstall some_gem when there are more than 1 version of that specific gem installed.
A list of the installed versions is displayed and a prompt is waiting user input for the option number of the item in the list to proceed with the uninstall.
When using ctrl+d key combination on this input prompt (I'm used to use ctrl+c/ctrl+d to interrupt CLI commands) an exception is raised :

gem uninstall activesupport 

Select gem to uninstall:
 1. activesupport-6.0.6
 2. activesupport-7.0.4
 3. All versions
> ERROR:  While executing gem ... (NoMethodError)
    undefined method `>=' for nil:NilClass

      elsif index >= 0 && index < list.size
                  ^^

Ctrl+c is handled correctly but for ctrl+d it looks like nil is returned from the #choose_from_list method in rubygems/user_interaction.rb and then in the Gem::Uninstaller class the method #uninstall (rubygems/uninstaller.rb) that nil return value is not handled correctly:

      _, index = choose_from_list "Select gem to uninstall:", gem_names

      if index == list.size
        remove_all list
      elsif index >= 0 && index < list.size
        uninstall_gem list[index]
      else
        say "Error: must enter a number [1-#{list.size + 1}]"
      end

I would expect the command to exit silently or maybe exit with the existing "Error: must enter a number" message but not with this unfriendly nil error message.
Maybe this scenario could be handled more gracefully by the tool?


When there are several versions of a gem to uninstall and there is a prompt with all the installed versions and it is waiting for the number of the option to uninstall, if you press ENTER then it displays an error message like this

Error: must enter a number [1-3]

and exits. That's fine but checking the exit code for this with echo $? it shows 0. Shouldn't be something different than zero if it exited with an error message?


Tried on Mac Monterrey w/M1 chip & Linux Manjaro x64, Ruby 2.7.5p203 and 3.1.2p20 versions.
Hope this helps to make Ruby tooling even more awesome.
Regards

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Gem::Uninstaller#uninstall in rubygems/uninstaller.rb and #choose_from_list in rubygems/user_interaction.rb, then reproduce the multi-version uninstall prompt with Ctrl+D and an empty input. Done means Ctrl+D no longer produces a nil comparison error, and the invalid-selection path reports the intended result through the command's exit status.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.