Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Reflection#inherited_ancestors_of method for modules #1530

Merged
merged 2 commits into from
Jun 12, 2023

Conversation

paracycle
Copy link
Member

@paracycle paracycle commented Jun 12, 2023

Motivation

As reported by @searls on the Sorbet Slack, Tapioca would remove include Kernel lines from a module when generating the RBI file for the gem.

Implementation

It turns out the problem stems from the implementation of Reflection#inherited_ancestors_of method doing the wrong thing for modules. For classes we look at the ancestors of the class' superclass (or Object if no explicit superclass is set), but for modules we used to look at the ancestors of Module. However, a module is an instance of Module, so the ancestors of Module would not be in the original module's ancestor list anyway. What we needed to check was the ancestors of a newly created Module instance, and return those.

This ends up affecting only Kernel mixed into a module, since Module's ancestors only has Kernel as a mixin, all the other ancestors are classes like Object and BasicObject. That's probably why this behaviour was not caught earlier.

Tests

Added a test for module mixins that exhibits the wrong behaviour and passes after the change.

We used to return the ancestors of the Module class itself, as inherited ancestors for modules, but that was wrong. We should return the ancestors of an empty module (i.e. `Module.new`) instead.
@paracycle paracycle requested a review from a team as a code owner June 12, 2023 19:55
@paracycle paracycle added the bug Something isn't working label Jun 12, 2023
@paracycle paracycle self-assigned this Jun 12, 2023
@paracycle paracycle merged commit 573f5a0 into main Jun 12, 2023
@paracycle paracycle deleted the uk-fix-module-mixins branch June 12, 2023 21:27
@shopify-shipit shopify-shipit bot temporarily deployed to production June 23, 2023 18:32 Inactive
Morriar pushed a commit that referenced this pull request Jul 12, 2023
Since #1530, inclusion of Kernel is properly generated by Tapioca.
When generating RBIs, this creates an issue for Isolation modules
where the `requires_ancestor { Kernel }` on `DslCompiler` is
reported as useless.

we do not have this error locally in Tapioca because the include
is conditional so Sorbet doesn't see it. But we get it with RBIs
in other projects because Tapioca finds the include through
reflection.

The Isolation sub-modules do not need to include Kernel.
It seems required only to call fork and exit so we can replace
them by `requires_ancestor { Kernel }`.

Closes #1563.

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants