Skip compiling constants with alias from another gem in namespace#1756
Merged
Skip compiling constants with alias from another gem in namespace#1756
Conversation
3 tasks
egiurleo
commented
Jan 10, 2024
paracycle
reviewed
Jan 12, 2024
493d965 to
e34c655
Compare
Morriar
approved these changes
Jan 12, 2024
Comment on lines
+267
to
+272
| constant = constantize(namespace) | ||
| next unless Module === constant | ||
|
|
||
| name_of(constant) != namespace |
Contributor
There was a problem hiding this comment.
Should we extract this as a private constant_name_differs_from_actual_namespace method to facilitate reading? Maybe just adding a comment in the block would help.
Contributor
Author
There was a problem hiding this comment.
What do you think of the comment I added?
When compiling a gem, it is possible for the gem to define an alias that includes an alias from another gem in its namespace. For example: In gem foo: ``` module Foo; end F = Foo ``` In gem bar: ``` module Foo module Bar; end end F::B = Foo::Bar ``` Because the alias `F` is not originally defined in the gem `bar`, tapioca will not skip writing that alias to the RBI file of `bar`, which will cause a Sorbet error because the alias `F` will be defined in the RBI files for both `foo` and `bar`. Now, tapioca will not generate RBI for aliases originally defined in other gems.
e34c655 to
9cc3977
Compare
paracycle
approved these changes
Jan 12, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
When compiling a gem, it is possible for the gem to define an alias that includes an alias from another gem in its namespace.
For example, let's say we have gems
fooandbar:Then the RBIs might look something like this:
There are two issues with this RBI:
F = Fooand another time in the lineF::B = Foo::Bar)Foo::Baris defined twice inbar.rbiThis commit will cause the RBI for gem
barto skip over the lineF::B = Foo::Bar, which will prevent the aliasFfrom being defined twice in RBI.More Context
I found this issue when trying to generate RBI for the
eventmachineandem-synchronygems.eventmachinedefines an aliasEM = EventMachine, andem-synchronydefines another aliasEM::S = EventMachine::Synchrony. The resulting RBI files cause a Sorbet error becauseEMis defined twice.Implementation
I implemented this change by adding an additional check to the
skip_alias?method that determines whether the namespace of a constant contains an alias and skips compiling that constant if so.Tests
I added one test that fails without this change and passes with it. All other tests continue to pass. I have tested this in Shopify core and verified no new Sorbet errors.