Sorbet - Unable to resolve constant

Viewed 3195

After running srb init and srb tc passing in a new Rails project, I installed RSpec and srb tc now has many complaints including:

spec/spec_helper.rb:16: Unable to resolve constant RSpec https://srb.help/5002
    16 |RSpec.configure do |config|

How can I resolve this error?

3 Answers

This is because Sorbet does not know about anything defined in a gem by default. It needs to generate an RBI file for the gem to understand the gem's constants and APIs.

When installing new gems you have to update the RBI files, which is easy with

srb rbi update

Note: If this errors with No such file or directory @ rb_sysopen - update, change this line in your local gem to use STDIN.gets instead of Kernel.gets. There are at least a couple PRs addressing the issue that as of this answer are not merged in.

I suspect srb init does not finish properly. I have the RSpec module defined in sorbet/gems/rspec-core.rbi and a few other rspec gems rbi.

Do have a sorbet/gems folder? Does srb rbi gems work for you? If they don't, check out the Slack channel & ask about it there.

Edit: The issue is with gems installed after initial srb init. For those, you should run srb rbi gems to generate an RBI file for the added gems.

Related