Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby 2.7.4 Net Constant Warnings

I'm getting repetitive warnings upon running a ruby script in a crontab, as well as manually in the terminal.

/Users/rich/.rbenv/versions/2.7.4/lib/ruby/2.7.0/net/protocol.rb:66: warning: already initialized constant Net::ProtocRetryError
/Users/rich/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/net-protocol-0.1.2/lib/net/protocol.rb:68: warning: previous definition of ProtocRetryError was here
/Users/rich/.rbenv/versions/2.7.4/lib/ruby/2.7.0/net/protocol.rb:206: warning: already initialized constant Net::BufferedIO::BUFSIZE
/Users/rich/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/net-protocol-0.1.2/lib/net/protocol.rb:208: warning: previous definition of BUFSIZE was here
/Users/rich/.rbenv/versions/2.7.4/lib/ruby/2.7.0/net/protocol.rb:503: warning: already initialized constant Net::NetPrivate::Socket
/Users/rich/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/net-protocol-0.1.2/lib/net/protocol.rb:504: warning: previous definition of Socket was here

I've changed the script to either use net/http or Faraday, the latter I'm assuming requires the first. Having seen this behaviour before recently and way back, this is a reload of the net gem, which is part of the core if I am correct. I'm just not sure why it's reloading.

I'm using rbenv to juggle ruby versions for a couple of reasons, and that won't change. My shebang is #!/Users/rich/.rbenv/shims/ruby but my ruby version is a bit different:

$ which ruby
==> /Users/rich/.rbenv/versions/2.7.4/bin/ruby

$ ruby -v
==> Ruby version: ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [arm64-darwin21]

This slight difference from the shebang and requested versions of ruby might be the issue. I have many scripts that rely on that shebang, which points to the current rbenv version, which is what I want. I change versions from time to time and don't want to hard code that instruction.

Is there a way I can see why this is happening? How can I make these go away? How can I stop reloading core gems that are already loaded?

like image 223
Rich_F Avatar asked May 19 '26 23:05

Rich_F


2 Answers

I had to add the following 2 lines to the Gemfile to finally eliminate all the warnings:

gem 'net-http'
gem 'uri', '0.10.0'    # force the default version for ruby 2.7
like image 71
Juan Schwindt Avatar answered May 22 '26 14:05

Juan Schwindt


Bundler was the culprit with some dependency issues and reloading various versions on top of previous Gemfile.lock versions...something like that. Update this way:

bundle update --bundler
like image 38
Rich_F Avatar answered May 22 '26 15:05

Rich_F