Generate RBI files for your project's gem dependencies
Tapioca is a library used to generate RBI (Ruby interface) files for use with Sorbet. RBI files provide the structure (classes, modules, methods, parameters) of the gem/library to Sorbet to assist with typechecking.
As yet, no gem exports type information in a consumable format and it would be a huge effort to manually maintain such an interface file for all the gems that your codebase depends on. Thus, there is a need for an automated way to generate the appropriate RBI file for a given gem. The
tapiocagem, developed at Shopify, is able to do exactly that to almost 99% accuracy. It can generate the definitions for all statically defined types and most of the runtime defined types exported from Ruby gems (non-Ruby gems are not handled yet).
When you run
tapioca syncin a project,
tapiocaloads all the gems that are in your dependency list from the Gemfile into memory. It then performs runtime introspection on the loaded types to understand their structure and generates an appropriate RBI file for each gem with a versioned filename.
For gems that have a normal default
requireand load all of their constants through such a require, everything works seamlessly. However, for gems that are marked as
require: falsein the Gemfile, or for gems that export optionally loaded types via different requires, where a single require does not load the whole gem code into memory,
tapiocawill not be able to load some of the types into memory and, thus, won't be able to generate complete RBIs for them. For this reason, we need to keep a small external file named
sorbet/tapioca/require.rbthat is executed after all the gems in the Gemfile have been required and before generation of gem RBIs have started. This file is responsible for adding the requires for additional files from gems, which are not covered by the default require.
For example, suppose you are using the class
BetterHtml::Parserexported from the
better_htmlgem. Just doing a
require "better_html"(which is the default require) does not load that type:
$ bundle exec pry  pry(main)> require 'better_html' => true  pry(main)> BetterHtml => BetterHtml  pry(main)> BetterHtml::Parser NameError: uninitialized constant BetterHtml::Parser from (pry):3:in `__pry__`  pry(main)> require 'better_html/parser' => true  pry(main)> BetterHtml::Parser => BetterHtml::Parser
In order to make sure that
tapiocacan reflect on that type, we need to add the line
require "better_html/parser"to the
sorbet/tapioca/require.rbfile. This will make sure
BetterHtml::Parseris loaded into memory and a type annotation is generated for it in the
better_html.rbifile. If this extra
requireline is not added to
sorbet/tapioca/require.rbfile, then the definition for that type will be missing from the RBI file.
If you ever run into a case, where you add a gem or update the version of a gem and run
tapioca syncbut don't have some types you expect in the generated gem RBI files, you will need to make sure you have added the necessary requires to the
You can use the command
tapioca requireto auto-populate the
sorbet/tapioca/require.rbfile with all the requires found in your application. Once the file generated, you should review it, remove all unnecessary requires and commit it.
Add this line to your application's
group :development do gem 'tapioca', require: false end
and do not forget to execute
$ bundle exec tapioca Commands: tapioca --version, -v # show version tapioca dsl [constant...] # generate RBIs for dynamic methods tapioca generate [gem...] # generate RBIs from gems tapioca help [COMMAND] # Describe available commands or one specific command tapioca init # initializes folder structure tapioca require # generate the list of files to be required by tapioca tapioca sync # sync RBIs to Gemfile tapioca todo # generate the list of unresolved constants
Options: --pre, -b, [--prerequire=file] # A file to be required before Bundler.require is called --post, -a, [--postrequire=file] # A file to be required after Bundler.require is called --out, -o, [--outdir=directory] # The output directory for generated RBI files --cmd, -c, [--generate-command=command] # The command to run to regenerate RBI files -x, [--exclude=gem [gem ...]] # Excludes the given gem(s) from RBI generation --typed, -t, [--typed-overrides=gem:level [gem:level ...]] # Overrides for typed sigils for generated gem RBIs
This will create the
sorbet/tapioca/require.rbfiles for you, if they don't exist. If any of the files already exist, they will not be changed.
tapioca generate [gems...]
This will generate RBIs for the specified gems and place them in the RBI directory.
This will sync the RBIs with the gems in the Gemfile and will add, update, and remove RBIs as necessary.
This will generate the file
sorbet/rbi/todo.rbidefining all unresolved constants as empty modules.
tapioca dsl [constant...]
This will generate DSL RBIs for specified constants (or for all handled constants, if a constant name is not supplied). You can read about DSL RBI generators supplied by
tapiocain the manual.
--prerequire [file]: A file to be required before
--postrequire [file]: A file to be required after
--out [directory]: The output directory for generated RBI files, default to
--generate-command [command]: The command to run to regenerate RBI files (used in header comment of the RBI files), defaults to the current command.
--typed-overrides [gem:level]: Overrides typed sigils for generated gem RBIs for gem
levelcan be one of
strong, see the Sorbet docs for more details).
Bug reports and pull requests are welcome on GitHub at https://github.com/Shopify/tapioca. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.