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 root namespace #472

Closed
wants to merge 3 commits into from
Closed

Fix root namespace #472

wants to merge 3 commits into from

Conversation

KaanOzkan
Copy link
Contributor

Motivation

Implementation

Tests

def self.perform_async(customer_id); end

sig { params(interval: T.any(DateTime, Time), customer_id: Integer).returns(String) }
sig { params(interval: T.any(DateTime, Time), customer_id: ::Integer).returns(String) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

String, DateTime, Time not affected by the name patch.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, this is interesting. I would have expected this to have been fixed too.

Copy link
Contributor

Choose a reason for hiding this comment

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

The reason these aren't changing is because they're hard-coded in the code: https://github.com/Shopify/tapioca/blob/main/lib/tapioca/compilers/dsl/sidekiq_worker.rb#L59.

The Integer param is derived from the live ruby code:
https://github.com/Shopify/tapioca/blob/main/lib/tapioca/compilers/dsl/sidekiq_worker.rb#L59

I wonder if there should be some helper that allows us to specify these manual types via code and have it get compiled to a string.

Comment on lines 9 to 13
@name ||= ::Tapioca::Reflection.qualified_name_of(@raw_type).freeze
end
end

T::Types::Simple::Private::Pool.instance_variable_set(:@cache, ObjectSpace::WeakMap.new)
Copy link
Member

@paracycle paracycle Aug 31, 2021

Choose a reason for hiding this comment

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

The problem apparently was that existing instances of T::Types::Simple had memoized their names in the @name ivar, so they were holding on to that even after we prepend the patch.

The fix is to use a different memoizing ivar in the prepended module, so that they all start fresh. However, that causes an infinite loop descending into the qualified_name_of method on Reflection since the method has a signature which tries to do the same thing recursively.

Thus, we need a copy of the qualified_name_of method here without a signature to fix everything:

Suggested change
@name ||= ::Tapioca::Reflection.qualified_name_of(@raw_type).freeze
end
end
T::Types::Simple::Private::Pool.instance_variable_set(:@cache, ObjectSpace::WeakMap.new)
@qualified_name ||= qualified_name_of(@raw_type).freeze
end
private
NAME_METHOD = Module.instance_method(:name)
def qualified_name_of(constant)
name = NAME_METHOD.bind(constant).call
return if name.nil?
if name.start_with?("::")
name
else
"::#{name}"
end
end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fixed the T.nilable conversions. However, there are still some types that are unaffected by the patch in the tests. Not sure if it's a blocker for merging this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Which ones for example? This should only affect the things we generate inside sigs and other Sorbet runtime related things, like enums and tstructs. Everything else, we need to do manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@paracycle paracycle mentioned this pull request Sep 1, 2021
Qualified name specifies the name from the root namespace. This will prevent errors in the future where we have clashing constant names that were separated by their fully qualified names. During RBI generation we used to drop the leading `::` anchors and the constant would point to a wrong type than what the developer intended.

e.g. #466
@KaanOzkan KaanOzkan force-pushed the ko/fix-root-namespace branch from 6b9ca80 to 6f03ce6 Compare September 1, 2021 17:54
@KaanOzkan KaanOzkan force-pushed the ko/fix-root-namespace branch from 6f03ce6 to 427de13 Compare September 1, 2021 18:13
The symbol queue operates on the full names of constants and does not
know how to handle a name that starts with a `::`. If such a name is
added to the queue, it looks like it is an alias of a constant with the
same name but without the `::` prefix.
@paracycle
Copy link
Member

Superseded by #780

@paracycle paracycle closed this Feb 12, 2022
@rafaelfranca rafaelfranca deleted the ko/fix-root-namespace branch March 29, 2023 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants