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

Unable to resolve constant Parser #384

Open scottjg opened 3 years ago

scottjg commented 3 years ago

Describe the bug: Trying sorbet and sorbet-rails for the first time. Added sorbet-rails to my gemfile and ran:

% bundle exec srb typecheck -e 'puts "Hello, world!"'

which resulted in the following errors

parlour.rbi:63: Unable to resolve constant Parser https://srb.help/5002
    63 |    sig { returns(Parser::Source::Buffer) }
                          ^^^^^^
  Autocorrect: Use `-a` to autocorrect
    parlour.rbi:63: Replace with Gem::RequestSet::Lockfile::Parser
    63 |    sig { returns(Parser::Source::Buffer) }
                          ^^^^^^
  Autocorrect: Use `-a` to autocorrect
    parlour.rbi:63: Replace with JSON::Ext::Parser
    63 |    sig { returns(Parser::Source::Buffer) }
                          ^^^^^^
  Autocorrect: Use `-a` to autocorrect
    parlour.rbi:63: Replace with Psych::Parser
    63 |    sig { returns(Parser::Source::Buffer) }
                          ^^^^^^
    https://github.com/sorbet/sorbet/tree/937479e090025723ff7e5711bef42036da8df172/rbi/stdlib/rubygems.rbi#L2819: Did you mean: Gem::RequestSet::Lockfile::Parser?
    2819 |class Gem::RequestSet::Lockfile::Parser
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    https://github.com/sorbet/sorbet/tree/937479e090025723ff7e5711bef42036da8df172/rbi/stdlib/json.rbi#L974: Did you mean: JSON::Ext::Parser?
     974 |class JSON::Ext::Parser
          ^^^^^^^^^^^^^^^^^^^^^^^
    https://github.com/sorbet/sorbet/tree/937479e090025723ff7e5711bef42036da8df172/rbi/stdlib/psych.rbi#L982: Did you mean: Psych::Parser?
     982 |class Psych::Parser
          ^^^^^^^^^^^^^^^^^^^

parlour.rbi:66: Unable to resolve constant Parser https://srb.help/5002
    66 |    sig { returns(Parser::Source::Range) }
                          ^^^^^^
  Autocorrect: Use `-a` to autocorrect
    parlour.rbi:66: Replace with Gem::RequestSet::Lockfile::Parser
    66 |    sig { returns(Parser::Source::Range) }
                          ^^^^^^
  Autocorrect: Use `-a` to autocorrect
    parlour.rbi:66: Replace with JSON::Ext::Parser
    66 |    sig { returns(Parser::Source::Range) }
                          ^^^^^^
  Autocorrect: Use `-a` to autocorrect
    parlour.rbi:66: Replace with Psych::Parser
    66 |    sig { returns(Parser::Source::Range) }
                          ^^^^^^
    https://github.com/sorbet/sorbet/tree/937479e090025723ff7e5711bef42036da8df172/rbi/stdlib/rubygems.rbi#L2819: Did you mean: Gem::RequestSet::Lockfile::Parser?
    2819 |class Gem::RequestSet::Lockfile::Parser
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    https://github.com/sorbet/sorbet/tree/937479e090025723ff7e5711bef42036da8df172/rbi/stdlib/json.rbi#L974: Did you mean: JSON::Ext::Parser?
     974 |class JSON::Ext::Parser
          ^^^^^^^^^^^^^^^^^^^^^^^
    https://github.com/sorbet/sorbet/tree/937479e090025723ff7e5711bef42036da8df172/rbi/stdlib/psych.rbi#L982: Did you mean: Psych::Parser?
     982 |class Psych::Parser
          ^^^^^^^^^^^^^^^^^^^

parlour.rbi:139: Unable to resolve constant Parser https://srb.help/5002
     139 |      sig { params(start: Parser::AST::Node).returns(Parser::AST::Node) }
                                    ^^^^^^
  Autocorrect: Use `-a` to autocorrect
    parlour.rbi:139: Replace with Gem::RequestSet::Lockfile::Parser
     139 |      sig { params(start: Parser::AST::Node).returns(Parser::AST::Node) }
                                    ^^^^^^
  Autocorrect: Use `-a` to autocorrect
    parlour.rbi:139: Replace with JSON::Ext::Parser
     139 |      sig { params(start: Parser::AST::Node).returns(Parser::AST::Node) }
                                    ^^^^^^
  Autocorrect: Use `-a` to autocorrect
    parlour.rbi:139: Replace with Psych::Parser
     139 |      sig { params(start: Parser::AST::Node).returns(Parser::AST::Node) }
                                    ^^^^^^
    https://github.com/sorbet/sorbet/tree/937479e090025723ff7e5711bef42036da8df172/rbi/stdlib/rubygems.rbi#L2819: Did you mean: Gem::RequestSet::Lockfile::Parser?
    2819 |class Gem::RequestSet::Lockfile::Parser
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    https://github.com/sorbet/sorbet/tree/937479e090025723ff7e5711bef42036da8df172/rbi/stdlib/json.rbi#L974: Did you mean: JSON::Ext::Parser?
     974 |class JSON::Ext::Parser
          ^^^^^^^^^^^^^^^^^^^^^^^
    https://github.com/sorbet/sorbet/tree/937479e090025723ff7e5711bef42036da8df172/rbi/stdlib/psych.rbi#L982: Did you mean: Psych::Parser?
     982 |class Psych::Parser
          ^^^^^^^^^^^^^^^^^^^

parlour.rbi:139: Unable to resolve constant Parser https://srb.help/5002
     139 |      sig { params(start: Parser::AST::Node).returns(Parser::AST::Node) }
                                                               ^^^^^^
  Autocorrect: Use `-a` to autocorrect
    parlour.rbi:139: Replace with Gem::RequestSet::Lockfile::Parser
     139 |      sig { params(start: Parser::AST::Node).returns(Parser::AST::Node) }
                                                               ^^^^^^
  Autocorrect: Use `-a` to autocorrect
    parlour.rbi:139: Replace with JSON::Ext::Parser
     139 |      sig { params(start: Parser::AST::Node).returns(Parser::AST::Node) }
                                                               ^^^^^^
  Autocorrect: Use `-a` to autocorrect
    parlour.rbi:139: Replace with Psych::Parser
     139 |      sig { params(start: Parser::AST::Node).returns(Parser::AST::Node) }
                                                               ^^^^^^
    https://github.com/sorbet/sorbet/tree/937479e090025723ff7e5711bef42036da8df172/rbi/stdlib/rubygems.rbi#L2819: Did you mean: Gem::RequestSet::Lockfile::Parser?
    2819 |class Gem::RequestSet::Lockfile::Parser
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    https://github.com/sorbet/sorbet/tree/937479e090025723ff7e5711bef42036da8df172/rbi/stdlib/json.rbi#L974: Did you mean: JSON::Ext::Parser?
     974 |class JSON::Ext::Parser
          ^^^^^^^^^^^^^^^^^^^^^^^
    https://github.com/sorbet/sorbet/tree/937479e090025723ff7e5711bef42036da8df172/rbi/stdlib/psych.rbi#L982: Did you mean: Psych::Parser?
     982 |class Psych::Parser
          ^^^^^^^^^^^^^^^^^^^

parlour.rbi:143: Unable to resolve constant Parser https://srb.help/5002
     143 |    sig { params(ast: Parser::AST::Node, unknown_node_errors: T::Boolean, generator: T.nilable(RbiGenerator)).void }
                                ^^^^^^

parlour.rbi:149: Unable to resolve constant Parser https://srb.help/5002
     149 |    sig { returns(Parser::AST::Node) }
                            ^^^^^^

parlour.rbi:149: Unable to resolve constant Parser https://srb.help/5002
     149 |    sig { returns(Parser::AST::Node) }
                            ^^^^^^

parlour.rbi:149: Unable to resolve constant Parser https://srb.help/5002
     149 |    sig { returns(Parser::AST::Node) }
                            ^^^^^^

parlour.rbi:171: Unable to resolve constant Parser https://srb.help/5002
     171 |      prop :params, T.nilable(T::Array[Parser::AST::Node])
                                                 ^^^^^^

parlour.rbi:171: Unable to resolve constant Parser https://srb.help/5002
     171 |      prop :params, T.nilable(T::Array[Parser::AST::Node])
                                                 ^^^^^^

parlour.rbi:171: Unable to resolve constant Parser https://srb.help/5002
     171 |      prop :params, T.nilable(T::Array[Parser::AST::Node])
                                                 ^^^^^^

parlour.rbi:171: Unable to resolve constant Parser https://srb.help/5002
     171 |      prop :params, T.nilable(T::Array[Parser::AST::Node])
                                                 ^^^^^^

parlour.rbi:171: Unable to resolve constant Parser https://srb.help/5002
     171 |      prop :params, T.nilable(T::Array[Parser::AST::Node])
                                                 ^^^^^^

