Switch gem RBI regeneration to RubyGems plugin #2343
Draft
+42
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Motivation
Current implementation
The current implementation has a few issues:
Bundler plugins
In the past, we explored Bundler plugins and couldn't make it work. We tried again and indeed something is quite odd about how the Bundler plugin hooks are emitted. The plugin seems to be required too late in the process and the
before-install-allevent is never invoked either during bundle install or bundle update.Implementation
We found another way that we might be able to make this work with the experience we want to provide, which is RubyGems plugins. These are similar to Bundler plugins, but associated to RubyGems events. To create a plugin, we just need to export a file ending with
_plugin.rband then we can hook to whatever we need.We are not interested in the RubyGems events themselves, but rather two "Bundler" events: when it started running and when it finished running. If the lockfile ended up in a different state between these two points in time, then we can run Tapioca gem to ensure that the RBIs are up to date.
The approach we took here is to gather the contents of the lockfile when the plugin gets required (before any installations happen or the lockfile is committed) and then we register an
at_exithook to run Tapioca if necessary at the end of running bundle update. The plugin file is commented with more design information.Consensus
If we agree with this direction, we can get rid of all of the code related to gem RBI generation in the Ruby LSP add-on and simply rely on the RubyGems plugin to keep those up to date.
That will not only reduce the amount of code we have to maintain, but it will better adhere to Tapioca's own logic and mechanisms.
Experience difference
If we run
bundle updateon a codebase that has gem RBIs that are out of sync, gem RBIs will be generated for dependencies that are not related to the updated gems.