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

Add compiler for ActiveRecord delegated types #1241

Merged
merged 13 commits into from
Dec 13, 2022

Conversation

fcheung
Copy link
Contributor

@fcheung fcheung commented Oct 24, 2022

Motivation

ActiveRecord's delegated types feature adds a number of methods to active record models that were not included in the rbis generated by tapioca (See also #1240)

Implementation

Unlike associations, scopes etc. activerecord does not keep any internal metadata about which delegated types have been declared. I added an extension that records the parameters that delegated_type was called with so that this data can later be accessed from the compiler

Tests

Tests added to spec/tapioca/dsl/compilers/active_record_delegated_types_spec.rb

@fcheung
Copy link
Contributor Author

fcheung commented Oct 31, 2022

Any more feedback @KaanOzkan ?

Copy link
Contributor

@KaanOzkan KaanOzkan left a comment

Choose a reason for hiding this comment

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

It LGTM but I'd like another review from someone with experience on delegated types.

Also can we fix the linting error? You can rebase to fix rest of the CI.

@fcheung
Copy link
Contributor Author

fcheung commented Nov 1, 2022

Thanks - I have fixed that white space error and rebased

mod.create_method(
"#{role}_name",
parameters: [],
return_type: "String",
Copy link
Member

Choose a reason for hiding this comment

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

String isn't the return type of role_name. It is a ActiveSupport::StringInquirer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

)

