Skip to content

Add gem extensions - #2711

Open
vinistock wants to merge 1 commit into
mainfrom
vs_add_gem_extensions
Open

Add gem extensions#2711
vinistock wants to merge 1 commit into
mainfrom
vs_add_gem_extensions

Conversation

@vinistock

Copy link
Copy Markdown
Member

Motivation

Similar to DSL extensions, it is useful to be able to patch certain behaviour to ensure accurate gem RBI generation. Gems can expose extensions that get required ahead of time, ensuring that they patch with Tapioca compatibility in mind.

The README and test included provide an example we hit recently that can be easily fixed with an extension.

Implementation

Very similar to DSL extensions. We use Gem.find_files to detect gem extensions contributed by gems and load them ahead of time, before generation starts.

Tests

Added a test.

@vinistock vinistock self-assigned this Sep 1, 2026
@vinistock vinistock added the enhancement New feature or request label Sep 1, 2026
# Extensions are loaded before the bundle is required so that they can patch the gems
# they apply to as those gems are being loaded.
::Gem.find_files("tapioca/gem/extensions/*.rb").each do |extension|
require File.expand_path(extension)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should also allow project-local files to be loaded as extensions?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait that's what prerequire is for right? I wonder if we should merge both concepts 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it ends up being the same concept, but with different names. I'm not sure how we should approach it though.

Simply removing prerequire and turning it into gem extensions would be a breaking change. We could also support both for some time or accept the distinct names for a bit until we're ready to make a breaking change.

WDYT?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make the gem extensions behave the same way than DSL ones and allow both project local and gem provided extensions.

Once we have this we can start deprecating prerequire saying to switch to the new gem extension feature.

Comment thread README.md Outdated
Comment thread lib/tapioca/loaders/gem.rb Outdated
@vinistock
vinistock force-pushed the vs_add_gem_extensions branch from c98e587 to 4887b5a Compare September 3, 2026 19:15
@vinistock
vinistock requested a review from Morriar September 3, 2026 19:18
@vinistock
vinistock marked this pull request as ready for review September 4, 2026 14:54
@vinistock
vinistock requested a review from a team as a code owner September 4, 2026 14:54

# Extensions are loaded before the bundle is required so that they can patch the gems
# they apply to as those gems are being loaded.
::Gem.find_files("tapioca/gem/extensions/*.rb").each do |extension|

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we skip extensions from excluded gems here? Right now an excluded gem can still run code during tapioca gem if it ships lib/tapioca/gem/extensions/*.rb, because we scan and require the extension before require_bundle has a chance to honor the exclude. That weakens --exclude as the side-effect escape hatch.

Maybe we can resolve the extension path back to its Gemfile::GemSpec and skip it when the owning gem is excluded.

Comment thread README.md Outdated
# Extensions are loaded before the bundle is required so that they can patch the gems
# they apply to as those gems are being loaded.
::Gem.find_files("tapioca/gem/extensions/*.rb").each do |extension|
require File.expand_path(extension)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make the gem extensions behave the same way than DSL ones and allow both project local and gem provided extensions.

Once we have this we can start deprecating prerequire saying to switch to the new gem extension feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants