rubysorbet

Ruby Sorbet is autocorrecting a line even though I have wrapped it in T.unsafe()


I have this code in my test_helper.rb

require 'simplecov'
class SimpleCov::Formatter::MergedFormatter
  def format(result)
    simplecov = T.unsafe(SimpleCov::Formatter::HTMLFormatter).new
    T.unsafe(simplecov.format(result))
  end
end

this gem is very old and does not have an .rbi file. What I do not understand by the documentation at https://sorbet.org/docs/error-reference#5002

Is it coming from a gem? Sorbet does not look through the gem’s source code. Instead, there must be an *.rbi file for this gem. Try finding the *.rbi corresponding to this gem, and searching through it for the constant.

but this gem does not have any rbi files.

Sorbet autocorrects SimpleCov::Formatter::HTMLFormatter to RuboCop::Formatter::HTMLFormatter

with this error: Unable to resolve constant HTMLFormatter https://srb.help/5002

But the object is perfectly fine, and also I've wrapped it in T.unsafe(...) and it still autocorrects.

expected result: How can I escape hatch this? (I also tried setting # typed: false at the top of the file but sorbet still complains on this line)


Solution

  • The minimum bar of entry for Sorbet adoption is for all of your constants (i.e. classes/modules) to be resolved. In this case, Sorbet has no idea about the constant SimpleCov::Formatter::HTMLFormatter so it can't deal with it, regardless of T.unsafe or not.

    I am hoping that you are using the suggested Tapioca based workflow to generate your RBI files, in which case, the RBI file for simplecov should have been generated for you. You don't need to find an .rbi file for the gem from somewhere, Tapioca will generate it from the gem itself.

    If you are using a different workflow, you can easily just create an RBI file (preferably under sorbet/rbi folder) that defines the constant and any methods that you need on it to get unblocked. Something like the following should work fine for your example:

    class SimpleCov::Formatter::HTMLFormatter
      def format(result); end
    end