Warnings when upgrading application from Rails 5 to Rails 7 - ruby-on-rails

with a recent upgrade to rails 7.0.2 from rails 5.2, when ever i start a rails console or a rails server on dev mode i see these warnings
/Users/opensource/.rvm/gems/ruby-2.7.4/gems/digest-3.1.0/lib/digest.rb:20: warning: already initialized constant Digest::REQUIRE_MUTEX
/Users/opensource/.rvm/rubies/ruby-2.7.4/lib/ruby/2.7.0/digest.rb:6: warning: previous definition of REQUIRE_MUTEX was here
/Users/opensource/.rvm/rubies/ruby-2.7.4/lib/ruby/2.7.0/net/protocol.rb:66: warning: already initialized constant Net::ProtocRetryError
/Users/opensource/.rvm/gems/ruby-2.7.4/gems/net-protocol-0.1.3/lib/net/protocol.rb:68: warning: previous definition of ProtocRetryError was here
/Users/opensource/.rvm/rubies/ruby-2.7.4/lib/ruby/2.7.0/net/protocol.rb:206: warning: already initialized constant Net::BufferedIO::BUFSIZE
/Users/opensource/.rvm/gems/ruby-2.7.4/gems/net-protocol-0.1.3/lib/net/protocol.rb:208: warning: previous definition of BUFSIZE was here
/Users/opensource/.rvm/rubies/ruby-2.7.4/lib/ruby/2.7.0/net/protocol.rb:503: warning: already initialized constant Net::NetPrivate::Socket
/Users/opensource/.rvm/gems/ruby-2.7.4/gems/net-protocol-0.1.3/lib/net/protocol.rb:504: warning: previous definition of Socket was here
/Users/opensource/.rvm/gems/ruby-2.7.4/gems/apipie-rails-0.7.0/lib/apipie/extractor/recorder.rb:163: warning: Skipping set of ruby2_keywords flag for process (method accepts keywords or method
For what's its worth the warnings are not present when the application is on Rails 7.0 but only appear when the application is upgraded to Rails 7.0.2
I upgraded rails as per the guide
5.2 -> 6.0 -> 6.1 -> 7.0 -> 7.0.2
May be i missed something, anyways I could fix these? probably these would go away with a ruby / rails subsequent upgrade?
Thanks.

Rails 7.0.1 added some gem dependencies for standard lib stuff that would get removed in ruby 3.0. This unfortunately causes issue with ruby 2.7.6. You can get around this in several ways. Upgrading ruby will also upgrade bundler which fixes the issue. Upgrading bundler without ruby also works but I don't recommend it (better to point to the system version). A more conservative workaround is find the gems that are causing issues and list them on the top of your Gemfile. In my case I had to make sure that the gem version was the same as the default ruby gem version. For example, I had to make the Gemfile have gem "uri", "0.10.0". A good way to debug what is causing these is put this at the top of your Gemfile:
Warning.module_eval do
alias_method :original_warn, :warn
def warn(msg)
if msg.match?(/already initialized constant/)
raise msg
else
original_warn(msg)
end
end
end
That way you get a stacktrace of the places that are requiring stdlib files that also have a conflicting rubygem.

It looks like these are all constants defined in dependency code gems. Running bundle clean --force or updating dependencies with bundle update and then running the bundle clean --force has worked for some when experiencing this issue.

Related

why is Rubocop raising “parser/current recognizes 2.5.5-compliant syntax, but you are running 2.5.3”?

In a Rails app I have started seeing the following in logs and test outputs.
warning: parser/current is loading parser/ruby25, which recognizes
warning: 2.5.5-compliant syntax, but you are running 2.5.3.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
This is coming from Rubocop.
I had understood that Rubocop checks whether a .ruby-version file exists in the app root and uses the Ruby version it specifies. https://rubocop.readthedocs.io/en/latest/configuration/#setting-the-target-ruby-version
The Rails app contains such a file
/.ruby-version
ruby-2.5.3
Why is Rubocop running checks against the wrong version of Ruby?
It's coming from parser, a dependency of rubocop.
https://github.com/whitequark/parser/blob/master/lib/parser/current.rb
Looking at this code it seems that if you are not using the last minor version of Ruby you're getting this warning.

How do I resolve this error that keeps me from starting Rails server?

I have searched documentation to this error that I have never seen before in working with Ruby on Rails. I just got a new MacBook and I installed Rails 4.2 and Ruby 2.4 and when I attempt to run rails server, I get this error:
.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/thread_safe-0.3.5/lib/thread_safe/cache.rb:155: warning: constant ::Fixnum is deprecated
How do I eliminate this error that is keeping me from starting up the Rails server?
Im not sure if that's the issue, since what you are seeing is just a warning. That warning appears because you are using ruby 2.4.0.
This version introduced this change: Unify Fixnum and Bignum into Integer
See here for the announcement: https://www.ruby-lang.org/en/news/2016/12/25/ruby-2-4-0-released/Z
The warnings come from the activesupport gem which is part of rails, and they will fix it soon.
Try downgrading your Ruby Version to 2.1 and try again.
Source

warning: constant ::Fixnum is deprecated When generating new model

I've tried to find some solution for this, but I really couldn't find anything related with the errors that is appearing to me when I run the rails command:
rails generate model Book title:string summary:text isbn:string
/home/vmu/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:51: warning: constant ::Fixnum is deprecated
/home/vmu/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:52: warning: constant ::Bignum is deprecated
/home/vmu/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/activesupport-5.0.1/lib/active_support/core_ext/numeric/conversions.rb:138: warning: constant ::Fixnum is deprecated
Running via Spring preloader in process 3579
Expected string default value for '--jbuilder'; got true (boolean)
invoke active_record
identical db/migrate/20170104114702_create_books.rb
identical app/models/book.rb
invoke test_unit
identical test/models/book_test.rb
identical test/fixtures/books.yml
Anyone know what may be causing these errors?
This warnings appear because you are using ruby 2.4.0.
This version introduced this change: Unify Fixnum and Bignum into Integer
See here for the announcement: https://www.ruby-lang.org/en/news/2016/12/25/ruby-2-4-0-released/
The warnings come from the activesupport gem which is part of rails and will be fixed in an upcoming release.
For now you can just ignore those warnings.
Update: Rails 5.0.2 has been released, which gets rid of the warnings.
I fixed mine by updating rails
bundle update rails
I assume you're using Rails 5? Check out this link (towards the bottom). Looks like these warnings will go away with release #27458.
If these deprecation warnings in active support are the only warnings you are seeing, you can surpress them by passing a RUBYOPT bash variable with the -W0 option which will silence.
so instead of rails server
try: RUBYOPT="-W0" rails server or RUBYOPT="-W0" bin/rails server
In rails 5.0 you may want to get in the habit of using bin/rails not just rails, since that's the global rails version which may or may not be the same as your local rails version.
I fixed this updating therubyracer gem from version '0.12.2' to '0.12.3'

After upgrade to Rails4

After Upgrade to Rails4 I am getting warning below how can i fix that
.../gems/activerecord-4.0.0/lib/active_record/core.rb:103: warning: already initialized constant #<Module:0xbc58784>::AttrNames
.../gems/activerecord-4.0.0/lib/active_record/core.rb:103: warning: previous definition of AttrNames was here
Please Help Me.
My Functionality is working is fine but I don't want warning in my app so how can i fix that.
This is probably caused by an old gem. For example an old version of ActiveAdmin caused similar warnings. Many old gems are not fully compatible with Rails 4.
To update gems run bundle update and make sure that in the Gemfile there are no old versions specified.

Rails 3.0 & Ruby 1.9.2rc: Rake commands return 'already initialized constant' & stack level too deep errors. Any ideas

I'm trying to run Rails 3 beta 4 & Ruby 1.9.2rc on Ubuntu 10.04. It worked initially, but after doing my first bundle install/package, I now get the following errors in all rails projects. Even a basic 'rails new testproject' followed by a rake brings up the error messages.
In short, I'm stumped. Any help regarding what could be causing this would be very appreciated.
The only thing I noticed - which may or may not be relevant - is that the directory in the ~/.bundle files is ruby/1.9.1. 1.9.1 is not installed on my machine - only 1.9.2rc. ruby -v brings back 1.9.2
(in /home/john/Websites/sandbox/testerino)
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:32: warning: already initialized constant RAKEVERSION
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake/alt_system.rb:32: warning: already initialized constant WINDOWS
WARNING: Possible conflict with Rake extension: String#ext already exists
WARNING: Possible conflict with Rake extension: String#pathmap already exists
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:404: warning: already initialized constant EMPTY_TASK_ARGS
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:452: warning: already initialized constant EMPTY
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:960: warning: already initialized constant RUBY_EXT
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:964: warning: already initialized constant RUBY
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:1033: warning: already initialized constant LN_SUPPORTED
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:1242: warning: already initialized constant ARRAY_METHODS
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:1245: warning: already initialized constant MUST_DEFINE
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:1249: warning: already initialized constant MUST_NOT_DEFINE
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:1253: warning: already initialized constant SPECIAL_RETURN
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:1259: warning: already initialized constant DELEGATING_METHODS
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:1569: warning: already initialized constant DEFAULT_IGNORE_PATTERNS
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:1575: warning: already initialized constant DEFAULT_IGNORE_PROCS
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:1612: warning: already initialized constant FileList
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:1638: warning: already initialized constant EARLY
/home/john/.bundle/ruby/1.9.1/gems/rake-0.8.7/lib/rake.rb:1968: warning: already initialized constant DEFAULT_RAKEFILES
rake aborted!
stack level too deep
I've been running into this issue too. It doesn't seem to be related to the specific versions of rails or ruby you mention, which are different than the ones I'm using (Rails 2.3.8, Rake 0.8.7, Ruby 1.9.1p378). It seems to be related to bundler and rake not working well together.
A resolution that worked for me is mentioned at the bottom of this lighthouse ticket. Here is the short version:
Run "bundle exec bash"; see if rake works now - if it does,
Make sure that the bash environments, before and after, are the same by consulting the env command and modifying ~/.bashrc or ~/.bash_profile accordingly.
Once you do this, you mess up bundler a little bit. At that point you have to clear RUBYOPT in order to run the bundle command:
RUBYOPT= bundle install --relock
EDIT:
Thinking about it a little more, I'm not sure this is necessarily the best way to address this particular issue. You might give Hiral Desai's tip and some other answers a try before resorting to this approach, since this one changes the environment.
I'm afraid the easier solution is running this command instead of rake db:migrate
bundle exec rake db:migrate
As per the previous message from Eric W. the RUBYOPT environment variable is set.
If you want to exit the new bash shell that is opened by "bundle exec bash" then just copy the RUBYOPT environment line, exit the shell then type "export RUBYOPT='THE ARGS THAT YOU COPIED FROM THE PREVIOUS ENVIRONMENT'
Note: this works with ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]
, Rails 3.0.7, gem 1.8.0 and Rake 0.8.7
bundle exec bash works for me
I was able to solve this issue by downgrading my rubygem install from 1.8.10 to 1.7.2.
gem update --system 1.7.2
Looks like a strange recursive require.
Did you try uninstalling all versions of rake and reinstalling it? There is a bug in 1.9.2rc affecting gem loading, so that might be it? I didn't look closely into it as many people are already following it closely...
Note: The 1.9.1 is the API version, so it is correct.
I had a .bundler folder on my root directory which was conflicting. Removing it did it for me.
rm -rf ~/.bundler
I've been running into this issue too after updating rubygem (1.8.10) and bundler (1.0.18)
I solved updating rake to 0.9.2
Just got such error because of deleted .rvmrc
So please make sure you running rake in correct environment ;)
If you have RVM installed, this issue can start to occur once you update rubygems to 1.8.15. The issue was that I had rake installed in the global gemset and my project's gemset. The solution was to have only one installation of rake. To delete it from the project gemset:
1. cd to the project
2. gem uninstall rake
Another possible solution is given at http://rubyist-journal.com/2011/07/29/howto-fix-rake-0-9-2-to-work-with-ruby-1-9-2-under-rvm/
It seems to boil down to having the same rake gem installed both in your user gem directory and system-wide. Removing either one fixes the issue.
This can happen when you have the rake gem installed in both your repo's rvm gemset and the global one.
Update to latest Rails minor version.
So if you are at 4.2, update => 4.2.latest.

Resources