chanzuckerberg / sorbet-rails

A set of tools to make the Sorbet typechecker work with Ruby on Rails seamlessly.
MIT License
638 stars 83 forks source link

warning: already initialized constant RAILS_RBI_RAKE_DIR #493

Closed billy1kaplan closed 2 years ago

billy1kaplan commented 2 years ago

Describe the bug:

After running Rails.application.load_tasks, I'm seeing the following

warning: already initialized constant RAILS_RBI_RAKE_DIR

on every call of bin/rails rails_rbi:...

Steps to reproduce:

I wanted to be able to automatically regenerate the sorbet-rails definition on changes to certain files. This works pretty well except for the above warning.

if Rails.env.development?
  # Make sure that the sorbet-rails tasks are available to be called
  Rails.application.load_tasks

  rails_routes_reloader = ActiveSupport::FileUpdateChecker.new([Rails.root.join("config", "routes.rb")]) do
    # For some reason, the reload can be invoked on the routes file changing but the routes haven't actually been
    # loaded into Rails. This forces Rails to update it's definition of the routes so that sorbet-rails can see them.
    Rails.application.reload_routes!

    Rake::Task["rails_rbi:routes"].invoke
  end

  ActiveSupport::Reloader.to_prepare do
    rails_routes_reloader.execute
  end
end

This might be a user error on my part. Is there a different way to autogenerate the definitions on config/routes.rb change?

Can we change: https://github.com/chanzuckerberg/sorbet-rails/blob/master/lib/sorbet-rails/tasks/rails_rbi.rake#L9

RAILS_RBI_RAKE_DIR  = File.dirname(__FILE__)

to

RAILS_RBI_RAKE_DIR  ||= File.dirname(__FILE__)

or is that a horrible idea?