Shopify/tapioca

Tapioca automatically converts nested style to compact style, causing 'Unable to resolve constant $class' error in RBI

hiko1129 opened this issue · 1 comments

sorbet version: 0.5.11332
tapioca version: 0.13.1
ruby version: 3.2.2

I encountered an issue when using Tapioca to generate RBI files for a gem dependency. The generated RBI file contains an Unable to resolve constant $class error.

Here's a minimal reproduction of the issue:

In repo X, I have the following files:
lib/x.rb

module X
  class Foo
  end

  class Bar
    def foo
      Foo.new
    end
  end
end

rbi/defs.rbi

# typed: strong

module X
  class Foo
  end

  class Bar
    sig { returns(Foo) }
    def foo
    end
  end
end

In repo Y, which depends on the gem from repo X, I have the following files:
Gemfile

gem "x"

After running bin/tapioca gem, the generated RBI file at sorbet/rbi/gems/x.rbi looks like this:

module X; end

class X::Foo
end

class X::Bar
  sig { returns(Foo) } # Unable to resolve constant `Foo`
  def foo
  end
end

Suspect file: pipeline.rb on GitHub

Thanks for the report. This is definitely annoying but I think the implementation will be too complex with too much overhead that it might not be worth it. Currently the exported gem rbis functionality is quiet simple. We parse the contents of rbi/ and merge it with generated RBI definitions. So we don't do any introspection for the contents of the rbi/ folder. For this to be automated we'd have to find the fully qualified name of each constant referenced in a sig and modify the tree.

A better solution could be encouraging gem authors to use fully qualified names while writing signatures. We could have a cop in https://github.com/shopify/rubocop-sorbet to do that but it's hard to enforce adoption of rubocop-sorbet.