Search code examples
ruby-on-railsrubysorbet

Why generated rbi by Tapioca doesn't include Devise current_user method?


I'm trying to add Devise to my Rails project that uses Sorbet. After adding Devise I ran bundle exec tapioca dsl and bundle exec tapioca gem. Now, on my controllers, the type check is complaining that it can not find the current_user helper method.

I noticed that on the generated file sorbet/rbi/gems/devise@4.9.2.rbi it includes the following description:

  class << self
    # Define authentication filters and accessor helpers based on mappings.
    # These filters should be used inside the controllers as before_actions,
    # so you can control the scope of the user who should be signed in to
    # access that specific controller/action.
    # Example:
    #
    #   Roles:
    #     User
    #     Admin
    #
    #   Generated methods:
    #     authenticate_user!  # Signs user in or redirect
    #     authenticate_admin! # Signs admin in or redirect
    #     user_signed_in?     # Checks whether there is a user signed in or not
    #     admin_signed_in?    # Checks whether there is an admin signed in or not
    #     current_user        # Current signed in user
    #     current_admin       # Current signed in admin
    #     user_session        # Session data available only to the user scope
    #     admin_session       # Session data available only to the admin scope
    #
    #   Use:
    #     before_action :authenticate_user!  # Tell devise to use :user map
    #     before_action :authenticate_admin! # Tell devise to use :admin map
    #
    # source://devise//lib/devise/controllers/helpers.rb#112
    def define_helpers(mapping); end
  end

Is there something that I'm missing to have those methods correctly identified by bundle exec srb tc?

On my ApplicationController class, I tried to include Devise::Controllers::Helpers, but that didn't help.


Solution

  • I got the answer on the tapioca repository on this GitHub issue that I created there.

    I'm quoting the answer from Kaan Ozkan:

    Those methods are defined when define_helpers is called. Therefore, define_helpers would need to be triggered during RBI generation for them to be visible in runtime and generated by tapioca.

    I think the simplest solution is manually defining all the generated methods in sorbet/rbi/shims/gems/devise.rbi:

    module Devise::Controllers::Helpers::ClassMethods
      def current_user; end
    end