Type Checking with Sorbet

The majority of the code in Homebrew is written in Ruby which is a dynamic language. To avail the benefits of static type checking, we have set up Sorbet in our codebase which provides the benefits of static type checking to dynamic languages like Ruby.
Sorbet’s Documentation is a good place to get started if you want to dive deeper into Sorbet and it’s abilities.

Sorbet elements in the Homebrew Codebase

The sorbet/ directory in Library/Homebrew consists of:

Using brew typecheck

When run without any arguments, brew typecheck, will run considering the strictness levels set in the files.yaml file. However, when typecheck is run on a specific file or directory, more errors may show up since Sorbet can not resolve constants defined outside the scope of the specified file. These problems can be solved with RBI files. Currently brew typecheck provides quiet, --file, --dir and --ignore options but you can explore more options with srb tc --help and passing them with srb tc.

Resolving Type Errors

Sorbet reports type errors along with an error reference code, which can be used to look up more information on how to debug the error, or what causes the error in the Sorbet documentation. Here is how we debug some common type errors:

Method Signatures

Detailed explanation about why we use Method Signatures and its syntax can be found here. The only extra thing to keep in mind is that we add method signatures to RBI files instead of the actual method definition in the code. This way we preserve the original code structure and everything related to Sorbet is kept within the Library/Homebrew/sorbet directory.

Fork me on GitHub