chanzuckerberg / sorbet-rails

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

Type of Rails.application should be YourApp::Application #98

Open bradleybuda opened 5 years ago

bradleybuda commented 5 years ago

Moved from: https://github.com/sorbet/sorbet-typed/issues/83

Describe the bug: In a Rails application the type of Rails.application is actually a subclass of Rails::Application that is generated when you type rails new. For example, rails new blog, Rails.application will create a class called Blog::Application defined in config/application.rb.

I am willing to make an attempt at fixing this if you think it's a valid change - let me know then I'll send a PR.

Steps to reproduce:

The impact of this is that any custom methods you define in application.rb on Blog::Application will not be visible to the type checker. If I have config/application.rb that looks like this:

# typed: true
require_relative 'boot'
require 'rails'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Blog
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2

    def name
      "Brad's awesome blog"
    end
  end
end

then I can't call Rails.application.name in a type-safe way.

Versions:

$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]

$ rails --version
Rails 5.2.1

$ srb --version
Sorbet typechecker 0.4.4429 git 51504253c985d0a967d3df6a39ac44b25db2c481 built on 2019-07-12 00:02:11 GMT with debug symbols
hdoan741 commented 5 years ago

Sure! I think this is a valid change. We probably want to introduce a new generator for this

On Thu, Jul 25, 2019 at 2:03 PM Bradley Buda notifications@github.com wrote:

Moved from: sorbet/sorbet-typed#83 https://github.com/sorbet/sorbet-typed/issues/83

Describe the bug: In a Rails application the type of Rails.application https://github.com/sorbet/sorbet-typed/blob/d17bc9ee37629ed824f78d3a2f92e99a8b4e8709/lib/railties/all/railties.rbi#L5 is actually a subclass of Rails::Application that is generated when you type rails new. For example, rails new blog, Rails.application will create a class called Blog::Application defined in config/application.rb.

I am willing to make an attempt at fixing this if you think it's a valid change - let me know then I'll send a PR.

Steps to reproduce:

The impact of this is that any custom methods you define in application.rb on Blog::Application will not be visible to the type checker. If I have config/application.rb that looks like this:

typed: truerequire_relative 'boot'require 'rails'

Require the gems listed in Gemfile, including any gems# you've limited to :test, :development, or :production.Bundler.require(*Rails.groups)

module Blog class Application < Rails::Application

Initialize configuration defaults for originally generated Rails version.

config.load_defaults 5.2

def name
  "Brad's awesome blog"
end

endend

then I can't call Rails.application.name in a type-safe way.

Versions:

$ ruby --version ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]

$ rails --version Rails 5.2.1

$ srb --version Sorbet typechecker 0.4.4429 git 51504253c985d0a967d3df6a39ac44b25db2c481 built on 2019-07-12 00:02:11 GMT with debug symbols

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/chanzuckerberg/sorbet-rails/issues/98?email_source=notifications&email_token=AAFH4ALKS4D6S4JEOGAQVOLQBIIINA5CNFSM4IG6NDSKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HBSO3BA, or mute the thread https://github.com/notifications/unsubscribe-auth/AAFH4AN56357CS4JF3PFIMTQBIIINANCNFSM4IG6NDSA .