Jeremy Lightsmith
Jeremy Lightsmith

Reputation: 256

Sorbet fails when using creating an instance of an autoloaded class in rails initializer

In a rails app using sorbet, when you have a method that expects an instance of a type, say Foo. And you need to initialize an instance of Foo in an initializer for the app that persists in memory between requests. And then you make any change to the source code of Foo or any file that Foo uses. Then sorbet believes that this instance is no longer an instance of Foo, and you need to restart your app in order for it to stop erroring.

This seems like a rare case, but in our app, we use a bit of dependency injection, and it's a large team. So sorbet is making us restart our app almost every time we do a git update, and many, many times throughout the day as we write code. We have a large app which takes a while to restart, and it's quite frustrating to have to do this.

Any ideas on how to fix this? If helpful, I could make a sample rails app to demonstrate this behavior.

Upvotes: 1

Views: 486

Answers (1)

xcskier56
xcskier56

Reputation: 621

I ran into a similar issue myself when I was referencing a model in a Rails initializer. I was advised that there are 2 solutions:

  1. Refactor the code so that you don't have this issue
  2. Add checked(:tests) or checked(:never) to the sigs of methods that are having this problem. This would preserve the runtime and test time checks, but eliminate the errors in development. See the link below for documentation

https://sorbet.org/docs/runtime#checked-whether-to-check-in-the-first-place

Upvotes: 1

Related Questions