parlour.rbi:171: Unable to resolve constant Parser https://srb.help/5002
     171 |      prop :params, T.nilable(T::Array[Parser::AST::Node])
                                                 ^^^^^^

parlour.rbi:184: Unable to resolve constant Parser https://srb.help/5002
     184 |    sig { params(node: T.nilable(Parser::AST::Node)).returns(T::Array[Symbol]) }
                                           ^^^^^^

parlour.rbi:187: Unable to resolve constant Parser https://srb.help/5002
     187 |    sig { params(node: Parser::AST::Node).returns(T::Boolean) }
                                 ^^^^^^

parlour.rbi:193: Unable to resolve constant Parser https://srb.help/5002
     193 |    sig { params(node: T.nilable(Parser::AST::Node)).returns(T.nilable(String)) }
                                           ^^^^^^

parlour.rbi:196: Unable to resolve constant Parser https://srb.help/5002
     196 |    sig { params(node: T.nilable(Parser::AST::Node), modifier: Symbol).returns(T::Boolean) }
                                           ^^^^^^

parlour.rbi:199: Unable to resolve constant Parser https://srb.help/5002
     199 |    sig { params(node: Parser::AST::Node).returns([T::Array[String], T::Array[String]]) }
                                 ^^^^^^

parlour.rbi:202: Unable to resolve constant Parser https://srb.help/5002
     202 |    sig { params(desc: String, node: T.any(Parser::AST::Node, NodePath)).returns(T.noreturn) }

Steps to reproduce:

Add sorbet-rails to gemfile. bundle. run bundle exec srb typecheck -e 'puts "Hello, world!"

Expected behavior:

no errors

Versions:

% ruby --version
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin20]
% bundle exec rails --version
Rails 6.1.0
% bundle exec srb --version
Sorbet typechecker 0.5.6164 git 937479e090025723ff7e5711bef42036da8df172 debug_symbols=true clean=1
% head -n 5 Gemfile.lock
GIT
  remote: https://github.com/chanzuckerberg/sorbet-rails.git
  revision: e2584c70d251445810d9df790d7f04cf55b45609
  specs:
    sorbet-rails (0.7.2)
hdoan741 commented 3 years ago

@scottjg Thanks for reporting the issue! Parser is a dependency that is specified in sorbet-rails gemspec. Can you check if you see parser in your Gemfile and you have a parser.rbi file in your repo? The file should be autogenerated by sorbet as part of running "srb init" or "srb rbi gems"

scottjg commented 3 years ago

sorry it took me so long to come back to this. in my case i hadn't run srb init (i could never get it to work), so i guess it hadn't generated the Parser rbi. I appreciate your help and I realize you're basically playing tech support for me so i apologize in advance.

I tried everything again today:

  1. add sorbet and sorbet-rails into the Gemfile, then bundle.
  2. run bundle exec srb init
  3. upon completion, run bundle exec srb tc

at this point there's no errors so i tried to get the rails stuff working

  1. pick a model (in my case it was called Channel) and set type comment to # typed: true
  2. rerun bundle exec srb tc

at this point, the first error is:

./app/models/channel.rb:117: Method `show_recent_videos` does not exist on `Channel` https://srb.help/7003
     117 |    show_recent_videos
              ^^^^^^^^^^^^^^^^^^

in my case, this is a field in the schema (from schema.rb):

  create_table "channels", force: :cascade do |t|
...
    t.boolean "show_recent_videos", default: true, null: false

next, i tried running bundle exec rake rails_rbi:models, which generated an rbi for Channel.

unfortunately, upon running srb tc again, now it appears my rails rbis are broken as i receive a few hundred of these errors:

./sorbet/rails-rbi/models/channel.rbi:435: Unable to resolve constant BatchEnumerator https://srb.help/5002
     435 |    ).returns(ActiveRecord::Batches::BatchEnumerator)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

./sorbet/rails-rbi/models/channel.rbi:548: Unable to resolve constant BatchEnumerator https://srb.help/5002
     548 |    ).returns(ActiveRecord::Batches::BatchEnumerator)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

if i run srb rbi gems, then i'm back to this if i srb tc again

./app/models/channel.rb:117: Method `show_recent_videos` does not exist on `Channel` https://srb.help/7003
     117 |    show_recent_videos
              ^^^^^^^^^^^^^^^^^^

i must be doing something obviously wrong... but alas, still no luck getting a typechecked rails model.