Repository: ruby/rubygems (Ruby)

Issue:
[Slightly confusing error message] linked to incompatible W���U - /root/.gem/gems/digest-3.1.1/lib/digest.so (LoadError)
So let me first explain the background situation.

I have had some unrelated issues with openssl (on linux) and decided to start anew and recompile
ruby (latest stable ruby always, so ruby 3.2.1).

When I did so, though, I got error messages such as:

    gem install gtk3

    /usr/lib/ruby/3.2.0/x86_64-linux/openssl.so: warning: already initialized constant OpenSSL::OPENSSL_VERSION
    /usr/lib/ruby/3.2.0/x86_64-linux/openssl.so: warning: already initialized constant OpenSSL::OPENSSL_LIBRARY_VERSION
    /usr/lib/ruby/3.2.0/x86_64-linux/openssl.so: warning: already initialized constant OpenSSL::OPENSSL_VERSION_NUMBER
    /usr/lib/ruby/3.2.0/x86_64-linux/openssl.so: warning: already initialized constant OpenSSL::OPENSSL_FIPS
    /usr/lib/ruby/3.2.0/x86_64-linux/openssl.so: warning: already initialized constant OpenSSL::BN::CONSTTIME
    /usr/lib/ruby/3.2.0/x86_64-linux/openssl.so: warning: already initialized constant OpenSSL::Config::DEFAULT_CONFIG_FILE
    /usr/lib/ruby/3.2.0/x86_64-linux/openssl.so: warning: already initialized constant OpenSSL::OPENSSL_VERSION
    /usr/lib/ruby/3.2.0/x86_64-linux/openssl.so: warning: already initialized constant OpenSSL::OPENSSL_LIBRARY_VERSION
    /usr/lib/ruby/3.2.0/x86_64-linux/openssl.so: warning: already initialized constant OpenSSL::OPENSSL_VERSION_NUMBER
    /usr/lib/ruby/3.2.0/x86_64-linux/openssl.so: warning: already initialized constant OpenSSL::OPENSSL_FIPS
    /usr/lib/ruby/3.2.0/x86_64-linux/openssl.so: warning: already initialized constant OpenSSL::BN::CONSTTIME
    /usr/lib/ruby/3.2.0/x86_64-linux/openssl.so: warning: already initialized constant OpenSSL::Config::DEFAULT_CONFIG_FILE

    ERROR:  While executing gem ... (ArgumentError)
        invalid byte sequence in UTF-8

        text.gsub(/[\000-\b\v-\f\016-\037\177]/, ".".freeze)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        /usr/lib/ruby/3.2.0/rubygems/text.rb:12:in `gsub'
        /usr/lib/ruby/3.2.0/rubygems/text.rb:12:in `clean_text'
        /usr/lib/ruby/3.2.0/rubygems/command_manager.rb:242:in `rescue in load_and_instantiate'
        /usr/lib/ruby/3.2.0/rubygems/command_manager.rb:232:in `load_and_instantiate'
        /usr/lib/ruby/3.2.0/rubygems/command_manager.rb:135:in `[]'
        /usr/lib/ruby/3.2.0/rubygems/command_manager.rb:207:in `find_command'
        /usr/lib/ruby/3.2.0/rubygems/command_manager.rb:249:in `invoke_command'
        /usr/lib/ruby/3.2.0/rubygems/command_manager.rb:191:in `process_args'
        /usr/lib/ruby/3.2.0/rubygems/command_manager.rb:149:in `run'
        /usr/lib/ruby/3.2.0/rubygems/gem_runner.rb:51:in `run'
        /usr/bin/gem:10:in `<main>'

This was already a bit confusing to me, because I was unaware of how or why OpenSSL namespace was
already initialized.

But then I got an even stranger error message:

    <internal:/home/x/Temp/rbt/rubygems-3.4.7/lib/rubygems/core_ext/kernel_require.rb>:88:in `require': linked to 
     incompatible W���U - /root/.gem/gems/digest-3.1.1/lib/digest.so (LoadError)
            from <internal:/home/x/Temp/rbt/rubygems-3.4.7/lib/rubygems/core_ext/kernel_require.rb>:88:in `require'
            from /root/.gem/gems/digest-3.1.1/lib/digest/loader.rb:3:in `<top (required)>'
            from <internal:/home/x/Temp/rbt/rubygems-3.4.7/lib/rubygems/core_ext/kernel_require.rb>:88:in `require'
            from <internal:/home/x/Temp/rbt/rubygems-3.4.7/lib/rubygems/core_ext/kernel_require.rb>:88:in `require'
            from /root/.gem/gems/digest-3.1.1/lib/digest.rb:16:in `<top (required)>'
            from <internal:/home/x/Temp/rbt/rubygems-3.4.7/lib/rubygems/core_ext/kernel_require.rb>:88:in `require'
            from <internal:/home/x/Temp/rbt/rubygems-3.4.7/lib/rubygems/core_ext/kernel_require.rb>:88:in `require'
            from /home/x/Temp/rbt/rubygems-3.4.7/lib/rubygems/security.rb:469:in `<module:Security>'
            from /home/x/Temp/rbt/rubygems-3.4.7/lib/rubygems/security.rb:327:in `<top (required)>'

I was not even certain what the issue was. Eventually I realised that some old gems were picked up from
/root/.gem/. So, I deleted that directory and recompiled ruby from source, just to be safe - and now everything
works again. So, that issue is no longer an issue.

However had, the display is very odd:

     linked to  incompatible W���U - /root/.gem/gems/digest-3.1.1/lib/digest.so (LoadError)

It literally shows a W and three odd unicode-like question marks and then U.

Could this message be improved? It does not tell me much. I also was not sure how or why I
got that error message, until I realised that this must have come from the older ruby version
in place. I don't expect this is an issue many other people run into; most will not compile ruby
from source. So this is probably not worth fixing or improving. And thus, the issue can be
closed.

Nonetheless I wanted to report it, just to make sure this was already known. I never saw such
strange ??? question marks on the commandline before. I assume this may fetch in some
binary data or something but this is also weird - normally I get simple text displayed, so this
is a bit strange IMO. Also the error:

        invalid byte sequence in UTF-8

        text.gsub(/[\000-\b\v-\f\016-\037\177]/, ".".freeze)

Should never show up. I understand that this is most likely caused by the earlier openssl/digest
problem, but in that case there should be an earlier check, and then informing the user what
the problem is, if it is a critical error. But as said I am just reporting this to let you guys
know - it probably really is not worth the time to improve upon, compared to other, more
serious issues that may exist in rubygems/bundler. rubygems also appears to be steadily
improving which is good - seems to keep people motivated to keep on improving it. Even
the old "file name too long" issue, while still there, appears to have been improved a bit
message-wise at the least.


Discussion: @rubyFeedback It sounds like you have some encoding issues in your system. We can add a better message so that RubyGems does not crash like that like I did at #6471, but I suspect you're going to have issues with other programs too until you fix the root cause.



Fix (patch):
diff --git a/lib/rubygems/gem_runner.rb b/lib/rubygems/gem_runner.rb
index 8335a0ad03fc..4cb924677f83 100644
--- a/lib/rubygems/gem_runner.rb
+++ b/lib/rubygems/gem_runner.rb
@@ -29,6 +29,7 @@ def initialize
   # Run the gem command with the following arguments.
 
   def run(args)
+    validate_encoding args
     build_args = extract_build_args args
 
     do_configuration args
@@ -72,6 +73,14 @@ def extract_build_args(args) # :nodoc:
 
   private
 
+  def validate_encoding(args)
+    invalid_arg = args.find {|arg| !arg.valid_encoding? }
+
+    if invalid_arg
+      raise Gem::OptionParser::InvalidArgument.new("'#{invalid_arg.scrub}' has invalid encoding")
+    end
+  end
+
   def do_configuration(args)
     Gem.configuration = @config_file_class.new(args)
     Gem.use_paths Gem.configuration[:gemhome], Gem.configuration[:gempath]