mod.create_method(
"build_#{role}",
Copy link
Member

Choose a reason for hiding this comment

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

Ins't build_#{role} already added by the belongs_to DSL generator?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don’t think so because the belongs_to is polymorphic

@fcheung
Copy link
Contributor Author

fcheung commented Nov 11, 2022

Could you have another look @rafaelfranca ?

@fcheung
Copy link
Contributor Author

fcheung commented Nov 13, 2022

Those ci failures against 6.1 are odd :

it compiles constants of generic types ERROR (0.04s)
Minitest::UnexpectedError: TypeError: T.let: Expected type GenericInterface[::Numeric], got type Concrete with hash 24942688895262217
Caller: /tmp/d20221111-6657-m3v2y/lib/generic.rb:16
/tmp/d20221111-6657-m3v2y/lib/generic.rb:16:in <top (required)>' /home/runner/work/tapioca/tapioca/lib/tapioca.rb:23:in block in silence_warnings'
/opt/hostedtoolcache/Ruby/2.7.6/x64/lib/ruby/2.7.0/rubygems/user_interaction.rb:47:in use_ui' /home/runner/work/tapioca/tapioca/lib/tapioca.rb:22:in silence_warnings'
/home/runner/work/tapioca/tapioca/spec/tapioca/gem/pipeline_spec.rb:3328:in `block (2 levels) in class:PipelineSpec'

doesn't seem related to my changes. It also fails on master for me when I run that test file on its own

@st0012
Copy link
Member

st0012 commented Nov 17, 2022

Hey @fcheung, the build has been fixed and once you rebased main it should run green.

@fcheung
Copy link
Contributor Author

fcheung commented Nov 17, 2022

Thanks @st0012 - I have rebased

@fcheung
Copy link
Contributor Author

fcheung commented Nov 17, 2022

This is also looking like an unrelated failure

cli
  gem
    generate
      it must generate RBIs for gems whose folder location starts with the same prefix as project folder PASS (1.85s)
      it uses ignores `abort` and `exit` calls inside autoloaded files PASS (11.87s)
      it must not generate RBIs for missing gem specs                 FAIL (2.58s)
        ########## STDOUT ##########
        <empty>
        ########## STDERR ##########
        bundler: failed to load command: tapioca (/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/bin/tapioca)
        /opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/3.1.0/rubygems/specification.rb:2111:in `method_missing': undefined method `identifier' for #<Gem::Specification:0x00007fb486753950 @extension_dir=nil, @full_gem_path=nil, @gem_dir=nil, @ignored=nil, @bin_dir=nil, @cache_dir=nil, @cache_file=nil, @doc_dir=nil, @ri_dir=nil, @spec_dir=nil, @spec_file=nil, @gems_dir=nil, @base_dir=nil, @loaded=false, @activated=false, @loaded_from="/opt/hostedtoolcache/Ruby/3.1.2/x64/lib/ruby/gems/3.1.0/specifications/parser-3.1.2.1.gemspec", @original_platform=nil, @installed_by_version=#<Gem::Version "3.3.7">, @autorequire=nil, @date=2022-08-08 00:00:00 UTC, @description="A Ruby parser written in pure Ruby.", @email=["[email protected]"], @homepage="https://github.com/whitequark/parser", @name="parser", @post_install_message=nil, @signing_key=nil, @summary="A Ruby parser written in pure Ruby.", @version=#<Gem::Version "3.1.2.1">, @authors=["whitequark"], @bindir="bin", @cert_chain=[], @dependencies=[<Gem::Dependency type=:runtime name="ast" requirements="~> 2.4.1">, <Gem::Dependency type=:development name="bundler" requirements=">= 1.15, < 3.0.0">, <Gem::Dependency type=:development name="rake" requirements="~> 13.0.1">, <Gem::Dependency type=:development name="racc" requirements="= 1.4.15">, <Gem::Dependency type=:development name="cliver" requirements="~> 0.3.2">, <Gem::Dependency type=:development name="yard" requirements=">= 0">, <Gem::Dependency type=:development name="kramdown" requirements=">= 0">, <Gem::Dependency type=:development name="minitest" requirements="~> 5.10">, <Gem::Dependency type=:development name="simplecov" requirements="~> 0.15.1">, <Gem::Dependency type=:development name="gauntlet" requirements=">= 0">], @executables=["ruby-parse", "ruby-rewrite"], @extensions=[], @extra_rdoc_files=[], @files=["bin/ruby-parse", "bin/ruby-rewrite"], @licenses=["MIT"], @metadata={"bug_tracker_uri"=>"https://github.com/whitequark/parser/issues", "changelog_uri"=>"https://github.com/whitequark/parser/blob/v3.1.2.1/CHANGELOG.md", "documentation_uri"=>"https://www.rubydoc.info/gems/parser/3.1.2.1", "source_code_uri"=>"https://github.com/whitequark/parser/tree/v3.1.2.1"}, @platform="ruby", @rdoc_options=[], @require_paths=["lib"], @required_ruby_version=#<Gem::Requirement:0x00007fb486750b10 @requirements=[[">=", #<Gem::Version "2.0.0">]]>, @required_rubygems_version=#<Gem::Requirement:0x00007fb4867519c0 @requirements=[[">=", #<Gem::Version "0">]]>, @requirements=[], @rubygems_version="3.3.7", @specification_version=4, @test_files=[], @new_platform="ruby", @full_name="parser-3.1.2.1", @source=#<Bundler::Source::Rubygems:0x32260 locally installed gems> parser-3.1.2.1> (NoMethodError)

@bdewater-thatch
Copy link
Contributor

I checked out this branch on our app, the types look correct and srb tc still passes afterwards 🎉

Copy link
Member

@paracycle paracycle left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, this looks great!

Comment on lines +60 to +66
describe "with relations enabled" do
before do
require "tapioca/dsl/compilers/active_record_relations"
activate_other_dsl_compilers(ActiveRecordRelations)
end

describe "without errors" do
Copy link
Member

Choose a reason for hiding this comment

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

I think we don't strictly need either of these describe blocks, since they don't seem to be used in any of the test cases below. I know this PR has been open for a long time, so I'm more than happy to refactor this after a merge.

@paracycle paracycle merged commit e0d3922 into Shopify:main Dec 13, 2022
@paracycle paracycle added the enhancement New feature or request label Dec 19, 2022
@shopify-shipit shopify-shipit bot temporarily deployed to production December 19, 2022 22:14 Inactive
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.

6 participants