Sorbet
A fast, powerful type checker designed for Ruby
Install / Use
/learn @sorbet/SorbetREADME
Sorbet
This repository contains Sorbet, a fast, powerful type checker designed for Ruby. It aims to be easy to add to existing codebases with gradual types, and fast to respond with errors and suggestions.
This README contains documentation specifically for contributing to Sorbet. You might also want to:
- Read the public Sorbet docs
- Or even edit the docs
- Watch the talks we've given about Sorbet
- Try the Sorbet playground online
If you are at Stripe, you might also want to see http://go/types/internals for docs about Stripe-specific development workflows and historical Stripe context.
<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->Table of Contents
- Sorbet user-facing design principles
- Quickstart
- Learning how Sorbet works
- Building Sorbet
- Running Sorbet
- Running the tests
- Testing Sorbet against pay-server
- Writing tests
- Debugging
- Writing docs
- Editor and environment
Sorbet user-facing design principles
Early in our project, we've defined some guidelines for how working with Sorbet should feel like.
-
Explicit
We're willing to write annotations, and in fact see them as beneficial; they make code more readable and predictable. We're here to help readers as much as writers.
-
Feel useful, not burdensome
While it is explicit, we are putting effort into making it concise. This shows in multiple ways:
- error messages should be clear
- verbosity should be compensated with more safety
-
As simple as possible, but powerful enough
Overall, we are not strong believers in super-complex type systems. They have their place, and we need a fair amount of expressive power to model (enough) real Ruby code, but all else being equal we want to be simpler. We believe that such a system scales better, and—most importantly—is easier for our users to learn & understand.
-
Compatible with Ruby
In particular, we don't want a new syntax. Existing Ruby syntax means we can leverage most of our existing tooling (editors, etc). Also, the point of Sorbet is to gradually improve an existing Ruby codebase. No new syntax makes it easier to be compatible with existing tools.
-
Scales
On all axes: execution speed, number of collaborators, lines of code, codebase age. We work in large Ruby codebases, and they will only get larger.
-
Can be adopted gradually
In order to make adoption possible at scale, we cannot require every team or project to adopt Sorbet all at once. Sorbet needs to support teams adopting it at different paces.
Quickstart
-
Install the dependencies
brew install bazel autoconf coreutils parallel
-
Clone this repository
git clone https://github.com/sorbet/sorbet.gitcd sorbet
-
Build Sorbet
./bazel build //main:sorbet --config=dbg
-
Run Sorbet!
bazel-bin/main/sorbet -e "42 + 'hello'"
Learning how Sorbet works
We've documented the internals of Sorbet in a separate doc. Cross-reference between that doc and here to learn how Sorbet works and how to change it!
There is also a talk online that describes Sorbet's high-level architecture and the reasons why it's fast:
Building Sorbet
There are multiple ways to build sorbet. This one is the most common:
./bazel build //main:sorbet --config=dbg
This will build an executable in bazel-bin/main/sorbet (see "Running Sorbet"
below). There are many options you can pass when building sorbet:
--config=dbg- Most common build config for development.
- Good stack traces, runs all ENFORCEs.
--config=sanitize- Link in extra sanitizers, in particular: UBSan and ASan.
- Catches most memory and undefined-behavior errors.
- Substantially larger and slower binary.
--config=debugsymbols- (Included by
--config=dbg) debugging symbols, and nothing else.
- (Included by
--config=forcedebug- Use more memory, but report even more sanity checks.
--config=static-libs- Forcibly use static linking (Sorbet defaults to dynamic linking for faster build times).
- Sorbet already uses this option in release builds (see below).
--config=release-macand--config=release-linux- Exact release configuration that we ship to our users.
Independently of providing or omitting any of the above flags, you can turn on optimizations for any build:
-c opt- Enables
clangoptimizations (i.e.,-O2)
- Enables
These args are not mutually exclusive. For example, a common pairing when debugging is
--config=dbg --config=sanitize
In .bazelrc you can find out what all these options (and others) mean.
Common Compilation Errors
(Mac) Xcode version must be specified to use an Apple CROSSTOOL
This error typically occurs after an Xcode upgrade.
Developer tools must be installed, the Xcode license must be accepted, and your active Xcode command line tools directory must point to an installed version of Xcode.
The following commands should do the trick:
# Install command line tools
xcode-select --install
# Ensure that the system finds command line tools in an active Xcode directory
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
# Accept the Xcode license.
sudo xcodebuild -license
# Clear bazel's cache, which may contain files generated from a previous
# version of Xcode command line tools.
bazel clean --expunge
(Mac) fatal error: 'math.h' file not found (or some other system header)
This error can happen on Macs when the /usr/include folder is missing. The
solution is to install macOS headers via the following package:
macOS Mojave:
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
macOS Catalina:
sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
Running Sorbet
Run Sorbet on an expression:
bazel-bin/main/sorbet -e "1 + false"
Run Sorbet on a file:
bazel-bin/main/sorbet foo.rb
Running bazel-bin/main/sorbet --help will show lots of options. These are
the common ones for contributors:
-p <IR>- Asks sorbet to print out any given intermediate representation.
- See
--helpfor available values of<IR>.
--stop-after <phase>- Useful when there's a bug in a later phase, and you want to quit early to debug.
-v,-vv,-vvv- Show
loggeroutput (increasing verbosity)
- Show
--max-threads=1- Useful for determining if you're dealing with a concurrency bug or not.
--wait-for-dbg- Will freeze Sorbet on startup and wait for a debugger to attach
- This is useful when you don't have control over launching the process (LSP)
Running the tests
To run all the tests:
bazel test //... --config=dbg
(The //... literally means "all targets".)
To run a subset of the tests curated for faster iteration and development speed, run:
bazel test test --config=dbg
Note that in bazel terms, the second test is an alias for //test:test, so we're being a bit cute here.
By default, all test output goes into files. To also print it to the screen:
bazel test //... --config=dbg --test_output=errors
If any test failed, you will see two pieces of information printed:
1. //test:test_testdata/resolver/optional_constant
2. /private/var/tmp/.../test/test_testdata/resolver/optional_constant/test.log
- the test's target (in case you want to run just this test again with
bazel test <target>) - a (runnable) file containing the test's output
To see the failing output, either:
- Re-run
bazel testwith the--test_output=errorsflag - Copy/paste the
*.logfile and run it (the output will open inless)
Testing Sorbet against pay-server
This is specific to contributing to Sorbet at Stripe.
If you are at Stripe and want to test your branch against pay-server, see http://go/types/local-dev.
Writing tests
We write tests by adding files to subfolders of the test/ directory.
Individual subfolders
Related Skills
diffs
337.7kUse the diffs tool to produce real, shareable diffs (viewer URL, file artifact, or both) instead of manual edit summaries.
clearshot
Structured screenshot analysis for UI implementation and critique. Analyzes every UI screenshot with a 5×5 spatial grid, full element inventory, and design system extraction — facts and taste together, every time. Escalates to full implementation blueprint when building. Trigger on any digital interface image file (png, jpg, gif, webp — websites, apps, dashboards, mockups, wireframes) or commands like 'analyse this screenshot,' 'rebuild this,' 'match this design,' 'clone this.' Skip for non-UI images (photos, memes, charts) unless the user explicitly wants to build a UI from them. Does NOT trigger on HTML source code, CSS, SVGs, or any code pasted as text.
openpencil
1.8kThe world's first open-source AI-native vector design tool and the first to feature concurrent Agent Teams. Design-as-Code. Turn prompts into UI directly on the live canvas. A modern alternative to Pencil.
animation-guide
A design system built with Base UI
